33extern int gsKit_texture_finish(GSGLOBAL *
gsGlobal, GSTEXTURE *Texture);
35int gsKit_texture_finish(GSGLOBAL *
gsGlobal, GSTEXTURE *Texture)
42 printf(
"VRAM Allocation Failed. Will not upload texture.\n");
46 if(Texture->
Clut != NULL)
55 printf(
"VRAM CLUT Allocation Failed. Will not upload texture.\n");
61 gsKit_texture_upload(
gsGlobal, Texture);
66 if(Texture->
Clut != NULL)
74 gsKit_setup_tbw(Texture);
80int gsKit_texture_png(GSGLOBAL *
gsGlobal, GSTEXTURE *Texture,
char *Path)
83 FILE* File = fopen(Path,
"rb");
86 printf(
"Failed to load PNG file: %s\n", Path);
92 png_uint_32 width, height;
93 png_bytep *row_pointers;
96 int row, i, k=0, j, bit_depth, color_type, interlace_type;
98 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp) NULL, NULL, NULL);
102 printf(
"PNG Read Struct Init Failed\n");
107 info_ptr = png_create_info_struct(png_ptr);
111 printf(
"PNG Info Struct Init Failed\n");
113 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
117 if(setjmp(png_jmpbuf(png_ptr)))
119 printf(
"Got PNG Error!\n");
120 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
125 png_init_io(png_ptr, File);
127 png_set_sig_bytes(png_ptr, sig_read);
129 png_read_info(png_ptr, info_ptr);
131 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,&interlace_type, NULL, NULL);
134 png_set_strip_16(png_ptr);
136 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 4)
137 png_set_expand(png_ptr);
139 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
140 png_set_tRNS_to_alpha(png_ptr);
142 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
144 png_read_update_info(png_ptr, info_ptr);
146 Texture->
Width = width;
150 Texture->
Clut = NULL;
152 if(png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGB_ALPHA)
154 int row_bytes = png_get_rowbytes(png_ptr, info_ptr);
156 Texture->
Mem = memalign(128, gsKit_texture_size_ee(Texture->
Width, Texture->
Height, Texture->
PSM));
158 row_pointers = calloc(height,
sizeof(png_bytep));
160 for (row = 0; row < height; row++) row_pointers[row] = malloc(row_bytes);
162 png_read_image(png_ptr, row_pointers);
164 struct pixel { u8 r,g,b,a; };
165 struct pixel *Pixels = (
struct pixel *) Texture->
Mem;
167 for (i=0;i<height;i++) {
168 for (j=0;j<width;j++) {
169 Pixels[k].r = row_pointers[i][4*j];
170 Pixels[k].g = row_pointers[i][4*j+1];
171 Pixels[k].b = row_pointers[i][4*j+2];
172 Pixels[k++].a = 128-((int) row_pointers[i][4*j+3] * 128 / 255);
176 for(row = 0; row < height; row++) free(row_pointers[row]);
180 else if(png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGB)
182 int row_bytes = png_get_rowbytes(png_ptr, info_ptr);
184 Texture->
Mem = memalign(128, gsKit_texture_size_ee(Texture->
Width, Texture->
Height, Texture->
PSM));
186 row_pointers = calloc(height,
sizeof(png_bytep));
188 for(row = 0; row < height; row++) row_pointers[row] = malloc(row_bytes);
190 png_read_image(png_ptr, row_pointers);
192 struct pixel3 { u8 r,g,b; };
193 struct pixel3 *Pixels = (
struct pixel3 *) Texture->
Mem;
195 for (i=0;i<height;i++) {
196 for (j=0;j<width;j++) {
197 Pixels[k].r = row_pointers[i][4*j];
198 Pixels[k].g = row_pointers[i][4*j+1];
199 Pixels[k++].b = row_pointers[i][4*j+2];
203 for(row = 0; row < height; row++) free(row_pointers[row]);
207 else if(png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE)
209 png_colorp palette = NULL;
211 png_bytep trans = NULL;
214 png_get_PLTE(png_ptr, info_ptr, &palette, &num_pallete);
215 png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, NULL);
218 struct png_clut { u8 r, g, b, a; };
223 int row_bytes = png_get_rowbytes(png_ptr, info_ptr);
225 Texture->
Mem = memalign(128, gsKit_texture_size_ee(Texture->
Width, Texture->
Height, Texture->
PSM));
227 row_pointers = calloc(height,
sizeof(png_bytep));
229 for(row = 0; row < height; row++) row_pointers[row] = malloc(row_bytes);
231 png_read_image(png_ptr, row_pointers);
233 Texture->
Clut = memalign(128, gsKit_texture_size_ee(8, 2,
GS_PSM_CT32));
236 unsigned char *pixel = (
unsigned char *)Texture->
Mem;
237 struct png_clut *clut = (
struct png_clut *)Texture->
Clut;
241 for (i = num_pallete; i < 16; i++) {
242 memset(&clut[i], 0,
sizeof(clut[i]));
245 for (i = 0; i < num_pallete; i++) {
246 clut[i].r = palette[i].red;
247 clut[i].g = palette[i].green;
248 clut[i].b = palette[i].blue;
252 for (i = 0; i < num_trans; i++)
253 clut[i].a = trans[i] >> 1;
255 for (i = 0; i < Texture->
Height; i++) {
256 for (j = 0; j < Texture->
Width / 2; j++)
257 memcpy(&pixel[k++], &row_pointers[i][1 * j], 1);
261 unsigned char *tmpdst = (
unsigned char *)Texture->
Mem;
262 unsigned char *tmpsrc = (
unsigned char *)pixel;
264 for (
byte = 0;
byte < gsKit_texture_size_ee(Texture->
Width, Texture->
Height, Texture->
PSM);
byte++) tmpdst[
byte] = (tmpsrc[
byte] << 4) | (tmpsrc[byte] >> 4);
266 for(row = 0; row < height; row++) free(row_pointers[row]);
271 else if (bit_depth == 8)
273 int row_bytes = png_get_rowbytes(png_ptr, info_ptr);
275 Texture->
Mem = memalign(128, gsKit_texture_size_ee(Texture->
Width, Texture->
Height, Texture->
PSM));
277 row_pointers = calloc(height,
sizeof(png_bytep));
279 for(row = 0; row < height; row++) row_pointers[row] = malloc(row_bytes);
281 png_read_image(png_ptr, row_pointers);
283 Texture->
Clut = memalign(128, gsKit_texture_size_ee(16, 16,
GS_PSM_CT32));
286 unsigned char *pixel = (
unsigned char *)Texture->
Mem;
287 struct png_clut *clut = (
struct png_clut *)Texture->
Clut;
291 for (i = num_pallete; i < 256; i++) {
292 memset(&clut[i], 0,
sizeof(clut[i]));
295 for (i = 0; i < num_pallete; i++) {
296 clut[i].r = palette[i].red;
297 clut[i].g = palette[i].green;
298 clut[i].b = palette[i].blue;
302 for (i = 0; i < num_trans; i++)
303 clut[i].a = trans[i] >> 1;
306 for (i = 0; i < num_pallete; i++) {
307 if ((i & 0x18) == 8) {
308 struct png_clut tmp = clut[i];
309 clut[i] = clut[i + 8];
314 for (i = 0; i < Texture->
Height; i++) {
315 for (j = 0; j < Texture->
Width; j++) {
316 memcpy(&pixel[k++], &row_pointers[i][1 * j], 1);
320 for(row = 0; row < height; row++) free(row_pointers[row]);
327 printf(
"This texture depth is not supported yet!\n");
332 png_read_end(png_ptr, NULL);
333 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
336 return gsKit_texture_finish(
gsGlobal, Texture);
338 printf(
"ERROR: gsKit_texture_png unimplemented.\n");
343int gsKit_texture_bmp(GSGLOBAL *
gsGlobal, GSTEXTURE *Texture,
char *Path)
354 File = fopen(Path,
"rb");
357 printf(
"BMP: Failed to load bitmap: %s\n", Path);
360 if (fread(&Bitmap.FileHeader, 1,
sizeof(Bitmap.FileHeader), File) !=
sizeof(Bitmap.FileHeader))
362 printf(
"BMP: Could not load bitmap: %s\n", Path);
367 if (fread(&Bitmap.InfoHeader, 1,
sizeof(Bitmap.InfoHeader), File) !=
sizeof(Bitmap.InfoHeader))
369 printf(
"BMP: Could not load bitmap: %s\n", Path);
374 Texture->
Width = Bitmap.InfoHeader.Width;
375 Texture->
Height = Bitmap.InfoHeader.Height;
378 if(Bitmap.InfoHeader.BitCount == 4)
384 Texture->
Clut = memalign(128, gsKit_texture_size_ee(8, 2,
GS_PSM_CT32));
388 fseek(File, 54, SEEK_SET);
389 if (fread(Texture->
Clut, 1, Bitmap.InfoHeader.ColorUsed*
sizeof(u32), File) != Bitmap.InfoHeader.ColorUsed*
sizeof(u32))
393 Texture->
Clut = NULL;
395 printf(
"BMP: Could not load bitmap: %s\n", Path);
400 clut = (GSBMCLUT *)Texture->
Clut;
401 for (i = Bitmap.InfoHeader.ColorUsed; i < 16; i++)
403 memset(&clut[i], 0,
sizeof(clut[i]));
406 for (i = 0; i < 16; i++)
408 u8 tmp = clut[i].Blue;
409 clut[i].Blue = clut[i].Red;
411 clut[i].Alpha = 0x80;
415 else if(Bitmap.InfoHeader.BitCount == 8)
421 Texture->
Clut = memalign(128, gsKit_texture_size_ee(16, 16,
GS_PSM_CT32));
425 fseek(File, 54, SEEK_SET);
426 if (fread(Texture->
Clut, Bitmap.InfoHeader.ColorUsed*
sizeof(u32), 1, File) <= 0)
430 Texture->
Clut = NULL;
432 printf(
"BMP: Could not load bitmap: %s\n", Path);
437 clut = (GSBMCLUT *)Texture->
Clut;
438 for (i = Bitmap.InfoHeader.ColorUsed; i < 256; i++)
440 memset(&clut[i], 0,
sizeof(clut[i]));
443 for (i = 0; i < 256; i++)
445 u8 tmp = clut[i].Blue;
446 clut[i].Blue = clut[i].Red;
448 clut[i].Alpha = 0x80;
452 for (i = 0; i < 256; i++)
456 GSBMCLUT tmp = clut[i];
462 else if(Bitmap.InfoHeader.BitCount == 16)
466 Texture->
Clut = NULL;
468 else if(Bitmap.InfoHeader.BitCount == 24)
472 Texture->
Clut = NULL;
475 fseek(File, 0, SEEK_END);
476 FTexSize = ftell(File);
477 FTexSize -= Bitmap.FileHeader.Offset;
479 fseek(File, Bitmap.FileHeader.Offset, SEEK_SET);
481 TextureSize = gsKit_texture_size_ee(Texture->
Width, Texture->
Height, Texture->
PSM);
483 Texture->
Mem = memalign(128,TextureSize);
485 if(Bitmap.InfoHeader.BitCount == 24)
487 image = memalign(128, FTexSize);
489 printf(
"BMP: Failed to allocate memory\n");
496 Texture->
Clut = NULL;
502 fread(image, FTexSize, 1, File);
503 p = (
void *)((u32)Texture->
Mem);
504 for (y = Texture->
Height - 1, cy = 0; y >= 0; y--, cy++) {
505 for (x = 0; x < Texture->
Width; x++) {
506 p[(y * Texture->
Width + x) * 3 + 2] = image[(cy * Texture->
Width + x) * 3 + 0];
507 p[(y * Texture->
Width + x) * 3 + 1] = image[(cy * Texture->
Width + x) * 3 + 1];
508 p[(y * Texture->
Width + x) * 3 + 0] = image[(cy * Texture->
Width + x) * 3 + 2];
514 else if(Bitmap.InfoHeader.BitCount == 16)
516 image = memalign(128, FTexSize);
518 printf(
"BMP: Failed to allocate memory\n");
525 Texture->
Clut = NULL;
531 fread(image, FTexSize, 1, File);
533 p = (
void *)((u32)Texture->
Mem);
534 for (y = Texture->
Height - 1, cy = 0; y >= 0; y--, cy++) {
535 for (x = 0; x < Texture->
Width; x++) {
537 value = *(u16*)&image[(cy * Texture->
Width + x) * 2];
538 value = (value & 0x8000) | value << 10 | (value & 0x3E0) | (value & 0x7C00) >> 10;
540 *(u16*)&p[(y * Texture->
Width + x) * 2] = value;
546 else if(Bitmap.InfoHeader.BitCount == 8 || Bitmap.InfoHeader.BitCount == 4)
548 char *tex = (
char *)((u32)Texture->
Mem);
549 image = memalign(128,FTexSize);
551 printf(
"BMP: Failed to allocate memory\n");
558 Texture->
Clut = NULL;
564 if (fread(image, FTexSize, 1, File) != 1)
572 Texture->
Clut = NULL;
574 printf(
"BMP: Read failed!, Size %d\n", FTexSize);
580 for (y = Texture->
Height - 1; y >= 0; y--)
582 if(Bitmap.InfoHeader.BitCount == 8)
583 memcpy(&tex[y * Texture->
Width], &image[(Texture->
Height - y - 1) * Texture->
Width], Texture->
Width);
585 memcpy(&tex[y * (Texture->
Width / 2)], &image[(Texture->
Height - y - 1) * (Texture->
Width / 2)], Texture->
Width / 2);
590 if(Bitmap.InfoHeader.BitCount == 4)
593 u8 *tmpdst = (u8 *)((u32)Texture->
Mem);
594 u8 *tmpsrc = (u8 *)tex;
596 for(
byte = 0;
byte < FTexSize;
byte++)
598 tmpdst[byte] = (tmpsrc[byte] << 4) | (tmpsrc[
byte] >> 4);
604 printf(
"BMP: Unknown bit depth format %d\n", Bitmap.InfoHeader.BitCount);
609 return gsKit_texture_finish(
gsGlobal, Texture);
614 struct jpeg_error_mgr pub;
616 jmp_buf setjmp_buffer;
622my_error_exit(j_common_ptr cinfo)
625 my_error_ptr myerr = (my_error_ptr)cinfo->err;
629 (*cinfo->err->output_message) (cinfo);
632 longjmp(myerr->setjmp_buffer, 1);
636#define MAX_TEXTURE 1024
637static void _ps2_load_JPEG_generic(GSTEXTURE *Texture,
struct jpeg_decompress_struct *cinfo,
struct my_error_mgr *jerr,
bool scale_down)
641 unsigned int longer = cinfo->image_width > cinfo->image_height ? cinfo->image_width : cinfo->image_height;
642 float downScale = (float)longer / (
float)MAX_TEXTURE;
643 cinfo->scale_denom = ceil(downScale);
646 jpeg_start_decompress(cinfo);
650 Texture->
Width = cinfo->output_width;
651 Texture->
Height = cinfo->output_height;
655 Texture->
Clut = NULL;
657 textureSize = cinfo->output_width*cinfo->output_height*cinfo->out_color_components;
659 printf(
"Texture Size = %i\n",textureSize);
661 Texture->
Mem = memalign(128, textureSize);
663 unsigned int row_stride = textureSize/Texture->
Height;
664 unsigned char *row_pointer = (
unsigned char *)Texture->
Mem;
665 while (cinfo->output_scanline < cinfo->output_height) {
666 jpeg_read_scanlines(cinfo, (JSAMPARRAY)&row_pointer, 1);
667 row_pointer += row_stride;
670 jpeg_finish_decompress(cinfo);
675int gsKit_texture_jpeg_scale(GSGLOBAL *
gsGlobal, GSTEXTURE *Texture,
char *Path,
bool scale_down)
679 struct jpeg_decompress_struct cinfo;
682 if (Texture == NULL) {
683 printf(
"jpeg: error Texture is NULL\n");
687 if ((fp = fopen(Path,
"rb")) <= 0) {
688 printf(
"jpeg: error opening FILE\n");
693 cinfo.err = jpeg_std_error(&jerr.pub);
694 jerr.pub.error_exit = my_error_exit;
696 if (setjmp(jerr.setjmp_buffer)) {
700 jpeg_destroy_decompress(&cinfo);
704 printf(
"jpeg: error during processing file\n");
707 jpeg_create_decompress(&cinfo);
708 jpeg_stdio_src(&cinfo, fp);
709 jpeg_read_header(&cinfo, TRUE);
711 _ps2_load_JPEG_generic(Texture, &cinfo, &jerr, scale_down);
713 jpeg_destroy_decompress(&cinfo);
717 printf(
"jpeg: File image readed, allocating VRAM\n");
719 return gsKit_texture_finish(
gsGlobal, Texture);
721 printf(
"ERROR: gsKit_texture_jpeg unimplemented.\n");
726int gsKit_texture_jpeg(GSGLOBAL *
gsGlobal, GSTEXTURE *Texture,
char *Path) {
727 return gsKit_texture_jpeg_scale(
gsGlobal, Texture, Path,
false);
730int gsKit_texture_tiff(GSGLOBAL *
gsGlobal, GSTEXTURE *Texture,
char *Path)
735 TIFF* tif = TIFFOpen(Path,
"r");
738 printf(
"Error Opening %s\n", Path);
745 Texture->
Clut = NULL;
747 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Texture->
Width);
748 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Texture->
Height);
749 TextureSize = gsKit_texture_size_ee(Texture->
Width, Texture->
Height, Texture->
PSM);
752 printf(
"Texture Size = %i\n",TextureSize);
755 Texture->
Mem = memalign(128,TextureSize);
757 if (!TIFFReadRGBAImage(tif, Texture->
Width, Texture->
Height, (uint32_t *)Texture->
Mem, 0))
759 printf(
"Error Reading TIFF Data\n");
776 return gsKit_texture_finish(
gsGlobal, Texture);
778 printf(
"ERROR: gsKit_texture_tiff unimplimented.\n");
783int gsKit_texture_raw(GSGLOBAL *
gsGlobal, GSTEXTURE *Texture,
char *Path)
788 File = fopen(Path,
"rb");
791 printf(
"Failed to load texture: %s\n", Path);
794 FileSize = gsKit_texture_size_ee(Texture->
Width, Texture->
Height, Texture->
PSM);
795 Texture->
Mem = memalign(128, FileSize);
800 Texture->
Clut = NULL;
803 if(fread(Texture->
Mem, 1, FileSize, File) != FileSize)
805 printf(
"Texture might be bad: %s\n", Path);
806 printf(
"Texture size: %d\n", FileSize);
810 return gsKit_texture_finish(
gsGlobal, Texture);
813int gsKit_texture_tga(GSGLOBAL *
gsGlobal, GSTEXTURE *Texture,
char *Path)
815 printf(
"ERROR: gsKit_texture_tga unimplimented.\n");
848 printf(
"VRAM Allocation Failed. Will not upload texture.\n");
858 for (i=0; i<size/4; i++) {
859 if (mem[i] == 0xFF00FFFF) {
862 u32 c = (mem[i] & 0x00FF0000) >> 16;
863 mem[i] = 0x80000000 | (c) | (c<<8) | (c<<16);
972 GSFONT *
gsFont = calloc(1,
sizeof(GSFONT));
996 printf(
"Error initializing .dat\n");
1002 strcpy(tmp,
".dat");
1014GSFONT *gsKit_init_font_raw(u8 type, u8 *data,
int size)
1017 GSFONT *
gsFont = calloc(1,
sizeof(GSFONT));
1035 printf(
"Error uploading font!\n");
1038 for (i=0; i<256; i++) {
1060 printf(
"Error uploading font!\n");
1064 for (i=0; i<256; i++) {
1070 else if( (
gsFont->
Type == GSKIT_FTYPE_PNG_DAT) ||
1077 printf(
"Error uploading font png!\n");
1084 printf(
"Error uploading font bmp!\n");
1097 fseek(File, 0, SEEK_SET);
1098 for(i=0; i<256; i++) {
1111 for (i=0; i<256; i++) {
1120 printf(
"Error uploading font!\n");
1125void gsKit_font_print_scaled(GSGLOBAL *
gsGlobal, GSFONT *
gsFont,
float X,
float Y,
int Z,
1126 float scale,
unsigned long color,
const char *String)
1169 cx+=(charsiz*scale)+1;
1200 unsigned char c=String[i];
1219 cx+=(charsiz*scale)+1;
#define GS_ATEST_OFF
Turns off Alpha Testing (Source).
#define GS_ATEST_ON
Turns on Alpha Testing (Source).
u32 gsKit_vram_alloc(GSGLOBAL *gsGlobal, u32 size, u8 type)
GS VRAM Allocation.
#define GS_FILTER_NEAREST
Use simple filtering (nearest neighbor).
#define GSKIT_ALLOC_USERBUFFER
GS VRAM "User" Allocation (Texture, CLUT, etc).
void gsKit_set_primalpha(GSGLOBAL *gsGlobal, u64 AlphaMode, u8 PerPixel)
Sets the Alpha Blending Parameters.
#define GSKIT_ALLOC_ERROR
Return this is VRAM allocation fails.
void gsKit_set_test(GSGLOBAL *gsGlobal, u8 Preset)
#define GS_PSM_CT32
R8 G8 B8 A8 (RGBA32) Texture.
#define GS_PSM_T8
8 Bit Texture with CLUT
#define GS_PSM_T4
4 Bit Texture with CLUT
#define GS_SETREG_ALPHA(A, B, C, D, FIX)
GS ALPHA Packing Macro.
#define GS_PSM_CT24
R8 G8 B8 (RGB24) Texture.
#define GS_PSM_CT16
RGBA16 Texture.
u8 * RawData
Raw File Data.
char * Path_DAT
Path (string) to the Glyph DAT File.
char * Path
Path (string) to the Font File.
u32 VChars
Character Columns.
int RawSize
Raw File Datasize.
GSTEXTURE * Texture
Font Texture Object.
u32 HChars
Character Rows.
s16 * Additional
Additional Font Data.
u32 CharHeight
Character Height.
u32 CharWidth
Character Width.
GSTEST * Test
TEST Register Value Structure Pointer.
u64 PrimAlpha
Primitive Alpha Value.
u8 PABE
Choose if to do Alpha Blend on a Per-Pixel Basis.
u32 * Clut
EE CLUT Memory Pointer.
u32 * Mem
EE Memory Pointer.
u32 Vram
GS VRAM Memory Pointer.
u32 VramClut
GS VRAM CLUT Memory Pointer.
u8 Delayed
Delay Texture Upload To VRAM.
u8 ClutPSM
CLUT Pixel Storage Method (Color Format).
u8 PSM
Pixel Storage Method (Color Format).
u32 Height
Height of the Texture.
u32 Width
Width of the Texture.
u32 Filter
NEAREST or LINEAR.