fork download
  1. #include "c_common.h"
  2.  
  3. unsigned char linewrk[2048*4];
  4.  
  5. png_structp png_ptr;
  6. png_infop info_ptr;
  7. FILE *fp;
  8.  
  9. int getNextPower2(int width)
  10. {
  11. int b = width;
  12. int n;
  13. for (n = 0; b != 0; n++) b >>= 1;
  14. b = 1 << n;
  15. if (b == 2 * width) b >>= 1;
  16. return b;
  17. }
  18.  
  19. void user_warning_fn(png_structp png_ptr, png_const_charp warning_msg)
  20. {
  21. }
  22.  
  23. void PngRamDiskRead(png_struct *png_ptr, png_bytep buf, png_size_t size)
  24. {
  25. /*PngFileInfo *fileinfo = (PngFileInfo *)png_get_io_ptr(png_ptr);
  26. if (fileinfo->offset + size <= fileinfo->len) {
  27. memcpy(buf, fileinfo->data + fileinfo->offset, size);
  28. fileinfo->offset += size;
  29. } else {
  30. png_error(png_ptr,"png_mem_read_func failed");
  31. }*/
  32. unsigned char** data = (unsigned char **)png_get_io_ptr(png_ptr);
  33. memcpy(buf, *data, size);
  34. *data += (int)size;
  35. return;
  36. }
  37.  
  38. int PgPngRead(Image* image,int mode,unsigned int FileNo)
  39. {
  40. unsigned int sig_read = 0;
  41. png_uint_32 width;
  42. png_uint_32 height;
  43. int bit_depth;
  44. int color_type;
  45. int interlace_type;
  46. int x;
  47. int y;
  48. png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  49. if (png_ptr == NULL) return -102;
  50.  
  51. png_set_error_fn(png_ptr, (png_voidp) NULL, (png_error_ptr) NULL, user_warning_fn);
  52. info_ptr = png_create_info_struct(png_ptr);
  53. if (info_ptr == NULL) return -103;
  54. if (mode == 0) {
  55. png_init_io(png_ptr, fp);
  56. png_set_sig_bytes(png_ptr, sig_read);
  57. } else {
  58. unsigned char* fileinfo = (unsigned char*)PgRamDataGet(FileNo);
  59. png_set_read_fn(png_ptr,(png_voidp)&fileinfo,(png_rw_ptr)PngRamDiskRead);
  60. }
  61. png_read_info(png_ptr, info_ptr);
  62. png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, int_p_NULL, int_p_NULL);
  63. if (width > 2048 || height > 2048) return -104;
  64.  
  65. image->imageWidth = width;
  66. image->imageHeight = height;
  67. if (width > 512) {
  68. image->textureWidth = width;
  69. } else {
  70. image->textureWidth = getNextPower2(width);
  71. }
  72. if (height > 512 ) {
  73. image->textureHeight = height;
  74. } else {
  75. image->textureHeight = getNextPower2(height);
  76. }
  77. image->ColorType = color_type;
  78. if (color_type == PNG_COLOR_TYPE_PALETTE) {
  79. image->BitDepth = bit_depth;
  80. png_colorp palette = NULL;
  81. int num_palette = 0;
  82. png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
  83. int i;
  84. image->ClutPlteCount = num_palette;
  85. int wcolor;
  86. for(i=0; i<num_palette; i++, palette++){
  87. wcolor = palette->red;
  88. wcolor = (wcolor << 8) | palette->green;
  89. wcolor = (wcolor << 8) | palette->blue;
  90. image->ClutPlte[i] = (0xff000000 | wcolor);
  91. }
  92. } else {
  93. image->BitDepth = bit_depth;
  94. image->ClutPlteCount = 0;
  95. }
  96. int w_size = png_get_rowbytes(png_ptr, info_ptr);
  97. image->MemType = PG_MEMTYPE_SYSTEM;
  98. image->TexType = 0;
  99.  
  100. switch(bit_depth) {
  101. case 4:
  102. {
  103. image->data = malloc(image->textureWidth*image->textureHeight/2);
  104. if (!image->data) return -105;
  105. char *line = (char*)&linewrk;
  106. char color;
  107. for (y = 0; y < height; y++) {
  108. png_read_row(png_ptr, (u8*) line, png_bytep_NULL);
  109. for (x = 0; x < image->textureWidth/2 ; x++) {
  110. color = line[x];
  111. image->data[x + y * image->textureWidth/2] = ( (color >> 4) | (color << 4) );
  112. }
  113. }
  114. }
  115. break;
  116. case 8:
  117. {
  118. image->data = malloc(image->textureWidth*image->textureHeight);
  119. if (!image->data) return -105;
  120. char *line = (char*)&linewrk;
  121. char color;
  122. for (y = 0; y < height; y++) {
  123. png_read_row(png_ptr, (u8*) line, png_bytep_NULL);
  124. for (x = 0; x < w_size; x++) {
  125. color = line[x];
  126. image->data[x + y * w_size] = color;
  127. }
  128. }
  129. }
  130. break;
  131. case 16:
  132. {
  133. u16 *line = (u16*)&linewrk;
  134. u16 color;
  135. for (y = 0; y < height; y++) {
  136. png_read_row(png_ptr, (u8*) line, png_bytep_NULL);
  137. for (x = 0; x < w_size; x++) {
  138. color = line[x];
  139. image->data[x + y * w_size] = color;
  140. }
  141. }
  142. }
  143. break;
  144. case 32:
  145. {
  146. u32 *line = (u32*)&linewrk;
  147. u32 color;
  148. for (y = 0; y < height; y++) {
  149. png_read_row(png_ptr, (u8*) line, png_bytep_NULL);
  150. for (x = 0; x < w_size; x++) {
  151. color = line[x];
  152. image->data[x + y * w_size] = color;
  153. }
  154. }
  155. }
  156. break;
  157. default:
  158. return -106;
  159. break;
  160. }
  161. png_read_end(png_ptr, info_ptr);
  162. return 0;
  163. }
  164.  
  165. int PgPngLoadRam(unsigned int FileNo,Image* image)
  166. {
  167. int ret = 0;
  168.  
  169. if (FileNo > (RAMFILEMAX - 1)) return -100;
  170. if (PgRamDataChk(FileNo) == -1) return -101;
  171.  
  172. ret = PgPngRead(image,1,FileNo);
  173. if (ret == -102) return ret;
  174. if (ret == -103) {
  175. png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
  176. } else {
  177. png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
  178. }
  179. return ret;
  180. }
  181.  
  182. int PgPngLoadDisk(const char* filename,Image* image)
  183. {
  184. int ret = 0;
  185.  
  186. if ((fp = fopen(filename, "rb")) == NULL) return -101;
  187.  
  188. ret = PgPngRead(image,0,0);
  189. if (ret == -102) return ret;
  190. if (ret == -103) {
  191. png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
  192. } else {
  193. png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
  194. }
  195. fclose(fp);
  196. return ret;
  197. }
  198.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:1:10: fatal error: c_common.h: No such file or directory
 #include "c_common.h"
          ^~~~~~~~~~~~
compilation terminated.
stdout
Standard output is empty