5 * Copyright (C) 2007 Hiroshi Morii All Rights Reserved.
6 * Email koolsmoky(at)users.sourceforge.net
7 * Web http://www.3dfxzone.it/koolsmoky
9 * this is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * this is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with GNU Make; see the file COPYING. If not, write to
21 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* use power of 2 texture size
25 * (0:disable, 1:enable, 2:3dfx) */
26 #define POW2_TEXTURES 0
28 /* check 8 bytes. use a larger value if needed. */
29 #define PNG_CHK_BYTES 8
32 #include "TxReSample.h"
35 #include "../Glide64/Gfx_1.3.h"
38 TxImage::getPNGInfo(FILE *fp, png_structp *png_ptr, png_infop *info_ptr)
40 unsigned char sig[PNG_CHK_BYTES];
42 /* check for valid file pointer */
46 /* check if file is PNG */
47 if (fread(sig, 1, PNG_CHK_BYTES, fp) != PNG_CHK_BYTES)
50 if (png_sig_cmp(sig, 0, PNG_CHK_BYTES) != 0)
53 /* get PNG file info */
54 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
58 *info_ptr = png_create_info_struct(*png_ptr);
60 png_destroy_read_struct(png_ptr, NULL, NULL);
64 if (setjmp(png_jmpbuf(*png_ptr))) {
65 DBG_INFO(80, L"error reading png!\n");
66 png_destroy_read_struct(png_ptr, info_ptr, NULL);
70 png_init_io(*png_ptr, fp);
71 png_set_sig_bytes(*png_ptr, PNG_CHK_BYTES);
72 png_read_info(*png_ptr, *info_ptr);
78 TxImage::readPNG(FILE* fp, int* width, int* height, uint16* format)
80 /* NOTE: returned image format is GR_TEXFMT_ARGB_8888 */
85 int bit_depth, color_type, interlace_type, compression_type, filter_type,
86 row_bytes, o_width, o_height, num_pas;
93 /* check if we have a valid png file */
97 if (!getPNGInfo(fp, &png_ptr, &info_ptr)) {
98 INFO(80, L"error reading png file! png image is corrupt.\n");
102 png_get_IHDR(png_ptr, info_ptr,
103 (png_uint_32*)&o_width, (png_uint_32*)&o_height, &bit_depth, &color_type,
104 &interlace_type, &compression_type, &filter_type);
106 DBG_INFO(80, L"png format %d x %d bitdepth:%d color:%x interlace:%x compression:%x filter:%x\n",
107 o_width, o_height, bit_depth, color_type,
108 interlace_type, compression_type, filter_type);
110 /* transformations */
112 /* Rice hi-res textures
119 /* strip if color channel is larger than 8 bits */
121 png_set_strip_16(png_ptr);
126 /* These are not really required per Rice format spec,
127 * but is done just in case someone uses them.
129 /* convert palette color to rgb color */
130 if (color_type == PNG_COLOR_TYPE_PALETTE) {
131 png_set_palette_to_rgb(png_ptr);
132 color_type = PNG_COLOR_TYPE_RGB;
135 /* expand 1,2,4 bit gray scale to 8 bit gray scale */
136 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
137 png_set_expand_gray_1_2_4_to_8(png_ptr);
139 /* convert gray scale or gray scale + alpha to rgb color */
140 if (color_type == PNG_COLOR_TYPE_GRAY ||
141 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
142 png_set_gray_to_rgb(png_ptr);
143 color_type = PNG_COLOR_TYPE_RGB;
147 /* add alpha channel if any */
148 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
149 png_set_tRNS_to_alpha(png_ptr);
150 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
153 /* convert rgb to rgba */
154 if (color_type == PNG_COLOR_TYPE_RGB) {
155 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
156 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
159 /* punt invalid formats */
160 if (color_type != PNG_COLOR_TYPE_RGB_ALPHA) {
161 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
162 DBG_INFO(80, L"Error: not PNG_COLOR_TYPE_RGB_ALPHA format!\n");
166 /*png_color_8p sig_bit;
167 if (png_get_sBIT(png_ptr, info_ptr, &sig_bit))
168 png_set_shift(png_ptr, sig_bit);*/
170 /* convert rgba to bgra */
171 png_set_bgr(png_ptr);
173 /* turn on interlace handling to cope with the weirdness
174 * of texture authors using interlaced format */
175 num_pas = png_set_interlace_handling(png_ptr);
177 /* update info structure */
178 png_read_update_info(png_ptr, info_ptr);
180 /* we only get here if ARGB8888 */
181 row_bytes = png_get_rowbytes(png_ptr, info_ptr);
183 /* allocate memory to read in image */
184 image = (uint8*)malloc(row_bytes * o_height);
191 for (pas = 0; pas < num_pas; pas++) { /* deal with interlacing */
194 for (i = 0; i < o_height; i++) {
196 png_read_rows(png_ptr, &tmpimage, NULL, 1);
197 tmpimage += row_bytes;
201 /* read rest of the info structure */
202 png_read_end(png_ptr, info_ptr);
204 *width = (row_bytes >> 2);
206 *format = GR_TEXFMT_ARGB_8888;
209 /* next power of 2 size conversions */
210 /* NOTE: I can do this in the above loop for faster operations, but some
211 * texture packs require a workaround. see HACKALERT in nextPow2().
214 TxReSample txReSample = new TxReSample; // XXX: temporary. move to a better place.
216 #if (POW2_TEXTURES == 2)
217 if (!txReSample->nextPow2(&image, width, height, 32, 1)) {
219 if (!txReSample->nextPow2(&image, width, height, 32, 0)) {
232 #endif /* POW2_TEXTURES */
236 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
240 DBG_INFO(80, L"Error: failed to load png image!\n");
248 TxImage::writePNG(uint8* src, FILE* fp, int width, int height, int rowStride, uint16 format, uint8 *palette)
253 png_colorp palette_ptr = NULL;
254 png_bytep trans_ptr = NULL;
255 int bit_depth, color_type, row_bytes, num_palette;
257 //uint16 srcfmt, destfmt;
262 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
266 info_ptr = png_create_info_struct(png_ptr);
267 if (info_ptr == NULL) {
268 png_destroy_write_struct(&png_ptr, NULL);
272 if (png_jmpbuf(png_ptr)) {
273 png_destroy_write_struct(&png_ptr, &info_ptr);
277 png_init_io(png_ptr, fp);
279 /* TODO: images must be converted to RGBA8888 or CI8,
280 * palettes need to be separated to A and RGB. */
283 * Format: 0 - RGBA, 1 - YUV, 2 - CI, 3 - IA, 4 - I
284 * Size: 0 - 4bit, 1 - 8bit, 2 - 16bit, 3 - 32 bit
285 * format = (Format << 8 | Size);
288 /* each channel is saved in 8bits for consistency */
290 case 0x0002:/* RGBA5551 */
296 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
298 case 0x0003:/* RGBA8888 */
299 case 0x0302:/* IA88 */
305 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
307 case 0x0300:/* IA31 */
313 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
315 case 0x0301:/* IA44 */
321 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
328 color_type = PNG_COLOR_TYPE_RGB;
331 case 0x0402:/* I16 */
336 color_type = PNG_COLOR_TYPE_RGB;
338 case 0x0200:/* CI4 */
341 color_type = PNG_COLOR_TYPE_PALETTE;
343 case 0x0201:/* CI8 */
346 color_type = PNG_COLOR_TYPE_PALETTE;
348 case 0x0102:/* YUV ? */
351 /* unsupported format */
352 png_destroy_write_struct(&png_ptr, &info_ptr);
356 switch (color_type) {
357 case PNG_COLOR_TYPE_RGB_ALPHA:
358 case PNG_COLOR_TYPE_RGB:
359 //row_bytes = (bit_depth * width) >> 1;
360 row_bytes = rowStride;
361 png_set_bgr(png_ptr);
362 png_set_sBIT(png_ptr, info_ptr, &sig_bit);
364 case PNG_COLOR_TYPE_PALETTE:
365 //row_bytes = (bit_depth * width) >> 3;
366 row_bytes = rowStride;
367 png_set_PLTE(png_ptr, info_ptr, palette_ptr, num_palette);
368 png_set_tRNS(png_ptr, info_ptr, trans_ptr, num_palette, 0);
371 //png_set_filter(png_ptr, 0, PNG_ALL_FILTERS);
373 //if (bit_depth == 16)
374 // png_set_swap(png_ptr);
377 // png_set_packswap(png_ptr);
379 png_set_IHDR(png_ptr, info_ptr, width, height,
380 bit_depth, color_type, PNG_INTERLACE_NONE,
381 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
383 //png_set_gAMA(png_ptr, info_ptr, 1.0);
385 png_write_info(png_ptr, info_ptr);
386 for (i = 0; i < height; i++) {
387 png_write_row(png_ptr, (png_bytep)src);
390 png_write_end(png_ptr, info_ptr);
392 png_destroy_write_struct(&png_ptr, &info_ptr);
394 //if (tex_ptr) delete [] tex_ptr;
400 TxImage::getBMPInfo(FILE* fp, BITMAPFILEHEADER* bmp_fhdr, BITMAPINFOHEADER* bmp_ihdr)
403 * read in BITMAPFILEHEADER
406 /* is this a BMP file? */
407 if (fread(&bmp_fhdr->bfType, 2, 1, fp) != 1)
410 if (memcmp(&bmp_fhdr->bfType, "BM", 2) != 0)
414 if (fread(&bmp_fhdr->bfSize, 4, 1, fp) != 1)
418 if (fread(&bmp_fhdr->bfReserved1, 2, 1, fp) != 1)
422 if (fread(&bmp_fhdr->bfReserved2, 2, 1, fp) != 1)
425 /* offset to the image data */
426 if (fread(&bmp_fhdr->bfOffBits, 4, 1, fp) != 1)
430 * read in BITMAPINFOHEADER
433 /* size of BITMAPINFOHEADER */
434 if (fread(&bmp_ihdr->biSize, 4, 1, fp) != 1)
437 /* is this a Windows BMP? */
438 if (bmp_ihdr->biSize != 40)
441 /* width of the bitmap in pixels */
442 if (fread(&bmp_ihdr->biWidth, 4, 1, fp) != 1)
445 /* height of the bitmap in pixels */
446 if (fread(&bmp_ihdr->biHeight, 4, 1, fp) != 1)
449 /* number of planes (always 1) */
450 if (fread(&bmp_ihdr->biPlanes, 2, 1, fp) != 1)
453 /* number of bits-per-pixel. (1, 4, 8, 16, 24, 32) */
454 if (fread(&bmp_ihdr->biBitCount, 2, 1, fp) != 1)
457 /* compression for a compressed bottom-up bitmap
458 * 0 : uncompressed format
459 * 1 : run-length encoded 4 bpp format
460 * 2 : run-length encoded 8 bpp format
463 if (fread(&bmp_ihdr->biCompression, 4, 1, fp) != 1)
466 /* size of the image in bytes */
467 if (fread(&bmp_ihdr->biSizeImage, 4, 1, fp) != 1)
470 /* horizontal resolution in pixels-per-meter */
471 if (fread(&bmp_ihdr->biXPelsPerMeter, 4, 1, fp) != 1)
474 /* vertical resolution in pixels-per-meter */
475 if (fread(&bmp_ihdr->biYPelsPerMeter, 4, 1, fp) != 1)
478 /* number of color indexes in the color table that are actually used */
479 if (fread(&bmp_ihdr->biClrUsed, 4, 1, fp) != 1)
482 /* the number of color indexes that are required for displaying */
483 if (fread(&bmp_ihdr->biClrImportant, 4, 1, fp) != 1)
490 TxImage::readBMP(FILE* fp, int* width, int* height, uint16* format)
492 /* NOTE: returned image format;
493 * 4, 8bit palette bmp -> GR_TEXFMT_P_8
494 * 24, 32bit bmp -> GR_TEXFMT_ARGB_8888
498 uint8 *image_row = NULL;
499 uint8 *tmpimage = NULL;
500 unsigned int row_bytes, pos;
503 BITMAPFILEHEADER bmp_fhdr;
504 BITMAPINFOHEADER bmp_ihdr;
511 /* check if we have a valid bmp file */
515 if (!getBMPInfo(fp, &bmp_fhdr, &bmp_ihdr)) {
516 INFO(80, L"error reading bitmap file! bitmap image is corrupt.\n");
520 DBG_INFO(80, L"bmp format %d x %d bitdepth:%d compression:%x offset:%d\n",
521 bmp_ihdr.biWidth, bmp_ihdr.biHeight, bmp_ihdr.biBitCount,
522 bmp_ihdr.biCompression, bmp_fhdr.bfOffBits);
524 /* rowStride in bytes */
525 row_bytes = (bmp_ihdr.biWidth * bmp_ihdr.biBitCount) >> 3;
526 /* align to 4bytes boundary */
527 row_bytes = (row_bytes + 3) & ~3;
529 /* Rice hi-res textures */
530 if (!(bmp_ihdr.biBitCount == 8 || bmp_ihdr.biBitCount == 4 || bmp_ihdr.biBitCount == 32 || bmp_ihdr.biBitCount == 24) ||
531 bmp_ihdr.biCompression != 0) {
532 DBG_INFO(80, L"Error: incompatible bitmap format!\n");
536 switch (bmp_ihdr.biBitCount) {
539 /* 8 bit, 32 bit bitmap */
540 image = (uint8*)malloc(row_bytes * bmp_ihdr.biHeight);
543 pos = bmp_fhdr.bfOffBits + row_bytes * (bmp_ihdr.biHeight - 1);
544 for (i = 0; i < bmp_ihdr.biHeight; i++) {
546 fseek(fp, pos, SEEK_SET);
547 if (fread(tmpimage, 1, row_bytes, fp) != row_bytes)
548 ERRLOG("fread() failed for row of '%i' bytes in 8/32-bit BMP image", row_bytes);
549 tmpimage += row_bytes;
556 image = (uint8*)malloc((row_bytes * bmp_ihdr.biHeight) << 1);
557 image_row = (uint8*)malloc(row_bytes);
558 if (image && image_row) {
560 pos = bmp_fhdr.bfOffBits + row_bytes * (bmp_ihdr.biHeight - 1);
561 for (i = 0; i < bmp_ihdr.biHeight; i++) {
563 fseek(fp, pos, SEEK_SET);
564 if (fread(image_row, 1, row_bytes, fp) != row_bytes)
565 ERRLOG("fread failed for row of '%i' bytes in 4-bit BMP image", row_bytes);
566 /* expand 4bpp to 8bpp. stuff 4bit values into 8bit comps. */
567 for (j = 0; j < (int) row_bytes; j++) {
568 tmpimage[j << 1] = image_row[j] & 0x0f;
569 tmpimage[(j << 1) + 1] = (image_row[j] & 0xf0) >> 4;
571 tmpimage += (row_bytes << 1);
576 if (image_row) free(image_row);
577 if (image) free(image);
583 image = (uint8*)malloc((bmp_ihdr.biWidth * bmp_ihdr.biHeight) << 2);
584 image_row = (uint8*)malloc(row_bytes);
585 if (image && image_row) {
587 pos = bmp_fhdr.bfOffBits + row_bytes * (bmp_ihdr.biHeight - 1);
588 for (i = 0; i < bmp_ihdr.biHeight; i++) {
590 fseek(fp, pos, SEEK_SET);
591 if (fread(image_row, 1, row_bytes, fp) != row_bytes)
592 ERRLOG("fread failed for row of '%i' bytes in 24-bit BMP image", row_bytes);
593 /* convert 24bpp to 32bpp. */
594 for (j = 0; j < bmp_ihdr.biWidth; j++) {
595 tmpimage[(j << 2)] = image_row[j * 3];
596 tmpimage[(j << 2) + 1] = image_row[j * 3 + 1];
597 tmpimage[(j << 2) + 2] = image_row[j * 3 + 2];
598 tmpimage[(j << 2) + 3] = 0xFF;
600 tmpimage += (bmp_ihdr.biWidth << 2);
605 if (image_row) free(image_row);
606 if (image) free(image);
612 *width = (row_bytes << 3) / bmp_ihdr.biBitCount;
613 *height = bmp_ihdr.biHeight;
615 switch (bmp_ihdr.biBitCount) {
618 *format = GR_TEXFMT_P_8;
622 *format = GR_TEXFMT_ARGB_8888;
626 /* next power of 2 size conversions */
627 /* NOTE: I can do this in the above loop for faster operations, but some
628 * texture packs require a workaround. see HACKALERT in nextPow2().
631 TxReSample txReSample = new TxReSample; // XXX: temporary. move to a better place.
633 #if (POW2_TEXTURES == 2)
634 if (!txReSample->nextPow2(&image, width, height, 8, 1)) {
636 if (!txReSample->nextPow2(&image, width, height, 8, 0)) {
649 #endif /* POW2_TEXTURES */
654 DBG_INFO(80, L"Error: failed to load bmp image!\n");
662 TxImage::getDDSInfo(FILE *fp, DDSFILEHEADER *dds_fhdr)
665 * read in DDSFILEHEADER
668 /* is this a DDS file? */
669 if (fread(&dds_fhdr->dwMagic, 4, 1, fp) != 1)
672 if (memcmp(&dds_fhdr->dwMagic, "DDS ", 4) != 0)
675 if (fread(&dds_fhdr->dwSize, 4, 1, fp) != 1)
679 if (fread(&dds_fhdr->dwFlags, 4, 1, fp) != 1)
682 /* height of dds in pixels */
683 if (fread(&dds_fhdr->dwHeight, 4, 1, fp) != 1)
686 /* width of dds in pixels */
687 if (fread(&dds_fhdr->dwWidth, 4, 1, fp) != 1)
690 if (fread(&dds_fhdr->dwLinearSize, 4, 1, fp) != 1)
693 if (fread(&dds_fhdr->dwDepth, 4, 1, fp) != 1)
696 if (fread(&dds_fhdr->dwMipMapCount, 4, 1, fp) != 1)
699 if (fread(&dds_fhdr->dwReserved1, 4 * 11, 1, fp) != 1)
702 if (fread(&dds_fhdr->ddpf.dwSize, 4, 1, fp) != 1)
705 if (fread(&dds_fhdr->ddpf.dwFlags, 4, 1, fp) != 1)
708 if (fread(&dds_fhdr->ddpf.dwFourCC, 4, 1, fp) != 1)
711 if (fread(&dds_fhdr->ddpf.dwRGBBitCount, 4, 1, fp) != 1)
714 if (fread(&dds_fhdr->ddpf.dwRBitMask, 4, 1, fp) != 1)
717 if (fread(&dds_fhdr->ddpf.dwGBitMask, 4, 1, fp) != 1)
720 if (fread(&dds_fhdr->ddpf.dwBBitMask, 4, 1, fp) != 1)
723 if (fread(&dds_fhdr->ddpf.dwRGBAlphaBitMask, 4, 1, fp) != 1)
726 if (fread(&dds_fhdr->dwCaps1, 4, 1, fp) != 1)
729 if (fread(&dds_fhdr->dwCaps2, 4, 1, fp) != 1)
736 TxImage::readDDS(FILE* fp, int* width, int* height, uint16* format)
739 DDSFILEHEADER dds_fhdr;
740 uint16 tmpformat = 0;
747 /* check if we have a valid dds file */
751 if (!getDDSInfo(fp, &dds_fhdr)) {
752 INFO(80, L"error reading dds file! dds image is corrupt.\n");
756 DBG_INFO(80, L"dds format %d x %d HeaderSize %d LinearSize %d\n",
757 dds_fhdr.dwWidth, dds_fhdr.dwHeight, dds_fhdr.dwSize, dds_fhdr.dwLinearSize);
759 if (!(dds_fhdr.dwFlags & (DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT|DDSD_PIXELFORMAT|DDSD_LINEARSIZE))) {
760 DBG_INFO(80, L"Error: incompatible dds format!\n");
764 if ((dds_fhdr.dwFlags & DDSD_MIPMAPCOUNT) && dds_fhdr.dwMipMapCount != 1) {
765 DBG_INFO(80, L"Error: mipmapped dds not supported!\n");
769 if (!((dds_fhdr.ddpf.dwFlags & DDPF_FOURCC) && dds_fhdr.dwCaps2 == 0)) {
770 DBG_INFO(80, L"Error: not fourcc standard texture!\n");
774 if (memcmp(&dds_fhdr.ddpf.dwFourCC, "DXT1", 4) == 0) {
775 DBG_INFO(80, L"DXT1 format\n");
776 /* compensate for missing LinearSize */
777 dds_fhdr.dwLinearSize = (dds_fhdr.dwWidth * dds_fhdr.dwHeight) >> 1;
778 tmpformat = GR_TEXFMT_ARGB_CMP_DXT1;
779 } else if (memcmp(&dds_fhdr.ddpf.dwFourCC, "DXT3", 4) == 0) {
780 DBG_INFO(80, L"DXT3 format\n");
781 dds_fhdr.dwLinearSize = dds_fhdr.dwWidth * dds_fhdr.dwHeight;
782 tmpformat = GR_TEXFMT_ARGB_CMP_DXT3;
783 } else if (memcmp(&dds_fhdr.ddpf.dwFourCC, "DXT5", 4) == 0) {
784 DBG_INFO(80, L"DXT5 format\n");
785 dds_fhdr.dwLinearSize = dds_fhdr.dwWidth * dds_fhdr.dwHeight;
786 tmpformat = GR_TEXFMT_ARGB_CMP_DXT5;
788 DBG_INFO(80, L"Error: not DXT1 or DXT3 or DXT5 format!\n");
793 image = (uint8*)malloc(dds_fhdr.dwLinearSize);
795 *width = dds_fhdr.dwWidth;
796 *height = dds_fhdr.dwHeight;
799 fseek(fp, 128, SEEK_SET); /* size of header is 128 bytes */
800 if (fread(image, 1, dds_fhdr.dwLinearSize, fp) != dds_fhdr.dwLinearSize)
801 ERRLOG("fread failed to read DDS image of '%i' bytes", dds_fhdr.dwLinearSize);