readpng.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <png.h>
  4. #include "readpng.h"
  5. #include "lprintf.h"
  6. #ifdef PSP
  7. #define BG_WIDTH 480
  8. #define BG_HEIGHT 272
  9. #else
  10. #define BG_WIDTH 320
  11. #define BG_HEIGHT 240
  12. #endif
  13. int readpng(void *dest, const char *fname, readpng_what what)
  14. {
  15. FILE *fp;
  16. png_structp png_ptr = NULL;
  17. png_infop info_ptr = NULL;
  18. png_bytepp row_ptr = NULL;
  19. int ret = -1;
  20. if (dest == NULL || fname == NULL)
  21. {
  22. return -1;
  23. }
  24. fp = fopen(fname, "rb");
  25. if (fp == NULL)
  26. {
  27. lprintf(__FILE__ ": failed to open: %s\n", fname);
  28. return -1;
  29. }
  30. png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  31. if (!png_ptr)
  32. {
  33. lprintf(__FILE__ ": png_create_read_struct() failed\n");
  34. fclose(fp);
  35. return -1;
  36. }
  37. info_ptr = png_create_info_struct(png_ptr);
  38. if (!info_ptr)
  39. {
  40. lprintf(__FILE__ ": png_create_info_struct() failed\n");
  41. goto done;
  42. }
  43. // Start reading
  44. png_init_io(png_ptr, fp);
  45. png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_PACKING, NULL);
  46. row_ptr = png_get_rows(png_ptr, info_ptr);
  47. if (row_ptr == NULL)
  48. {
  49. lprintf(__FILE__ ": png_get_rows() failed\n");
  50. goto done;
  51. }
  52. // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)info_ptr->width, (int)info_ptr->height, info_ptr->pixel_depth);
  53. switch (what)
  54. {
  55. case READPNG_BG:
  56. {
  57. int height, width, h;
  58. unsigned short *dst = dest;
  59. if (info_ptr->pixel_depth != 24)
  60. {
  61. lprintf(__FILE__ ": bg image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
  62. break;
  63. }
  64. height = info_ptr->height;
  65. if (height > BG_HEIGHT) height = BG_HEIGHT;
  66. width = info_ptr->width;
  67. if (width > BG_WIDTH) width = BG_WIDTH;
  68. for (h = 0; h < height; h++)
  69. {
  70. unsigned char *src = row_ptr[h];
  71. int len = width;
  72. while (len--)
  73. {
  74. #ifdef PSP
  75. *dst++ = ((src[2]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[0] >> 3); // BGR
  76. #else
  77. *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3); // RGB
  78. #endif
  79. src += 3;
  80. }
  81. dst += BG_WIDTH - width;
  82. }
  83. break;
  84. }
  85. case READPNG_FONT:
  86. {
  87. int x, y, x1, y1;
  88. unsigned char *dst = dest;
  89. if (info_ptr->width != 128 || info_ptr->height != 160)
  90. {
  91. lprintf(__FILE__ ": unexpected font image size %ix%i, needed 128x160\n",
  92. (int)info_ptr->width, (int)info_ptr->height);
  93. break;
  94. }
  95. if (info_ptr->pixel_depth != 8)
  96. {
  97. lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
  98. break;
  99. }
  100. for (y = 0; y < 16; y++)
  101. {
  102. for (x = 0; x < 16; x++)
  103. {
  104. for (y1 = 0; y1 < 10; y1++)
  105. {
  106. unsigned char *src = row_ptr[y*10 + y1] + x*8;
  107. for (x1 = 8/2; x1 > 0; x1--, src+=2)
  108. *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
  109. }
  110. }
  111. }
  112. break;
  113. }
  114. case READPNG_SELECTOR:
  115. {
  116. int x1, y1;
  117. unsigned char *dst = dest;
  118. if (info_ptr->width != 8 || info_ptr->height != 10)
  119. {
  120. lprintf(__FILE__ ": unexpected selector image size %ix%i, needed 8x10\n",
  121. (int)info_ptr->width, (int)info_ptr->height);
  122. break;
  123. }
  124. if (info_ptr->pixel_depth != 8)
  125. {
  126. lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
  127. break;
  128. }
  129. for (y1 = 0; y1 < 10; y1++)
  130. {
  131. unsigned char *src = row_ptr[y1];
  132. for (x1 = 8/2; x1 > 0; x1--, src+=2)
  133. *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
  134. }
  135. break;
  136. }
  137. case READPNG_320_24:
  138. case READPNG_480_24:
  139. {
  140. int height, width, h;
  141. int needw = (what == READPNG_480_24) ? 480 : 320;
  142. unsigned char *dst = dest;
  143. if (info_ptr->pixel_depth != 24)
  144. {
  145. lprintf(__FILE__ ": image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
  146. break;
  147. }
  148. height = info_ptr->height;
  149. if (height > 240) height = 240;
  150. width = info_ptr->width;
  151. if (width > needw) width = needw;
  152. for (h = 0; h < height; h++)
  153. {
  154. int len = width;
  155. unsigned char *src = row_ptr[h];
  156. dst += (needw - width) * 3;
  157. for (len = width; len > 0; len--, dst+=3, src+=3)
  158. dst[0] = src[2], dst[1] = src[1], dst[2] = src[0];
  159. }
  160. break;
  161. }
  162. }
  163. ret = 0;
  164. done:
  165. png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);
  166. fclose(fp);
  167. return ret;
  168. }