img_ico.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* ICO/CUR: pick the largest icon; decode its PNG or BMP DIB payload. */
  2. #include "img_internal.h"
  3. struct ico_ent {
  4. unsigned int w, h, bpp, size, off;
  5. };
  6. static int ico_choose(const unsigned char *d, unsigned int n,
  7. struct ico_ent *best, unsigned int *type)
  8. {
  9. unsigned int count, i;
  10. if (n < 6 || d[0] != 0 || d[1] != 0)
  11. return -1;
  12. *type = img_le16(d + 2);
  13. if ((*type != 1 && *type != 2) || (count = img_le16(d + 4)) == 0 ||
  14. 6u + count * 16u > n)
  15. return -1;
  16. best->w = best->h = 0;
  17. best->bpp = best->size = best->off = 0;
  18. for (i = 0; i < count; i++) {
  19. const unsigned char *e = d + 6 + i * 16;
  20. unsigned int w = e[0] ? e[0] : 256;
  21. unsigned int h = e[1] ? e[1] : 256;
  22. unsigned int bpp = img_le16(e + 6);
  23. unsigned int size = img_le32(e + 8);
  24. unsigned int off = img_le32(e + 12);
  25. if (size == 0 || off > n || size > n - off)
  26. continue;
  27. if (w * h > best->w * best->h ||
  28. (w * h == best->w * best->h && bpp > best->bpp)) {
  29. best->w = w;
  30. best->h = h;
  31. best->bpp = bpp;
  32. best->size = size;
  33. best->off = off;
  34. }
  35. }
  36. return best->size ? 0 : -1;
  37. }
  38. static int ico_dib_info(const unsigned char *d, unsigned int n,
  39. unsigned int *w, unsigned int *h, unsigned int *bpp,
  40. unsigned int *dib, unsigned int *colors, int *topdown)
  41. {
  42. unsigned int height;
  43. if (n < 40 || img_le32(d) < 40)
  44. return -1;
  45. *w = img_le32(d + 4);
  46. height = img_le32(d + 8);
  47. *topdown = ((int)height < 0);
  48. height = *topdown ? (unsigned int)(-(int)height) : height;
  49. *h = height / 2;
  50. *bpp = img_le16(d + 14);
  51. *dib = img_le32(d);
  52. *colors = img_le32(d + 32);
  53. if (*w == 0 || *h == 0 || *w > 256 || *h > 256)
  54. return -1;
  55. if (*bpp != 1 && *bpp != 4 && *bpp != 8 && *bpp != 24 && *bpp != 32)
  56. return -1;
  57. return 0;
  58. }
  59. int img_sniff_ico(const unsigned char *d, unsigned int n, struct img_info *out)
  60. {
  61. struct ico_ent e;
  62. unsigned int type;
  63. const unsigned char *p;
  64. if (ico_choose(d, n, &e, &type))
  65. return IMG_FMT_UNKNOWN;
  66. p = d + e.off;
  67. out->format = IMG_FMT_ICO;
  68. out->has_alpha = 0;
  69. out->progressive = 0;
  70. if (e.size >= 8 && p[0] == 0x89 && p[1] == 'P' && p[2] == 'N' &&
  71. p[3] == 'G') {
  72. struct img_info sub;
  73. if (img_sniff_png(p, e.size, &sub) == IMG_FMT_UNKNOWN)
  74. return IMG_FMT_UNKNOWN;
  75. out->width = sub.width;
  76. out->height = sub.height;
  77. out->has_alpha = sub.has_alpha;
  78. return IMG_FMT_ICO;
  79. }
  80. {
  81. unsigned int w, h, bpp, dib, colors;
  82. int topdown;
  83. if (ico_dib_info(p, e.size, &w, &h, &bpp, &dib, &colors,
  84. &topdown))
  85. return IMG_FMT_UNKNOWN;
  86. (void)topdown;
  87. out->width = w;
  88. out->height = h;
  89. out->has_alpha = 1; /* the AND mask is always present */
  90. }
  91. return IMG_FMT_ICO;
  92. }
  93. int img_decode_ico(const unsigned char *d, unsigned int n,
  94. const struct img_options *opt, struct img_mem *m,
  95. struct img_image *out)
  96. {
  97. struct ico_ent e;
  98. unsigned int type, w, h, bpp, dib, colors, i;
  99. const unsigned char *p;
  100. if (ico_choose(d, n, &e, &type))
  101. return IMG_ERR_FORMAT;
  102. p = d + e.off;
  103. (void)type;
  104. if (e.size >= 8 && p[0] == 0x89 && p[1] == 'P' && p[2] == 'N' &&
  105. p[3] == 'G') {
  106. int r = img_decode_png(p, e.size, opt, m, out);
  107. if (r == 0)
  108. out->format = IMG_FMT_ICO;
  109. return r;
  110. }
  111. {
  112. int topdown;
  113. unsigned int pal_off, pal_n, row_stride, and_off, and_stride;
  114. unsigned int xor_off, x, y;
  115. unsigned char pal[256][3];
  116. unsigned int pal_a[256];
  117. if (ico_dib_info(p, e.size, &w, &h, &bpp, &dib, &colors,
  118. &topdown))
  119. return IMG_ERR_FORMAT;
  120. if (!img_size_ok(w, h, opt))
  121. return IMG_ERR_TOOBIG;
  122. pal_n = 0;
  123. if (bpp <= 8) {
  124. pal_n = colors ? colors : (1u << bpp);
  125. if (pal_n > 256)
  126. pal_n = 256;
  127. pal_off = dib;
  128. if (pal_off + pal_n * 4u > e.size)
  129. return IMG_ERR_CORRUPT;
  130. for (i = 0; i < pal_n; i++) {
  131. const unsigned char *q = p + pal_off + i * 4;
  132. pal[i][0] = q[2];
  133. pal[i][1] = q[1];
  134. pal[i][2] = q[0];
  135. pal_a[i] = 255;
  136. }
  137. }
  138. row_stride = ((w * bpp + 31u) / 32u) * 4u;
  139. xor_off = dib + (bpp <= 8 ? pal_n * 4u : 0);
  140. if (xor_off > e.size || row_stride > e.size - xor_off ||
  141. (unsigned long long)row_stride * h > e.size - xor_off)
  142. return IMG_ERR_CORRUPT;
  143. and_off = xor_off + row_stride * h;
  144. and_stride = ((w + 31u) / 32u) * 4u;
  145. {
  146. int r = img_alloc(m, w, h, IMG_FMT_ICO, 1, out);
  147. if (r)
  148. return r;
  149. }
  150. for (y = 0; y < h; y++) {
  151. const unsigned char *row = p + xor_off +
  152. (topdown ? y : (h - 1 - y)) * row_stride;
  153. for (x = 0; x < w; x++) {
  154. unsigned int rr = 0, gg = 0, bb = 0, aa = 255;
  155. unsigned int oi = y * w + x;
  156. if (bpp <= 8) {
  157. unsigned int idx;
  158. if (bpp == 8)
  159. idx = row[x];
  160. else if (bpp == 4)
  161. idx = (row[x / 2] >> ((x & 1) ? 0 : 4)) & 15;
  162. else
  163. idx = (row[x / 8] >> (7 - (x & 7))) & 1;
  164. if (idx >= pal_n)
  165. idx = 0;
  166. rr = pal[idx][0];
  167. gg = pal[idx][1];
  168. bb = pal[idx][2];
  169. aa = pal_a[idx];
  170. } else if (bpp == 24) {
  171. const unsigned char *q = row + x * 3;
  172. bb = q[0];
  173. gg = q[1];
  174. rr = q[2];
  175. } else {
  176. const unsigned char *q = row + x * 4;
  177. bb = q[0];
  178. gg = q[1];
  179. rr = q[2];
  180. aa = q[3];
  181. }
  182. if (and_off + and_stride * (y + 1) <= e.size) {
  183. const unsigned char *arow = p + and_off +
  184. (topdown ? y : (h - 1 - y)) * and_stride;
  185. if (arow[x / 8] & (0x80u >> (x & 7)))
  186. aa = 0;
  187. }
  188. out->px[oi] = img_rgb565(rr, gg, bb);
  189. out->alpha[oi] = (unsigned char)aa;
  190. }
  191. }
  192. }
  193. return 0;
  194. }