pngtrans.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /* pngtrans.c - transforms the data in a row (used by both readers and writers)
  2. *
  3. * libpng 1.2.8 - December 3, 2004
  4. * For conditions of distribution and use, see copyright notice in png.h
  5. * Copyright (c) 1998-2004 Glenn Randers-Pehrson
  6. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  7. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  8. */
  9. #define PNG_INTERNAL
  10. #include "png.h"
  11. #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
  12. /* turn on BGR-to-RGB mapping */
  13. void PNGAPI
  14. png_set_bgr(png_structp png_ptr)
  15. {
  16. png_debug(1, "in png_set_bgr\n");
  17. png_ptr->transformations |= PNG_BGR;
  18. }
  19. #endif
  20. #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
  21. /* turn on 16 bit byte swapping */
  22. void PNGAPI
  23. png_set_swap(png_structp png_ptr)
  24. {
  25. png_debug(1, "in png_set_swap\n");
  26. if (png_ptr->bit_depth == 16)
  27. png_ptr->transformations |= PNG_SWAP_BYTES;
  28. }
  29. #endif
  30. #if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
  31. /* turn on pixel packing */
  32. void PNGAPI
  33. png_set_packing(png_structp png_ptr)
  34. {
  35. png_debug(1, "in png_set_packing\n");
  36. if (png_ptr->bit_depth < 8)
  37. {
  38. png_ptr->transformations |= PNG_PACK;
  39. png_ptr->usr_bit_depth = 8;
  40. }
  41. }
  42. #endif
  43. #if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED)
  44. /* turn on packed pixel swapping */
  45. void PNGAPI
  46. png_set_packswap(png_structp png_ptr)
  47. {
  48. png_debug(1, "in png_set_packswap\n");
  49. if (png_ptr->bit_depth < 8)
  50. png_ptr->transformations |= PNG_PACKSWAP;
  51. }
  52. #endif
  53. #if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
  54. void PNGAPI
  55. png_set_shift(png_structp png_ptr, png_color_8p true_bits)
  56. {
  57. png_debug(1, "in png_set_shift\n");
  58. png_ptr->transformations |= PNG_SHIFT;
  59. png_ptr->shift = *true_bits;
  60. }
  61. #endif
  62. #if defined(PNG_READ_INTERLACING_SUPPORTED) || \
  63. defined(PNG_WRITE_INTERLACING_SUPPORTED)
  64. int PNGAPI
  65. png_set_interlace_handling(png_structp png_ptr)
  66. {
  67. png_debug(1, "in png_set_interlace handling\n");
  68. if (png_ptr->interlaced)
  69. {
  70. png_ptr->transformations |= PNG_INTERLACE;
  71. return (7);
  72. }
  73. return (1);
  74. }
  75. #endif
  76. #if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
  77. /* Add a filler byte on read, or remove a filler or alpha byte on write.
  78. * The filler type has changed in v0.95 to allow future 2-byte fillers
  79. * for 48-bit input data, as well as to avoid problems with some compilers
  80. * that don't like bytes as parameters.
  81. */
  82. void PNGAPI
  83. png_set_filler(png_structp png_ptr, png_uint_32 filler, int filler_loc)
  84. {
  85. png_debug(1, "in png_set_filler\n");
  86. png_ptr->transformations |= PNG_FILLER;
  87. png_ptr->filler = (png_byte)filler;
  88. if (filler_loc == PNG_FILLER_AFTER)
  89. png_ptr->flags |= PNG_FLAG_FILLER_AFTER;
  90. else
  91. png_ptr->flags &= ~PNG_FLAG_FILLER_AFTER;
  92. /* This should probably go in the "do_read_filler" routine.
  93. * I attempted to do that in libpng-1.0.1a but that caused problems
  94. * so I restored it in libpng-1.0.2a
  95. */
  96. if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  97. {
  98. png_ptr->usr_channels = 4;
  99. }
  100. /* Also I added this in libpng-1.0.2a (what happens when we expand
  101. * a less-than-8-bit grayscale to GA? */
  102. if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY && png_ptr->bit_depth >= 8)
  103. {
  104. png_ptr->usr_channels = 2;
  105. }
  106. }
  107. #if !defined(PNG_1_0_X)
  108. /* Added to libpng-1.2.7 */
  109. void PNGAPI
  110. png_set_add_alpha(png_structp png_ptr, png_uint_32 filler, int filler_loc)
  111. {
  112. png_debug(1, "in png_set_add_alpha\n");
  113. png_set_filler(png_ptr, filler, filler_loc);
  114. png_ptr->transformations |= PNG_ADD_ALPHA;
  115. }
  116. #endif
  117. #endif
  118. #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \
  119. defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
  120. void PNGAPI
  121. png_set_swap_alpha(png_structp png_ptr)
  122. {
  123. png_debug(1, "in png_set_swap_alpha\n");
  124. png_ptr->transformations |= PNG_SWAP_ALPHA;
  125. }
  126. #endif
  127. #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \
  128. defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
  129. void PNGAPI
  130. png_set_invert_alpha(png_structp png_ptr)
  131. {
  132. png_debug(1, "in png_set_invert_alpha\n");
  133. png_ptr->transformations |= PNG_INVERT_ALPHA;
  134. }
  135. #endif
  136. #if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
  137. void PNGAPI
  138. png_set_invert_mono(png_structp png_ptr)
  139. {
  140. png_debug(1, "in png_set_invert_mono\n");
  141. png_ptr->transformations |= PNG_INVERT_MONO;
  142. }
  143. /* invert monochrome grayscale data */
  144. void /* PRIVATE */
  145. png_do_invert(png_row_infop row_info, png_bytep row)
  146. {
  147. png_debug(1, "in png_do_invert\n");
  148. /* This test removed from libpng version 1.0.13 and 1.2.0:
  149. * if (row_info->bit_depth == 1 &&
  150. */
  151. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  152. if (row == NULL || row_info == NULL)
  153. return;
  154. #endif
  155. if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
  156. {
  157. png_bytep rp = row;
  158. png_uint_32 i;
  159. png_uint_32 istop = row_info->rowbytes;
  160. for (i = 0; i < istop; i++)
  161. {
  162. *rp = (png_byte)(~(*rp));
  163. rp++;
  164. }
  165. }
  166. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
  167. row_info->bit_depth == 8)
  168. {
  169. png_bytep rp = row;
  170. png_uint_32 i;
  171. png_uint_32 istop = row_info->rowbytes;
  172. for (i = 0; i < istop; i+=2)
  173. {
  174. *rp = (png_byte)(~(*rp));
  175. rp+=2;
  176. }
  177. }
  178. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
  179. row_info->bit_depth == 16)
  180. {
  181. png_bytep rp = row;
  182. png_uint_32 i;
  183. png_uint_32 istop = row_info->rowbytes;
  184. for (i = 0; i < istop; i+=4)
  185. {
  186. *rp = (png_byte)(~(*rp));
  187. *(rp+1) = (png_byte)(~(*(rp+1)));
  188. rp+=4;
  189. }
  190. }
  191. }
  192. #endif
  193. #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
  194. /* swaps byte order on 16 bit depth images */
  195. void /* PRIVATE */
  196. png_do_swap(png_row_infop row_info, png_bytep row)
  197. {
  198. png_debug(1, "in png_do_swap\n");
  199. if (
  200. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  201. row != NULL && row_info != NULL &&
  202. #endif
  203. row_info->bit_depth == 16)
  204. {
  205. png_bytep rp = row;
  206. png_uint_32 i;
  207. png_uint_32 istop= row_info->width * row_info->channels;
  208. for (i = 0; i < istop; i++, rp += 2)
  209. {
  210. png_byte t = *rp;
  211. *rp = *(rp + 1);
  212. *(rp + 1) = t;
  213. }
  214. }
  215. }
  216. #endif
  217. #if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED)
  218. static png_byte onebppswaptable[256] = {
  219. 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
  220. 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
  221. 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
  222. 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
  223. 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4,
  224. 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
  225. 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC,
  226. 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
  227. 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,
  228. 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
  229. 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA,
  230. 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
  231. 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6,
  232. 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
  233. 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,
  234. 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
  235. 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1,
  236. 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
  237. 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9,
  238. 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
  239. 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5,
  240. 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
  241. 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED,
  242. 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
  243. 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3,
  244. 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
  245. 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,
  246. 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
  247. 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7,
  248. 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
  249. 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF,
  250. 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
  251. };
  252. static png_byte twobppswaptable[256] = {
  253. 0x00, 0x40, 0x80, 0xC0, 0x10, 0x50, 0x90, 0xD0,
  254. 0x20, 0x60, 0xA0, 0xE0, 0x30, 0x70, 0xB0, 0xF0,
  255. 0x04, 0x44, 0x84, 0xC4, 0x14, 0x54, 0x94, 0xD4,
  256. 0x24, 0x64, 0xA4, 0xE4, 0x34, 0x74, 0xB4, 0xF4,
  257. 0x08, 0x48, 0x88, 0xC8, 0x18, 0x58, 0x98, 0xD8,
  258. 0x28, 0x68, 0xA8, 0xE8, 0x38, 0x78, 0xB8, 0xF8,
  259. 0x0C, 0x4C, 0x8C, 0xCC, 0x1C, 0x5C, 0x9C, 0xDC,
  260. 0x2C, 0x6C, 0xAC, 0xEC, 0x3C, 0x7C, 0xBC, 0xFC,
  261. 0x01, 0x41, 0x81, 0xC1, 0x11, 0x51, 0x91, 0xD1,
  262. 0x21, 0x61, 0xA1, 0xE1, 0x31, 0x71, 0xB1, 0xF1,
  263. 0x05, 0x45, 0x85, 0xC5, 0x15, 0x55, 0x95, 0xD5,
  264. 0x25, 0x65, 0xA5, 0xE5, 0x35, 0x75, 0xB5, 0xF5,
  265. 0x09, 0x49, 0x89, 0xC9, 0x19, 0x59, 0x99, 0xD9,
  266. 0x29, 0x69, 0xA9, 0xE9, 0x39, 0x79, 0xB9, 0xF9,
  267. 0x0D, 0x4D, 0x8D, 0xCD, 0x1D, 0x5D, 0x9D, 0xDD,
  268. 0x2D, 0x6D, 0xAD, 0xED, 0x3D, 0x7D, 0xBD, 0xFD,
  269. 0x02, 0x42, 0x82, 0xC2, 0x12, 0x52, 0x92, 0xD2,
  270. 0x22, 0x62, 0xA2, 0xE2, 0x32, 0x72, 0xB2, 0xF2,
  271. 0x06, 0x46, 0x86, 0xC6, 0x16, 0x56, 0x96, 0xD6,
  272. 0x26, 0x66, 0xA6, 0xE6, 0x36, 0x76, 0xB6, 0xF6,
  273. 0x0A, 0x4A, 0x8A, 0xCA, 0x1A, 0x5A, 0x9A, 0xDA,
  274. 0x2A, 0x6A, 0xAA, 0xEA, 0x3A, 0x7A, 0xBA, 0xFA,
  275. 0x0E, 0x4E, 0x8E, 0xCE, 0x1E, 0x5E, 0x9E, 0xDE,
  276. 0x2E, 0x6E, 0xAE, 0xEE, 0x3E, 0x7E, 0xBE, 0xFE,
  277. 0x03, 0x43, 0x83, 0xC3, 0x13, 0x53, 0x93, 0xD3,
  278. 0x23, 0x63, 0xA3, 0xE3, 0x33, 0x73, 0xB3, 0xF3,
  279. 0x07, 0x47, 0x87, 0xC7, 0x17, 0x57, 0x97, 0xD7,
  280. 0x27, 0x67, 0xA7, 0xE7, 0x37, 0x77, 0xB7, 0xF7,
  281. 0x0B, 0x4B, 0x8B, 0xCB, 0x1B, 0x5B, 0x9B, 0xDB,
  282. 0x2B, 0x6B, 0xAB, 0xEB, 0x3B, 0x7B, 0xBB, 0xFB,
  283. 0x0F, 0x4F, 0x8F, 0xCF, 0x1F, 0x5F, 0x9F, 0xDF,
  284. 0x2F, 0x6F, 0xAF, 0xEF, 0x3F, 0x7F, 0xBF, 0xFF
  285. };
  286. static png_byte fourbppswaptable[256] = {
  287. 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,
  288. 0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0,
  289. 0x01, 0x11, 0x21, 0x31, 0x41, 0x51, 0x61, 0x71,
  290. 0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1,
  291. 0x02, 0x12, 0x22, 0x32, 0x42, 0x52, 0x62, 0x72,
  292. 0x82, 0x92, 0xA2, 0xB2, 0xC2, 0xD2, 0xE2, 0xF2,
  293. 0x03, 0x13, 0x23, 0x33, 0x43, 0x53, 0x63, 0x73,
  294. 0x83, 0x93, 0xA3, 0xB3, 0xC3, 0xD3, 0xE3, 0xF3,
  295. 0x04, 0x14, 0x24, 0x34, 0x44, 0x54, 0x64, 0x74,
  296. 0x84, 0x94, 0xA4, 0xB4, 0xC4, 0xD4, 0xE4, 0xF4,
  297. 0x05, 0x15, 0x25, 0x35, 0x45, 0x55, 0x65, 0x75,
  298. 0x85, 0x95, 0xA5, 0xB5, 0xC5, 0xD5, 0xE5, 0xF5,
  299. 0x06, 0x16, 0x26, 0x36, 0x46, 0x56, 0x66, 0x76,
  300. 0x86, 0x96, 0xA6, 0xB6, 0xC6, 0xD6, 0xE6, 0xF6,
  301. 0x07, 0x17, 0x27, 0x37, 0x47, 0x57, 0x67, 0x77,
  302. 0x87, 0x97, 0xA7, 0xB7, 0xC7, 0xD7, 0xE7, 0xF7,
  303. 0x08, 0x18, 0x28, 0x38, 0x48, 0x58, 0x68, 0x78,
  304. 0x88, 0x98, 0xA8, 0xB8, 0xC8, 0xD8, 0xE8, 0xF8,
  305. 0x09, 0x19, 0x29, 0x39, 0x49, 0x59, 0x69, 0x79,
  306. 0x89, 0x99, 0xA9, 0xB9, 0xC9, 0xD9, 0xE9, 0xF9,
  307. 0x0A, 0x1A, 0x2A, 0x3A, 0x4A, 0x5A, 0x6A, 0x7A,
  308. 0x8A, 0x9A, 0xAA, 0xBA, 0xCA, 0xDA, 0xEA, 0xFA,
  309. 0x0B, 0x1B, 0x2B, 0x3B, 0x4B, 0x5B, 0x6B, 0x7B,
  310. 0x8B, 0x9B, 0xAB, 0xBB, 0xCB, 0xDB, 0xEB, 0xFB,
  311. 0x0C, 0x1C, 0x2C, 0x3C, 0x4C, 0x5C, 0x6C, 0x7C,
  312. 0x8C, 0x9C, 0xAC, 0xBC, 0xCC, 0xDC, 0xEC, 0xFC,
  313. 0x0D, 0x1D, 0x2D, 0x3D, 0x4D, 0x5D, 0x6D, 0x7D,
  314. 0x8D, 0x9D, 0xAD, 0xBD, 0xCD, 0xDD, 0xED, 0xFD,
  315. 0x0E, 0x1E, 0x2E, 0x3E, 0x4E, 0x5E, 0x6E, 0x7E,
  316. 0x8E, 0x9E, 0xAE, 0xBE, 0xCE, 0xDE, 0xEE, 0xFE,
  317. 0x0F, 0x1F, 0x2F, 0x3F, 0x4F, 0x5F, 0x6F, 0x7F,
  318. 0x8F, 0x9F, 0xAF, 0xBF, 0xCF, 0xDF, 0xEF, 0xFF
  319. };
  320. /* swaps pixel packing order within bytes */
  321. void /* PRIVATE */
  322. png_do_packswap(png_row_infop row_info, png_bytep row)
  323. {
  324. png_debug(1, "in png_do_packswap\n");
  325. if (
  326. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  327. row != NULL && row_info != NULL &&
  328. #endif
  329. row_info->bit_depth < 8)
  330. {
  331. png_bytep rp, end, table;
  332. end = row + row_info->rowbytes;
  333. if (row_info->bit_depth == 1)
  334. table = onebppswaptable;
  335. else if (row_info->bit_depth == 2)
  336. table = twobppswaptable;
  337. else if (row_info->bit_depth == 4)
  338. table = fourbppswaptable;
  339. else
  340. return;
  341. for (rp = row; rp < end; rp++)
  342. *rp = table[*rp];
  343. }
  344. }
  345. #endif /* PNG_READ_PACKSWAP_SUPPORTED or PNG_WRITE_PACKSWAP_SUPPORTED */
  346. #if defined(PNG_WRITE_FILLER_SUPPORTED) || \
  347. defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
  348. /* remove filler or alpha byte(s) */
  349. void /* PRIVATE */
  350. png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags)
  351. {
  352. png_debug(1, "in png_do_strip_filler\n");
  353. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  354. if (row != NULL && row_info != NULL)
  355. #endif
  356. {
  357. png_bytep sp=row;
  358. png_bytep dp=row;
  359. png_uint_32 row_width=row_info->width;
  360. png_uint_32 i;
  361. if ((row_info->color_type == PNG_COLOR_TYPE_RGB ||
  362. (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
  363. (flags & PNG_FLAG_STRIP_ALPHA))) &&
  364. row_info->channels == 4)
  365. {
  366. if (row_info->bit_depth == 8)
  367. {
  368. /* This converts from RGBX or RGBA to RGB */
  369. if (flags & PNG_FLAG_FILLER_AFTER)
  370. {
  371. dp+=3; sp+=4;
  372. for (i = 1; i < row_width; i++)
  373. {
  374. *dp++ = *sp++;
  375. *dp++ = *sp++;
  376. *dp++ = *sp++;
  377. sp++;
  378. }
  379. }
  380. /* This converts from XRGB or ARGB to RGB */
  381. else
  382. {
  383. for (i = 0; i < row_width; i++)
  384. {
  385. sp++;
  386. *dp++ = *sp++;
  387. *dp++ = *sp++;
  388. *dp++ = *sp++;
  389. }
  390. }
  391. row_info->pixel_depth = 24;
  392. row_info->rowbytes = row_width * 3;
  393. }
  394. else /* if (row_info->bit_depth == 16) */
  395. {
  396. if (flags & PNG_FLAG_FILLER_AFTER)
  397. {
  398. /* This converts from RRGGBBXX or RRGGBBAA to RRGGBB */
  399. sp += 8; dp += 6;
  400. for (i = 1; i < row_width; i++)
  401. {
  402. /* This could be (although png_memcpy is probably slower):
  403. png_memcpy(dp, sp, 6);
  404. sp += 8;
  405. dp += 6;
  406. */
  407. *dp++ = *sp++;
  408. *dp++ = *sp++;
  409. *dp++ = *sp++;
  410. *dp++ = *sp++;
  411. *dp++ = *sp++;
  412. *dp++ = *sp++;
  413. sp += 2;
  414. }
  415. }
  416. else
  417. {
  418. /* This converts from XXRRGGBB or AARRGGBB to RRGGBB */
  419. for (i = 0; i < row_width; i++)
  420. {
  421. /* This could be (although png_memcpy is probably slower):
  422. png_memcpy(dp, sp, 6);
  423. sp += 8;
  424. dp += 6;
  425. */
  426. sp+=2;
  427. *dp++ = *sp++;
  428. *dp++ = *sp++;
  429. *dp++ = *sp++;
  430. *dp++ = *sp++;
  431. *dp++ = *sp++;
  432. *dp++ = *sp++;
  433. }
  434. }
  435. row_info->pixel_depth = 48;
  436. row_info->rowbytes = row_width * 6;
  437. }
  438. row_info->channels = 3;
  439. }
  440. else if ((row_info->color_type == PNG_COLOR_TYPE_GRAY ||
  441. (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
  442. (flags & PNG_FLAG_STRIP_ALPHA))) &&
  443. row_info->channels == 2)
  444. {
  445. if (row_info->bit_depth == 8)
  446. {
  447. /* This converts from GX or GA to G */
  448. if (flags & PNG_FLAG_FILLER_AFTER)
  449. {
  450. for (i = 0; i < row_width; i++)
  451. {
  452. *dp++ = *sp++;
  453. sp++;
  454. }
  455. }
  456. /* This converts from XG or AG to G */
  457. else
  458. {
  459. for (i = 0; i < row_width; i++)
  460. {
  461. sp++;
  462. *dp++ = *sp++;
  463. }
  464. }
  465. row_info->pixel_depth = 8;
  466. row_info->rowbytes = row_width;
  467. }
  468. else /* if (row_info->bit_depth == 16) */
  469. {
  470. if (flags & PNG_FLAG_FILLER_AFTER)
  471. {
  472. /* This converts from GGXX or GGAA to GG */
  473. sp += 4; dp += 2;
  474. for (i = 1; i < row_width; i++)
  475. {
  476. *dp++ = *sp++;
  477. *dp++ = *sp++;
  478. sp += 2;
  479. }
  480. }
  481. else
  482. {
  483. /* This converts from XXGG or AAGG to GG */
  484. for (i = 0; i < row_width; i++)
  485. {
  486. sp += 2;
  487. *dp++ = *sp++;
  488. *dp++ = *sp++;
  489. }
  490. }
  491. row_info->pixel_depth = 16;
  492. row_info->rowbytes = row_width * 2;
  493. }
  494. row_info->channels = 1;
  495. }
  496. if (flags & PNG_FLAG_STRIP_ALPHA)
  497. row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
  498. }
  499. }
  500. #endif
  501. #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
  502. /* swaps red and blue bytes within a pixel */
  503. void /* PRIVATE */
  504. png_do_bgr(png_row_infop row_info, png_bytep row)
  505. {
  506. png_debug(1, "in png_do_bgr\n");
  507. if (
  508. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  509. row != NULL && row_info != NULL &&
  510. #endif
  511. (row_info->color_type & PNG_COLOR_MASK_COLOR))
  512. {
  513. png_uint_32 row_width = row_info->width;
  514. if (row_info->bit_depth == 8)
  515. {
  516. if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  517. {
  518. png_bytep rp;
  519. png_uint_32 i;
  520. for (i = 0, rp = row; i < row_width; i++, rp += 3)
  521. {
  522. png_byte save = *rp;
  523. *rp = *(rp + 2);
  524. *(rp + 2) = save;
  525. }
  526. }
  527. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  528. {
  529. png_bytep rp;
  530. png_uint_32 i;
  531. for (i = 0, rp = row; i < row_width; i++, rp += 4)
  532. {
  533. png_byte save = *rp;
  534. *rp = *(rp + 2);
  535. *(rp + 2) = save;
  536. }
  537. }
  538. }
  539. else if (row_info->bit_depth == 16)
  540. {
  541. if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  542. {
  543. png_bytep rp;
  544. png_uint_32 i;
  545. for (i = 0, rp = row; i < row_width; i++, rp += 6)
  546. {
  547. png_byte save = *rp;
  548. *rp = *(rp + 4);
  549. *(rp + 4) = save;
  550. save = *(rp + 1);
  551. *(rp + 1) = *(rp + 5);
  552. *(rp + 5) = save;
  553. }
  554. }
  555. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  556. {
  557. png_bytep rp;
  558. png_uint_32 i;
  559. for (i = 0, rp = row; i < row_width; i++, rp += 8)
  560. {
  561. png_byte save = *rp;
  562. *rp = *(rp + 4);
  563. *(rp + 4) = save;
  564. save = *(rp + 1);
  565. *(rp + 1) = *(rp + 5);
  566. *(rp + 5) = save;
  567. }
  568. }
  569. }
  570. }
  571. }
  572. #endif /* PNG_READ_BGR_SUPPORTED or PNG_WRITE_BGR_SUPPORTED */
  573. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
  574. defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
  575. defined(PNG_LEGACY_SUPPORTED)
  576. void PNGAPI
  577. png_set_user_transform_info(png_structp png_ptr, png_voidp
  578. user_transform_ptr, int user_transform_depth, int user_transform_channels)
  579. {
  580. png_debug(1, "in png_set_user_transform_info\n");
  581. #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
  582. png_ptr->user_transform_ptr = user_transform_ptr;
  583. png_ptr->user_transform_depth = (png_byte)user_transform_depth;
  584. png_ptr->user_transform_channels = (png_byte)user_transform_channels;
  585. #else
  586. if(user_transform_ptr || user_transform_depth || user_transform_channels)
  587. png_warning(png_ptr,
  588. "This version of libpng does not support user transform info");
  589. #endif
  590. }
  591. #endif
  592. /* This function returns a pointer to the user_transform_ptr associated with
  593. * the user transform functions. The application should free any memory
  594. * associated with this pointer before png_write_destroy and png_read_destroy
  595. * are called.
  596. */
  597. png_voidp PNGAPI
  598. png_get_user_transform_ptr(png_structp png_ptr)
  599. {
  600. #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
  601. return ((png_voidp)png_ptr->user_transform_ptr);
  602. #else
  603. if(png_ptr)
  604. return (NULL);
  605. return (NULL);
  606. #endif
  607. }