load-gif.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. // Copyright (C) 2004-2021 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "pixmap-imp.h"
  24. #include <string.h>
  25. #include <limits.h>
  26. struct info
  27. {
  28. int gif89a;
  29. unsigned int width, height;
  30. unsigned char aspect;
  31. unsigned int xres, yres;
  32. unsigned int image_left, image_top;
  33. unsigned int image_width, image_height;
  34. unsigned int image_interlaced;
  35. int has_gct;
  36. int gct_entries;
  37. unsigned char *gct;
  38. unsigned int gct_background;
  39. int has_lct;
  40. int lct_entries;
  41. unsigned char *lct;
  42. int has_transparency;
  43. unsigned int transparent;
  44. unsigned char *mask;
  45. fz_pixmap *pix;
  46. };
  47. /* default color table, where the first two entries are black and white */
  48. static const unsigned char dct[256 * 3] = {
  49. 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02,
  50. 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06,
  51. 0x07, 0x07, 0x07, 0x08, 0x08, 0x08, 0x09, 0x09, 0x09, 0x0a, 0x0a, 0x0a,
  52. 0x0b, 0x0b, 0x0b, 0x0c, 0x0c, 0x0c, 0x0d, 0x0d, 0x0d, 0x0e, 0x0e, 0x0e,
  53. 0x0f, 0x0f, 0x0f, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x12, 0x12, 0x12,
  54. 0x13, 0x13, 0x13, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x16, 0x16, 0x16,
  55. 0x17, 0x17, 0x17, 0x18, 0x18, 0x18, 0x19, 0x19, 0x19, 0x1a, 0x1a, 0x1a,
  56. 0x1b, 0x1b, 0x1b, 0x1c, 0x1c, 0x1c, 0x1d, 0x1d, 0x1d, 0x1e, 0x1e, 0x1e,
  57. 0x1f, 0x1f, 0x1f, 0x20, 0x20, 0x20, 0x21, 0x21, 0x21, 0x22, 0x22, 0x22,
  58. 0x23, 0x23, 0x23, 0x24, 0x24, 0x24, 0x25, 0x25, 0x25, 0x26, 0x26, 0x26,
  59. 0x27, 0x27, 0x27, 0x28, 0x28, 0x28, 0x29, 0x29, 0x29, 0x2a, 0x2a, 0x2a,
  60. 0x2b, 0x2b, 0x2b, 0x2c, 0x2c, 0x2c, 0x2d, 0x2d, 0x2d, 0x2e, 0x2e, 0x2e,
  61. 0x2f, 0x2f, 0x2f, 0x30, 0x30, 0x30, 0x31, 0x31, 0x31, 0x32, 0x32, 0x32,
  62. 0x33, 0x33, 0x33, 0x34, 0x34, 0x34, 0x35, 0x35, 0x35, 0x36, 0x36, 0x36,
  63. 0x37, 0x37, 0x37, 0x38, 0x38, 0x38, 0x39, 0x39, 0x39, 0x3a, 0x3a, 0x3a,
  64. 0x3b, 0x3b, 0x3b, 0x3c, 0x3c, 0x3c, 0x3d, 0x3d, 0x3d, 0x3e, 0x3e, 0x3e,
  65. 0x3f, 0x3f, 0x3f, 0x40, 0x40, 0x40, 0x41, 0x41, 0x41, 0x42, 0x42, 0x42,
  66. 0x43, 0x43, 0x43, 0x44, 0x44, 0x44, 0x45, 0x45, 0x45, 0x46, 0x46, 0x46,
  67. 0x47, 0x47, 0x47, 0x48, 0x48, 0x48, 0x49, 0x49, 0x49, 0x4a, 0x4a, 0x4a,
  68. 0x4b, 0x4b, 0x4b, 0x4c, 0x4c, 0x4c, 0x4d, 0x4d, 0x4d, 0x4e, 0x4e, 0x4e,
  69. 0x4f, 0x4f, 0x4f, 0x50, 0x50, 0x50, 0x51, 0x51, 0x51, 0x52, 0x52, 0x52,
  70. 0x53, 0x53, 0x53, 0x54, 0x54, 0x54, 0x55, 0x55, 0x55, 0x56, 0x56, 0x56,
  71. 0x57, 0x57, 0x57, 0x58, 0x58, 0x58, 0x59, 0x59, 0x59, 0x5a, 0x5a, 0x5a,
  72. 0x5b, 0x5b, 0x5b, 0x5c, 0x5c, 0x5c, 0x5d, 0x5d, 0x5d, 0x5e, 0x5e, 0x5e,
  73. 0x5f, 0x5f, 0x5f, 0x60, 0x60, 0x60, 0x61, 0x61, 0x61, 0x62, 0x62, 0x62,
  74. 0x63, 0x63, 0x63, 0x64, 0x64, 0x64, 0x65, 0x65, 0x65, 0x66, 0x66, 0x66,
  75. 0x67, 0x67, 0x67, 0x68, 0x68, 0x68, 0x69, 0x69, 0x69, 0x6a, 0x6a, 0x6a,
  76. 0x6b, 0x6b, 0x6b, 0x6c, 0x6c, 0x6c, 0x6d, 0x6d, 0x6d, 0x6e, 0x6e, 0x6e,
  77. 0x6f, 0x6f, 0x6f, 0x70, 0x70, 0x70, 0x71, 0x71, 0x71, 0x72, 0x72, 0x72,
  78. 0x73, 0x73, 0x73, 0x74, 0x74, 0x74, 0x75, 0x75, 0x75, 0x76, 0x76, 0x76,
  79. 0x77, 0x77, 0x77, 0x78, 0x78, 0x78, 0x79, 0x79, 0x79, 0x7a, 0x7a, 0x7a,
  80. 0x7b, 0x7b, 0x7b, 0x7c, 0x7c, 0x7c, 0x7d, 0x7d, 0x7d, 0x7e, 0x7e, 0x7e,
  81. 0x7f, 0x7f, 0x7f, 0x80, 0x80, 0x80, 0x81, 0x81, 0x81, 0x82, 0x82, 0x82,
  82. 0x83, 0x83, 0x83, 0x84, 0x84, 0x84, 0x85, 0x85, 0x85, 0x86, 0x86, 0x86,
  83. 0x87, 0x87, 0x87, 0x88, 0x88, 0x88, 0x89, 0x89, 0x89, 0x8a, 0x8a, 0x8a,
  84. 0x8b, 0x8b, 0x8b, 0x8c, 0x8c, 0x8c, 0x8d, 0x8d, 0x8d, 0x8e, 0x8e, 0x8e,
  85. 0x8f, 0x8f, 0x8f, 0x90, 0x90, 0x90, 0x91, 0x91, 0x91, 0x92, 0x92, 0x92,
  86. 0x93, 0x93, 0x93, 0x94, 0x94, 0x94, 0x95, 0x95, 0x95, 0x96, 0x96, 0x96,
  87. 0x97, 0x97, 0x97, 0x98, 0x98, 0x98, 0x99, 0x99, 0x99, 0x9a, 0x9a, 0x9a,
  88. 0x9b, 0x9b, 0x9b, 0x9c, 0x9c, 0x9c, 0x9d, 0x9d, 0x9d, 0x9e, 0x9e, 0x9e,
  89. 0x9f, 0x9f, 0x9f, 0xa0, 0xa0, 0xa0, 0xa1, 0xa1, 0xa1, 0xa2, 0xa2, 0xa2,
  90. 0xa3, 0xa3, 0xa3, 0xa4, 0xa4, 0xa4, 0xa5, 0xa5, 0xa5, 0xa6, 0xa6, 0xa6,
  91. 0xa7, 0xa7, 0xa7, 0xa8, 0xa8, 0xa8, 0xa9, 0xa9, 0xa9, 0xaa, 0xaa, 0xaa,
  92. 0xab, 0xab, 0xab, 0xac, 0xac, 0xac, 0xad, 0xad, 0xad, 0xae, 0xae, 0xae,
  93. 0xaf, 0xaf, 0xaf, 0xb0, 0xb0, 0xb0, 0xb1, 0xb1, 0xb1, 0xb2, 0xb2, 0xb2,
  94. 0xb3, 0xb3, 0xb3, 0xb4, 0xb4, 0xb4, 0xb5, 0xb5, 0xb5, 0xb6, 0xb6, 0xb6,
  95. 0xb7, 0xb7, 0xb7, 0xb8, 0xb8, 0xb8, 0xb9, 0xb9, 0xb9, 0xba, 0xba, 0xba,
  96. 0xbb, 0xbb, 0xbb, 0xbc, 0xbc, 0xbc, 0xbd, 0xbd, 0xbd, 0xbe, 0xbe, 0xbe,
  97. 0xbf, 0xbf, 0xbf, 0xc0, 0xc0, 0xc0, 0xc1, 0xc1, 0xc1, 0xc2, 0xc2, 0xc2,
  98. 0xc3, 0xc3, 0xc3, 0xc4, 0xc4, 0xc4, 0xc5, 0xc5, 0xc5, 0xc6, 0xc6, 0xc6,
  99. 0xc7, 0xc7, 0xc7, 0xc8, 0xc8, 0xc8, 0xc9, 0xc9, 0xc9, 0xca, 0xca, 0xca,
  100. 0xcb, 0xcb, 0xcb, 0xcc, 0xcc, 0xcc, 0xcd, 0xcd, 0xcd, 0xce, 0xce, 0xce,
  101. 0xcf, 0xcf, 0xcf, 0xd0, 0xd0, 0xd0, 0xd1, 0xd1, 0xd1, 0xd2, 0xd2, 0xd2,
  102. 0xd3, 0xd3, 0xd3, 0xd4, 0xd4, 0xd4, 0xd5, 0xd5, 0xd5, 0xd6, 0xd6, 0xd6,
  103. 0xd7, 0xd7, 0xd7, 0xd8, 0xd8, 0xd8, 0xd9, 0xd9, 0xd9, 0xda, 0xda, 0xda,
  104. 0xdb, 0xdb, 0xdb, 0xdc, 0xdc, 0xdc, 0xdd, 0xdd, 0xdd, 0xde, 0xde, 0xde,
  105. 0xdf, 0xdf, 0xdf, 0xe0, 0xe0, 0xe0, 0xe1, 0xe1, 0xe1, 0xe2, 0xe2, 0xe2,
  106. 0xe3, 0xe3, 0xe3, 0xe4, 0xe4, 0xe4, 0xe5, 0xe5, 0xe5, 0xe6, 0xe6, 0xe6,
  107. 0xe7, 0xe7, 0xe7, 0xe8, 0xe8, 0xe8, 0xe9, 0xe9, 0xe9, 0xea, 0xea, 0xea,
  108. 0xeb, 0xeb, 0xeb, 0xec, 0xec, 0xec, 0xed, 0xed, 0xed, 0xee, 0xee, 0xee,
  109. 0xef, 0xef, 0xef, 0xf0, 0xf0, 0xf0, 0xf1, 0xf1, 0xf1, 0xf2, 0xf2, 0xf2,
  110. 0xf3, 0xf3, 0xf3, 0xf4, 0xf4, 0xf4, 0xf5, 0xf5, 0xf5, 0xf6, 0xf6, 0xf6,
  111. 0xf7, 0xf7, 0xf7, 0xf8, 0xf8, 0xf8, 0xf9, 0xf9, 0xf9, 0xfa, 0xfa, 0xfa,
  112. 0xfb, 0xfb, 0xfb, 0xfc, 0xfc, 0xfc, 0xfd, 0xfd, 0xfd, 0xfe, 0xfe, 0xfe,
  113. };
  114. static const unsigned char *
  115. gif_read_subblocks(fz_context *ctx, struct info *info, const unsigned char *p, const unsigned char *end, fz_buffer *buf)
  116. {
  117. int len;
  118. do
  119. {
  120. if (end - p < 1)
  121. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in data subblocks in gif image");
  122. len = *p;
  123. p += 1;
  124. if (len > 0)
  125. {
  126. if (end - p < len)
  127. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in data subblock in gif image");
  128. if (buf)
  129. fz_append_data(ctx, buf, p, len);
  130. p += len;
  131. }
  132. } while (len > 0);
  133. return p;
  134. }
  135. static const unsigned char *
  136. gif_read_header(fz_context *ctx, struct info *info, const unsigned char *p, const unsigned char *end)
  137. {
  138. if (end - p < 6)
  139. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in header in gif image");
  140. if (memcmp(&p[0], "GIF", 3))
  141. fz_throw(ctx, FZ_ERROR_FORMAT, "invalid signature in gif image");
  142. if (memcmp(&p[3], "87a", 3) && memcmp(&p[3], "89a", 3))
  143. fz_throw(ctx, FZ_ERROR_FORMAT, "unsupported version in gif image");
  144. info->gif89a = !memcmp(p, "GIF89a", 6);
  145. return p + 6;
  146. }
  147. /* coverity[-tainted_data_return] */
  148. static unsigned int
  149. safe_load_u16(const unsigned char *p)
  150. {
  151. return p[1] << 8 | p[0];
  152. }
  153. static const unsigned char *
  154. gif_read_lsd(fz_context *ctx, struct info *info, const unsigned char *p, const unsigned char *end)
  155. {
  156. if (end - p < 7)
  157. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in logical screen descriptor in gif image");
  158. info->width = safe_load_u16(p);
  159. info->height = safe_load_u16(p+2);
  160. if (info->width <= 0)
  161. fz_throw(ctx, FZ_ERROR_FORMAT, "image width must be > 0");
  162. if (info->height <= 0)
  163. fz_throw(ctx, FZ_ERROR_FORMAT, "image height must be > 0");
  164. if (info->height > UINT_MAX / info->width / 3 /* components */)
  165. fz_throw(ctx, FZ_ERROR_LIMIT, "image dimensions might overflow");
  166. info->has_gct = (p[4] >> 7) & 0x1;
  167. if (info->has_gct)
  168. {
  169. info->gct_entries = 1 << ((p[4] & 0x7) + 1);
  170. info->gct_background = fz_clampi(p[5], 0, info->gct_entries - 1);
  171. }
  172. info->aspect = p[6];
  173. info->xres = 96;
  174. info->yres = 96;
  175. if (info->aspect > 0)
  176. info->yres = (((float) info->aspect + 15) / 64) * 96;
  177. return p + 7;
  178. }
  179. static const unsigned char *
  180. gif_read_gct(fz_context *ctx, struct info *info, const unsigned char *p, const unsigned char *end)
  181. {
  182. if (end - p < info->gct_entries * 3)
  183. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in global color table in gif image");
  184. info->gct = Memento_label(fz_malloc(ctx, info->gct_entries * 3), "gif_gct");
  185. memmove(info->gct, p, info->gct_entries * 3);
  186. return p + info->gct_entries * 3;
  187. }
  188. static const unsigned char *
  189. gif_read_id(fz_context *ctx, struct info *info, const unsigned char *p, const unsigned char *end)
  190. {
  191. if (end - p < 10)
  192. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in image descriptor in gif image");
  193. info->image_left = p[2] << 8 | p[1];
  194. info->image_top = p[4] << 8 | p[3];
  195. info->image_width = p[6] << 8 | p[5];
  196. info->image_height = p[8] << 8 | p[7];
  197. info->has_lct = (p[9] >> 7) & 0x1;
  198. info->image_interlaced = (p[9] >> 6) & 0x1;
  199. if (info->has_lct)
  200. info->lct_entries = 1 << ((p[9] & 0x7) + 1);
  201. return p + 10;
  202. }
  203. static const unsigned char *
  204. gif_read_lct(fz_context *ctx, struct info *info, const unsigned char *p, const unsigned char *end)
  205. {
  206. if (end - p < info->lct_entries * 3)
  207. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in local color table in gif image");
  208. info->lct = Memento_label(fz_malloc(ctx, info->lct_entries * 3), "gif_lct");
  209. memmove(info->lct, p, info->lct_entries * 3);
  210. return p + info->lct_entries * 3;
  211. }
  212. static void
  213. gif_read_line(fz_context *ctx, struct info *info, int ct_entries, const unsigned char *ct, unsigned int y, unsigned char *sp)
  214. {
  215. unsigned int index = (info->image_top + y) * info->width + info->image_left;
  216. unsigned char *samples = fz_pixmap_samples(ctx, info->pix);
  217. unsigned char *dp = &samples[index * 4];
  218. unsigned char *mp = &info->mask[index];
  219. unsigned int x, k;
  220. if (info->image_top + y >= info->height)
  221. return;
  222. for (x = 0; x < info->image_width && info->image_left + x < info->width; x++, sp++, mp++, dp += 4)
  223. if (!info->has_transparency || *sp != info->transparent)
  224. {
  225. *mp = 0x02;
  226. for (k = 0; k < 3; k++)
  227. dp[k] = ct[fz_clampi(*sp, 0, ct_entries - 1) * 3 + k];
  228. dp[3] = 255;
  229. }
  230. else if (*mp == 0x01)
  231. *mp = 0x00;
  232. }
  233. static const unsigned char *
  234. gif_read_tbid(fz_context *ctx, struct info *info, const unsigned char *p, const unsigned char *end)
  235. {
  236. fz_stream *stm = NULL, *lzwstm = NULL;
  237. unsigned int mincodesize, y;
  238. fz_buffer *compressed = NULL, *uncompressed = NULL;
  239. const unsigned char *ct;
  240. unsigned char *sp;
  241. int ct_entries;
  242. if (end - p < 1)
  243. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in table based image data in gif image");
  244. mincodesize = *p;
  245. /* if there is no overlap, avoid pasting image data, just consume it */
  246. if (info->image_top >= info->height || info->image_left >= info->width)
  247. {
  248. p = gif_read_subblocks(ctx, info, p + 1, end, NULL);
  249. return p;
  250. }
  251. fz_var(compressed);
  252. fz_var(lzwstm);
  253. fz_var(stm);
  254. fz_var(uncompressed);
  255. fz_try(ctx)
  256. {
  257. compressed = fz_new_buffer(ctx, 0);
  258. p = gif_read_subblocks(ctx, info, p + 1, end, compressed);
  259. stm = fz_open_buffer(ctx, compressed);
  260. lzwstm = fz_open_lzwd(ctx, stm, 0, mincodesize + 1, 1, 1);
  261. uncompressed = fz_read_all(ctx, lzwstm, 0);
  262. if (uncompressed->len < (size_t)info->image_width * info->image_height)
  263. {
  264. fz_warn(ctx, "premature end in compressed table based image data in gif image");
  265. while (uncompressed->len < (size_t)info->image_width * info->image_height)
  266. fz_append_byte(ctx, uncompressed, 0x00);
  267. }
  268. if (info->has_lct)
  269. {
  270. ct = info->lct;
  271. ct_entries = info->lct_entries;
  272. }
  273. else if (info->has_gct)
  274. {
  275. ct = info->gct;
  276. ct_entries = info->gct_entries;
  277. }
  278. else
  279. {
  280. ct = dct;
  281. ct_entries = 256;
  282. }
  283. sp = uncompressed->data;
  284. if (info->image_interlaced)
  285. {
  286. for (y = 0; y < info->image_height; y += 8, sp += info->image_width)
  287. gif_read_line(ctx, info, ct_entries, ct, y, sp);
  288. for (y = 4; y < info->image_height; y += 8, sp += info->image_width)
  289. gif_read_line(ctx, info, ct_entries, ct, y, sp);
  290. for (y = 2; y < info->image_height; y += 4, sp += info->image_width)
  291. gif_read_line(ctx, info, ct_entries, ct, y, sp);
  292. for (y = 1; y < info->image_height; y += 2, sp += info->image_width)
  293. gif_read_line(ctx, info, ct_entries, ct, y, sp);
  294. }
  295. else
  296. for (y = 0; y < info->image_height; y++, sp += info->image_width)
  297. gif_read_line(ctx, info, ct_entries, ct, y, sp);
  298. }
  299. fz_always(ctx)
  300. {
  301. fz_drop_buffer(ctx, uncompressed);
  302. fz_drop_buffer(ctx, compressed);
  303. fz_drop_stream(ctx, lzwstm);
  304. fz_drop_stream(ctx, stm);
  305. }
  306. fz_catch(ctx)
  307. {
  308. fz_rethrow(ctx);
  309. }
  310. return p;
  311. }
  312. static const unsigned char *
  313. gif_read_gce(fz_context *ctx, struct info *info, const unsigned char *p, const unsigned char *end)
  314. {
  315. if (end - p < 8)
  316. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in graphic control extension in gif image");
  317. if (p[2] != 0x04)
  318. fz_throw(ctx, FZ_ERROR_FORMAT, "out of range graphic control extension block size in gif image");
  319. info->has_transparency = p[3] & 0x1;
  320. if (info->has_transparency)
  321. info->transparent = p[6];
  322. return p + 8;
  323. }
  324. static const unsigned char *
  325. gif_read_ce(fz_context *ctx, struct info *info, const unsigned char *p, const unsigned char *end)
  326. {
  327. return gif_read_subblocks(ctx, info, p + 2, end, NULL);
  328. }
  329. static const unsigned char*
  330. gif_read_pte(fz_context *ctx, struct info *info, const unsigned char *p, const unsigned char *end)
  331. {
  332. if (end - p < 15)
  333. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in plain text extension in gif image");
  334. if (p[2] != 0x0c)
  335. fz_throw(ctx, FZ_ERROR_FORMAT, "out of range plain text extension block size in gif image");
  336. return gif_read_subblocks(ctx, info, p + 15, end, NULL);
  337. }
  338. static const unsigned char *
  339. gif_read_icc(fz_context *ctx, struct info *info, const unsigned char *p, const unsigned char *end)
  340. {
  341. #if FZ_ENABLE_ICC
  342. fz_colorspace *icc = NULL;
  343. fz_buffer *buf = NULL;
  344. fz_var(p);
  345. buf = fz_new_buffer(ctx, 0);
  346. fz_try(ctx)
  347. {
  348. p = gif_read_subblocks(ctx, info, p, end, buf);
  349. icc = fz_new_icc_colorspace(ctx, FZ_COLORSPACE_RGB, 0, NULL, buf);
  350. fz_drop_colorspace(ctx, info->pix->colorspace);
  351. info->pix->colorspace = icc;
  352. }
  353. fz_always(ctx)
  354. fz_drop_buffer(ctx, buf);
  355. fz_catch(ctx)
  356. {
  357. fz_rethrow_if(ctx, FZ_ERROR_SYSTEM);
  358. fz_report_error(ctx);
  359. fz_warn(ctx, "ignoring embedded ICC profile in GIF");
  360. }
  361. return p;
  362. #else
  363. return gif_read_subblocks(ctx, info, p, end, NULL);
  364. #endif
  365. }
  366. /*
  367. NETSCAPE2.0
  368. http://odur.let.rug.nl/~kleiweg/gif/netscape.html
  369. http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension
  370. http://www.vurdalakov.net/misc/gif/netscape-buffering-application-extension
  371. https://code.google.com/p/gifdotnet/source/browse/src/GifDotNet/GifApplicationExtensionBlock.cs#95
  372. http://trac.webkit.org/browser/trunk/Source/WebCore/platform/image-decoders/gif/GIFImageReader.cpp#L617
  373. ANIMEXTS1.0
  374. http://www.vurdalakov.net/misc/gif/animexts-looping-application-extension
  375. https://code.google.com/p/gifdotnet/source/browse/src/GifDotNet/GifApplicationExtensionBlock.cs#95
  376. ICCRGBG1012
  377. http://www.color.org/icc1V42.pdf
  378. XMP DataXMP
  379. http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/xmp/pdfs/XMPSpecificationPart3.pdf
  380. fractint
  381. http://fractint.net/fractsvn/trunk/fractint/common/loadfile.c
  382. ZGATEXTI5 ZGATILEI5 ZGACTRLI5 ZGANPIMGI5
  383. ZGAVECTI5 ZGAALPHAI5 ZGATITLE4.0 ZGATEXTI4.0
  384. Zoner GIF animator 4.0 and 5.0
  385. */
  386. static const unsigned char *
  387. gif_read_ae(fz_context *ctx, struct info *info, const unsigned char *p, const unsigned char *end)
  388. {
  389. static char *ignorable[] = {
  390. "NETSACPE2.0", "ANIMEXTS1.0", "XMP DataXMP",
  391. "ZGATEXTI5\0\0", "ZGATILEI5\0\0", "ZGANPIMGI5\0", "ZGACTRLI5\0\0",
  392. "ZGAVECTI5\0\0", "ZGAALPHAI5\0", "ZGATITLE4.0", "ZGATEXTI4.0",
  393. };
  394. int i, ignored;
  395. if (end - p < 14)
  396. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in application extension in gif image");
  397. if (p[2] != 0x0b)
  398. fz_throw(ctx, FZ_ERROR_FORMAT, "out of range application extension block size in gif image");
  399. ignored = 0;
  400. for (i = 0; i < (int)nelem(ignorable); i++)
  401. ignored |= memcmp(&p[3], ignorable[i], 8 + 3);
  402. if (!ignored)
  403. {
  404. char extension[9];
  405. memmove(extension, &p[3], 8);
  406. extension[8] = '\0';
  407. fz_warn(ctx, "ignoring unsupported application extension '%s' in gif image", extension);
  408. }
  409. if (!memcmp(&p[3], "ICCRGBG1012", 11))
  410. return gif_read_icc(ctx, info, p + 14, end);
  411. return gif_read_subblocks(ctx, info, p + 14, end, NULL);
  412. }
  413. static void
  414. gif_mask_transparency(fz_context *ctx, struct info *info)
  415. {
  416. unsigned char *mp = info->mask;
  417. unsigned char *dp = fz_pixmap_samples(ctx, info->pix);
  418. unsigned int x, y;
  419. for (y = 0; y < info->height; y++)
  420. for (x = 0; x < info->width; x++, mp++, dp += 4)
  421. if (*mp == 0x00)
  422. dp[3] = 0;
  423. }
  424. static fz_pixmap *
  425. gif_read_image(fz_context *ctx, struct info *info, const unsigned char *p, size_t total, int only_metadata)
  426. {
  427. const unsigned char *end = p + total;
  428. memset(info, 0x00, sizeof (*info));
  429. /* Read header */
  430. p = gif_read_header(ctx, info, p, end);
  431. /* Read logical screen descriptor */
  432. p = gif_read_lsd(ctx, info, p, end);
  433. if (only_metadata)
  434. return NULL;
  435. info->pix = fz_new_pixmap(ctx, fz_device_rgb(ctx), info->width, info->height, NULL, 1);
  436. fz_try(ctx)
  437. {
  438. info->mask = fz_calloc(ctx, (size_t)info->width * info->height, 1);
  439. /* Read optional global color table */
  440. if (info->has_gct)
  441. {
  442. unsigned char *bp, *dp = fz_pixmap_samples(ctx, info->pix);
  443. unsigned int x, y, k;
  444. p = gif_read_gct(ctx, info, p, end);
  445. bp = &info->gct[info->gct_background * 3];
  446. memset(info->mask, 0x01, (size_t)info->width * info->height);
  447. for (y = 0; y < info->height; y++)
  448. for (x = 0; x < info->width; x++, dp += 4)
  449. {
  450. for (k = 0; k < 3; k++)
  451. dp[k] = bp[k];
  452. dp[3] = 255;
  453. }
  454. }
  455. while (1)
  456. {
  457. /* Read block indicator */
  458. if (end - p < 1)
  459. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end of block indicator in gif image");
  460. /* Read trailer */
  461. if (p[0] == 0x3b)
  462. {
  463. break;
  464. }
  465. /* Read extension */
  466. else if (p[0] == 0x21)
  467. {
  468. /* Read extension label */
  469. if (end - p < 2)
  470. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in extension label in gif image");
  471. if (p[1] == 0x01 && info->gif89a)
  472. {
  473. /* Read plain text extension */
  474. p = gif_read_pte(ctx, info, p, end);
  475. /* Graphic control extension applies only to the graphic rendering block following it */
  476. info->transparent = 0;
  477. info->has_transparency = 0;
  478. }
  479. else if (p[1] == 0xf9 && info->gif89a)
  480. /* Read graphic control extension */
  481. p = gif_read_gce(ctx, info, p, end);
  482. else if (p[1] == 0xfe && info->gif89a)
  483. /* Read comment extension */
  484. p = gif_read_ce(ctx, info, p, end);
  485. else if (p[1] == 0xff && info->gif89a)
  486. /* Read application extension */
  487. p = gif_read_ae(ctx, info, p, end);
  488. else
  489. {
  490. fz_warn(ctx, "ignoring unsupported extension label %02x in gif image", p[1]);
  491. p = gif_read_subblocks(ctx, info, p + 2, end, NULL);
  492. }
  493. }
  494. /* Read image descriptor */
  495. else if (p[0] == 0x2c)
  496. {
  497. p = gif_read_id(ctx, info, p, end);
  498. if (info->has_lct)
  499. /* Read local color table */
  500. p = gif_read_lct(ctx, info, p, end);
  501. /* Read table based image data */
  502. p = gif_read_tbid(ctx, info, p, end);
  503. /* Graphic control extension applies only to the graphic rendering block following it */
  504. info->transparent = 0;
  505. info->has_transparency = 0;
  506. /* Image descriptor applies only to the table based image data following it */
  507. info->image_left = info->image_top = 0;
  508. info->image_width = info->width;
  509. info->image_height = info->height;
  510. info->image_interlaced = 0;
  511. fz_free(ctx, info->lct);
  512. info->lct = NULL;
  513. info->has_lct = 0;
  514. }
  515. else
  516. fz_throw(ctx, FZ_ERROR_FORMAT, "unsupported block indicator %02x in gif image", p[0]);
  517. }
  518. gif_mask_transparency(ctx, info);
  519. fz_premultiply_pixmap(ctx, info->pix);
  520. }
  521. fz_always(ctx)
  522. {
  523. fz_free(ctx, info->mask);
  524. fz_free(ctx, info->lct);
  525. fz_free(ctx, info->gct);
  526. }
  527. fz_catch(ctx)
  528. {
  529. fz_drop_pixmap(ctx, info->pix);
  530. fz_rethrow(ctx);
  531. }
  532. return info->pix;
  533. }
  534. fz_pixmap *
  535. fz_load_gif(fz_context *ctx, const unsigned char *p, size_t total)
  536. {
  537. fz_pixmap *image;
  538. struct info gif;
  539. image = gif_read_image(ctx, &gif, p, total, 0);
  540. image->xres = gif.xres;
  541. image->yres = gif.yres;
  542. return image;
  543. }
  544. void
  545. fz_load_gif_info(fz_context *ctx, const unsigned char *p, size_t total, int *wp, int *hp, int *xresp, int *yresp, fz_colorspace **cspacep)
  546. {
  547. struct info gif;
  548. gif_read_image(ctx, &gif, p, total, 1);
  549. *cspacep = fz_keep_colorspace(ctx, fz_device_rgb(ctx));
  550. *wp = gif.width;
  551. *hp = gif.height;
  552. *xresp = gif.xres;
  553. *yresp = gif.yres;
  554. }