Skip to content

Instantly share code, notes, and snippets.

@SonoSooS
Created January 9, 2018 05:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save SonoSooS/f39a50f4fd56d1c4df7f647622a76c8c to your computer and use it in GitHub Desktop.
Save SonoSooS/f39a50f4fd56d1c4df7f647622a76c8c to your computer and use it in GitHub Desktop.
Redshift for 3DS
/*
CTR_Redshift - handcrafted CPU image downscaler
Copyright (C) 2017-2018 Sono (https://github.com/MarcuzD)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Lesser Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Redshift - https://github.com/jonls/redshift
Redshift is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Redshift is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Redshift. If not, see <http://www.gnu.org/licenses/>.
Copyright (c) 2009-2017 Jon Lund Steffensen <jonlst@gmail.com>
*/
#include <3ds.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "testimage_bin.h"
//Part of Redshift
#include "redshift.h"
#include "colorramp.h"
void ReadAt(u32* dst, u8 idx, int screen)
{
u32 pos = idx;
GSPGPU_WriteHWRegs(screen & 1 ? 0x400580 : 0x400480, &pos, 4);
GSPGPU_ReadHWRegs(screen & 1 ? 0x400584 : 0x400484, dst, 4);
}
void WriteAt(const u32* dst, u8 idx, int screen)
{
u32 pos = idx & 0xFF;
GSPGPU_WriteHWRegs(screen & 1 ? 0x400580 : 0x400480, &pos, 4);
GSPGPU_WriteHWRegs(screen & 1 ? 0x400584 : 0x400484, dst, 4);
}
void ReadAll(u32* dst, int screen)
{
u8 idx = 0;
do
{
ReadAt(dst++, idx, screen);
}
while(++idx);
}
void WriteAll(const u32* dst, int screen)
{
u8 idx = 0;
do
{
WriteAt(dst++, idx, screen);
}
while(++idx);
}
void ClampCS(color_setting_t* cs)
{
if(cs->temperature < MIN_TEMP) cs->temperature = MIN_TEMP;
if(cs->temperature > MAX_TEMP) cs->temperature = MAX_TEMP;
if(cs->gamma[0] < MIN_GAMMA) cs->gamma[0] = MIN_GAMMA;
if(cs->gamma[1] < MIN_GAMMA) cs->gamma[1] = MIN_GAMMA;
if(cs->gamma[2] < MIN_GAMMA) cs->gamma[2] = MIN_GAMMA;
if(cs->gamma[0] > MAX_GAMMA) cs->gamma[0] = MAX_GAMMA;
if(cs->gamma[1] > MAX_GAMMA) cs->gamma[1] = MAX_GAMMA;
if(cs->gamma[2] > MAX_GAMMA) cs->gamma[2] = MAX_GAMMA;
if(cs->brightness < MIN_BRIGHTNESS) cs->brightness = MIN_BRIGHTNESS;
if(cs->brightness > MAX_BRIGHTNESS) cs->brightness = MAX_BRIGHTNESS;
}
void ApplyCS(color_setting_t* cs, int screen)
{
u16* c = malloc(0x600);
u8 i = 0;
struct
{
u8 r;
u8 g;
u8 b;
u8 z;
} *px = malloc(0x400);
//ReadAll((u32*)px, screen);
do
{
*(u32*)&px[i] = i | (i << 8) | (i << 16);
}
while(++i);
do
{
*(c + i + 0x000) = px[i].r | (px[i].r << 8);
*(c + i + 0x100) = px[i].g | (px[i].g << 8);
*(c + i + 0x200) = px[i].b | (px[i].b << 8);
}
while(++i);
colorramp_fill(c + 0x000, c + 0x100, c + 0x200, 0x100, cs);
do
{
px[i].r = *(c + i + 0x000) >> 8;
px[i].g = *(c + i + 0x100) >> 8;
px[i].b = *(c + i + 0x200) >> 8;
}
while(++i);
WriteAll((u32*)px, screen);
free(px);
free(c);
}
int main()
{
gfxInit(GSP_RGBA8_OES, GSP_RGBA8_OES, false);
gfxSetDoubleBuffering(GFX_TOP, 0);
gfxSet3D(1);
PrintConsole console;
consoleInit(GFX_BOTTOM, &console);
gfxSwapBuffers();
gfxSwapBuffers();
int redraw = 1;
u32 kDown = 0;
u32 kHeld = 0;
u32 kUp = 0;
touchPosition touch;
int i = 0;
int sel = 0;
color_setting_t cs;
memset(&cs, 0, sizeof(cs));
cs.temperature = NEUTRAL_TEMP;
cs.gamma[0] = 1.0F;
cs.gamma[1] = 1.0F;
cs.gamma[2] = 1.0F;
cs.brightness = 1.0F;
do
{
u32* src = testimage_bin;
u32* ptr = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, 0, 0);
for(i = 0; i != 400 * 240; i++) *(ptr++) = __builtin_bswap32(*(src++));
src = testimage_bin;
ptr = gfxGetFramebuffer(GFX_TOP, GFX_RIGHT, 0, 0);
for(i = 0; i != 400 * 240; i++) *(ptr++) = __builtin_bswap32(*(src++));
}
while(0);
while(aptMainLoop())
{
hidScanInput();
kDown = hidKeysDown();
kHeld = hidKeysHeld();
kUp = hidKeysUp();
if(kHeld & KEY_SELECT) break;
if(kHeld & KEY_TOUCH) hidTouchRead(&touch);
if((kDown | kUp) || (kHeld & KEY_TOUCH)) redraw = 1;
if(kDown & KEY_RIGHT)
{
if(++sel > 4) sel = 4;
}
if(kDown & KEY_LEFT)
{
if(--sel < 0) sel = 0;
}
if(kDown & (KEY_X | KEY_Y))
{
memset(&cs, 0, sizeof(cs));
cs.temperature = NEUTRAL_TEMP;
cs.gamma[0] = 1.0F;
cs.gamma[1] = 1.0F;
cs.gamma[2] = 1.0F;
cs.brightness = 1.0F;
}
if((((kDown & KEY_UP) ? 1 : 0) ^ ((kDown & KEY_DOWN) ? 1 : 0)))
{
if(!sel)
{
if(kDown & KEY_UP)
cs.temperature += (kHeld & (KEY_L | KEY_R)) ? 1 : 100;
else
cs.temperature -= (kHeld & (KEY_L | KEY_R)) ? 1 : 100;
}
else
{
float* f = 0;
if(!(sel >> 2))
f = &cs.gamma[sel - 1];
if(!(sel ^ 4))
f = &cs.brightness;
if(f)
{
if(kDown & KEY_UP)
*f += (kHeld & (KEY_L | KEY_R)) ? 0.01F : 0.1F;
else
*f -= (kHeld & (KEY_L | KEY_R)) ? 0.01F : 0.1F;
}
}
}
if(kDown)
{
ClampCS(&cs);
if(kHeld & KEY_A)
ApplyCS(&cs, 1);
if(kHeld & KEY_B)
ApplyCS(&cs, 0);
}
if(redraw)
{
console.cursorX = 0;
console.cursorY = 0;
puts("CTR_Redshift v0.0 by Sono\n");
if(kHeld & KEY_TOUCH)
{
printf("Touch: %03i x %03i\n", touch.px, touch.py);
}
else puts("\e[2K");
printf("\n%c Colortemp: %iK\n\n", (sel == 0 ? '>' : ' '), cs.temperature);
printf("%c Gamma[R]: %.2f\n", (sel == 1 ? '>' : ' '), cs.gamma[0]);
printf("%c Gamma[G]: %.2f\n", (sel == 2 ? '>' : ' '), cs.gamma[1]);
printf("%c Gamma[B]: %.2f\n", (sel == 3 ? '>' : ' '), cs.gamma[2]);
printf("\n%c Brightness: %.2f\n\n", (sel == 4 ? '>' : ' '), cs.brightness);
redraw = 0;
}
gspWaitForVBlank();
}
ded:
gfxExit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment