metapixel-rwimg.patch 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. diff -u -r metapixel-1.0.2/rwimg/rwgif.c metapixel-1.0.2-rwimg/rwimg/rwgif.c
  2. --- metapixel-1.0.2/rwimg/rwgif.c 2006-12-10 17:59:54.000000000 +0000
  3. +++ metapixel-1.0.2-rwimg/rwimg/rwgif.c 2015-08-27 16:46:56.402621517 +0100
  4. @@ -54,7 +54,7 @@
  5. assert(data != 0);
  6. - data->file = DGifOpenFileName(filename);
  7. + data->file = DGifOpenFileName(filename, NULL);
  8. assert(data->file !=0);
  9. @@ -137,7 +137,7 @@
  10. }
  11. free(buffer);
  12. - assert(DGifCloseFile(data->file) == GIF_OK);
  13. + assert(DGifCloseFile(data->file, NULL) == GIF_OK);
  14. return data;
  15. }
  16. diff -u -r metapixel-1.0.2/rwimg/rwpng.c metapixel-1.0.2-rwimg/rwimg/rwpng.c
  17. --- metapixel-1.0.2/rwimg/rwpng.c 2006-12-10 17:59:54.000000000 +0000
  18. +++ metapixel-1.0.2-rwimg/rwimg/rwpng.c 2015-08-27 16:44:12.337891932 +0100
  19. @@ -35,6 +35,7 @@
  20. png_structp png_ptr;
  21. png_infop info_ptr, end_info;
  22. int row_stride;
  23. + int pixel_stride;
  24. int have_read;
  25. } png_data_t;
  26. @@ -42,10 +43,11 @@
  27. open_png_file_reading (const char *filename, int *width, int *height)
  28. {
  29. png_data_t *data = (png_data_t*)malloc(sizeof(png_data_t));
  30. + int bit_depth, color_type;
  31. assert(data != 0);
  32. - data->file = fopen(filename, "r");
  33. + data->file = fopen(filename, "rb");
  34. assert(data->file != 0);
  35. data->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
  36. @@ -57,19 +59,41 @@
  37. data->end_info = png_create_info_struct(data->png_ptr);
  38. assert(data->end_info != 0);
  39. - if (setjmp(data->png_ptr->jmpbuf))
  40. + if (setjmp (png_jmpbuf (data->png_ptr)))
  41. assert(0);
  42. png_init_io(data->png_ptr, data->file);
  43. png_read_info(data->png_ptr, data->info_ptr);
  44. - *width = data->info_ptr->width;
  45. - *height = data->info_ptr->height;
  46. + *width = png_get_image_width (data->png_ptr, data->info_ptr);
  47. + *height = png_get_image_height (data->png_ptr, data->info_ptr);
  48. - assert(data->info_ptr->bit_depth == 8 || data->info_ptr->bit_depth == 16);
  49. - assert(data->info_ptr->color_type == PNG_COLOR_TYPE_RGB || data->info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  50. - assert(data->info_ptr->interlace_type == PNG_INTERLACE_NONE);
  51. + bit_depth = png_get_bit_depth (data->png_ptr, data->info_ptr);
  52. + if (bit_depth != 8 && bit_depth != 16)
  53. + {
  54. + fprintf(stderr, "PNG files are only supported with bit depths 8 and 16.\n");
  55. + /* FIXME: free stuff */
  56. + return 0;
  57. + }
  58. +
  59. + color_type = png_get_color_type (data->png_ptr, data->info_ptr);
  60. + if (color_type != PNG_COLOR_TYPE_RGB
  61. + && color_type != PNG_COLOR_TYPE_RGB_ALPHA
  62. + && color_type != PNG_COLOR_TYPE_GRAY
  63. + && color_type != PNG_COLOR_TYPE_GRAY_ALPHA)
  64. + {
  65. + fprintf(stderr, "PNG files are only supported in RGB and Gray, with or without alpha.\n");
  66. + /* FIXME: free stuff */
  67. + return 0;
  68. + }
  69. +
  70. + if (png_get_interlace_type (data->png_ptr, data->info_ptr) != PNG_INTERLACE_NONE)
  71. + {
  72. + fprintf(stderr, "Interlaced PNG files are not supported.\n");
  73. + /* FIXME: free stuff */
  74. + return 0;
  75. + }
  76. data->have_read = 0;
  77. @@ -83,30 +107,44 @@
  78. int i;
  79. int bps, spp;
  80. unsigned char *row;
  81. + int color_type, width;
  82. - if (setjmp(data->png_ptr->jmpbuf))
  83. + if (setjmp (png_jmpbuf (data->png_ptr)))
  84. assert(0);
  85. - if (data->info_ptr->color_type == PNG_COLOR_TYPE_RGB)
  86. + color_type = png_get_color_type (data->png_ptr, data->info_ptr);
  87. + if (color_type == PNG_COLOR_TYPE_GRAY)
  88. + spp = 1;
  89. + else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  90. + spp = 2;
  91. + else if (color_type == PNG_COLOR_TYPE_RGB)
  92. spp = 3;
  93. else
  94. spp = 4;
  95. - if (data->info_ptr->bit_depth == 16)
  96. + if (png_get_bit_depth (data->png_ptr, data->info_ptr) == 16)
  97. bps = 2;
  98. else
  99. bps = 1;
  100. - row = (unsigned char*)malloc(data->info_ptr->width * spp * bps);
  101. + width = png_get_image_width (data->png_ptr, data->info_ptr);
  102. + row = (unsigned char*)malloc (width * spp * bps);
  103. for (i = 0; i < num_lines; ++i)
  104. {
  105. int j, channel;
  106. png_read_row(data->png_ptr, (png_bytep)row, 0);
  107. - for (j = 0; j < data->info_ptr->width; ++j)
  108. - for (channel = 0; channel < 3; ++channel)
  109. - lines[i * data->info_ptr->width * 3 + j * 3 + channel] = row[j * spp * bps + channel * bps];
  110. +
  111. + if (spp <= 2)
  112. + for (j = 0; j < width; ++j)
  113. + for (channel = 0; channel < 3; ++channel)
  114. + lines[i * width * 3 + j * 3 + channel] = row[j * spp * bps];
  115. + else
  116. + for (j = 0; j < width; ++j)
  117. + for (channel = 0; channel < 3; ++channel)
  118. + lines[i * width * 3 + j * 3 + channel]
  119. + = row[j * spp * bps + channel * bps];
  120. }
  121. free(row);
  122. @@ -119,7 +157,7 @@
  123. {
  124. png_data_t *data = (png_data_t*)_data;
  125. - if (setjmp(data->png_ptr->jmpbuf))
  126. + if (setjmp (png_jmpbuf (data->png_ptr)))
  127. assert(0);
  128. if (data->have_read)
  129. @@ -137,9 +175,9 @@
  130. assert(data != 0);
  131. - assert(pixel_stride == 3 || pixel_stride == 4);
  132. + assert(pixel_stride >= 3);
  133. - data->file = fopen(filename, "w");
  134. + data->file = fopen(filename, "wb");
  135. assert(data->file != 0);
  136. data->png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
  137. @@ -148,7 +186,7 @@
  138. data->info_ptr = png_create_info_struct(data->png_ptr);
  139. assert(data->info_ptr != 0);
  140. - if (setjmp(data->png_ptr->jmpbuf))
  141. + if (setjmp (png_jmpbuf (data->png_ptr)))
  142. assert(0);
  143. if (pixel_stride == 4)
  144. @@ -156,21 +194,13 @@
  145. png_init_io(data->png_ptr, data->file);
  146. - data->info_ptr->width = width;
  147. - data->info_ptr->height = height;
  148. - data->info_ptr->valid = 0;
  149. - data->info_ptr->rowbytes = width * 3;
  150. - data->info_ptr->palette = 0;
  151. - data->info_ptr->num_palette = 0;
  152. - data->info_ptr->num_trans = 0;
  153. - data->info_ptr->bit_depth = 8;
  154. - data->info_ptr->color_type = PNG_COLOR_TYPE_RGB;
  155. - data->info_ptr->compression_type = PNG_COMPRESSION_TYPE_DEFAULT;
  156. - data->info_ptr->filter_type = PNG_FILTER_TYPE_DEFAULT;
  157. - data->info_ptr->interlace_type = PNG_INTERLACE_NONE;
  158. + png_set_IHDR (data->png_ptr, data->info_ptr,
  159. + width, height, 8, PNG_COLOR_TYPE_RGB,
  160. + PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  161. png_write_info(data->png_ptr, data->info_ptr);
  162. + data->pixel_stride = pixel_stride;
  163. data->row_stride = row_stride;
  164. return data;
  165. @@ -180,13 +210,45 @@
  166. png_write_lines (void *_data, unsigned char *lines, int num_lines)
  167. {
  168. png_data_t *data = (png_data_t*)_data;
  169. + unsigned char *packed_line;
  170. int i;
  171. + int width;
  172. - if (setjmp(data->png_ptr->jmpbuf))
  173. + if (setjmp (png_jmpbuf (data->png_ptr)))
  174. assert(0);
  175. + width = png_get_image_width (data->png_ptr, data->info_ptr);
  176. + if (data->pixel_stride != 3)
  177. + {
  178. + packed_line = (unsigned char*)malloc(width * 3);
  179. + assert(packed_line != 0);
  180. + }
  181. + else
  182. + packed_line = 0;
  183. +
  184. for (i = 0; i < num_lines; ++i)
  185. - png_write_row(data->png_ptr, (png_bytep)(lines + i * data->row_stride));
  186. + {
  187. + unsigned char *p = lines + i * data->row_stride;
  188. +
  189. + if (packed_line != 0)
  190. + {
  191. + int j;
  192. +
  193. + for (j = 0; j < width; ++j)
  194. + {
  195. + packed_line[j * 3 + 0] = p[j * data->pixel_stride + 0];
  196. + packed_line[j * 3 + 1] = p[j * data->pixel_stride + 1];
  197. + packed_line[j * 3 + 2] = p[j * data->pixel_stride + 2];
  198. + }
  199. +
  200. + p = packed_line;
  201. + }
  202. +
  203. + png_write_row(data->png_ptr, (png_bytep)p);
  204. + }
  205. +
  206. + if (packed_line != 0)
  207. + free(packed_line);
  208. }
  209. void
  210. @@ -194,7 +256,7 @@
  211. {
  212. png_data_t *data = (png_data_t*)_data;
  213. - if (setjmp(data->png_ptr->jmpbuf))
  214. + if (setjmp (png_jmpbuf (data->png_ptr)))
  215. assert(0);
  216. png_write_end(data->png_ptr, data->info_ptr);