img.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Image library core: format sniffing, decode dispatch, arena image
  2. * allocation and error/format names. See img.h. */
  3. #include "img_internal.h"
  4. int img_size_ok(unsigned int w, unsigned int h,
  5. const struct img_options *opt)
  6. {
  7. unsigned long long n;
  8. if (w == 0 || h == 0 || w > 32768 || h > 32768)
  9. return 0;
  10. n = (unsigned long long)w * h;
  11. if (n > 0x3FFFFFFFull)
  12. return 0;
  13. if (opt && opt->max_pixels && n > opt->max_pixels)
  14. return 0;
  15. return 1;
  16. }
  17. int img_alloc(struct img_mem *m, unsigned int w, unsigned int h,
  18. unsigned int format, int alpha, struct img_image *out)
  19. {
  20. unsigned long long n = (unsigned long long)w * h;
  21. unsigned short *px;
  22. unsigned char *a = 0;
  23. if (!m || !out || n == 0 || n > 0x3FFFFFFFull)
  24. return IMG_ERR_TOOBIG;
  25. px = (unsigned short *)img_mem_alloc(m, (unsigned int)(n * 2), 16);
  26. if (!px)
  27. return IMG_ERR_NOMEM;
  28. if (alpha) {
  29. a = (unsigned char *)img_mem_alloc(m, (unsigned int)n, 16);
  30. if (!a)
  31. return IMG_ERR_NOMEM;
  32. }
  33. out->format = format;
  34. out->w = w;
  35. out->h = h;
  36. out->px = px;
  37. out->alpha = a;
  38. out->flags = alpha ? IMG_FLAG_ALPHA : 0;
  39. return 0;
  40. }
  41. static int png_sig(const unsigned char *d, unsigned int n)
  42. {
  43. static const unsigned char sig[8] = {
  44. 0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A
  45. };
  46. unsigned int i;
  47. if (n < 8)
  48. return 0;
  49. for (i = 0; i < 8; i++)
  50. if (d[i] != sig[i])
  51. return 0;
  52. return 1;
  53. }
  54. int img_sniff(const void *data, unsigned int len, struct img_info *out)
  55. {
  56. const unsigned char *d = (const unsigned char *)data;
  57. int fmt;
  58. if (!out)
  59. return IMG_ERR_FORMAT;
  60. out->format = IMG_FMT_UNKNOWN;
  61. out->width = out->height = 0;
  62. out->has_alpha = 0;
  63. if (!d || len == 0)
  64. return IMG_ERR_EMPTY;
  65. if (png_sig(d, len))
  66. fmt = img_sniff_png(d, len, out);
  67. else if (len >= 3 && d[0] == 0xFF && d[1] == 0xD8 && d[2] == 0xFF)
  68. fmt = img_sniff_jpeg(d, len, out);
  69. else if (len >= 6 && d[0] == 'G' && d[1] == 'I' && d[2] == 'F' &&
  70. d[3] == '8' && (d[4] == '7' || d[4] == '9') && d[5] == 'a')
  71. fmt = img_sniff_gif(d, len, out);
  72. else if (len >= 2 && d[0] == 'B' && d[1] == 'M')
  73. fmt = img_sniff_bmp(d, len, out);
  74. else if (len >= 2 && d[0] == 'P' && d[1] >= '1' && d[1] <= '6')
  75. fmt = img_sniff_pnm(d, len, out);
  76. else
  77. fmt = img_sniff_tga(d, len, out); /* no magic: strict parse */
  78. if (fmt == IMG_FMT_UNKNOWN)
  79. return IMG_ERR_FORMAT;
  80. return 0;
  81. }
  82. int img_decode(const void *data, unsigned int len,
  83. const struct img_options *opt, struct img_mem *m,
  84. struct img_image *out)
  85. {
  86. const unsigned char *d = (const unsigned char *)data;
  87. struct img_info info;
  88. int r;
  89. if (!d || len == 0)
  90. return IMG_ERR_EMPTY;
  91. if (!m || !out)
  92. return IMG_ERR_NOMEM;
  93. r = img_sniff(d, len, &info);
  94. if (r)
  95. return r;
  96. if (!img_size_ok(info.width, info.height, opt))
  97. return IMG_ERR_TOOBIG;
  98. switch (info.format) {
  99. case IMG_FMT_PNG:
  100. r = img_decode_png(d, len, opt, m, out);
  101. break;
  102. case IMG_FMT_JPEG:
  103. r = img_decode_jpeg(d, len, opt, m, out);
  104. break;
  105. case IMG_FMT_GIF:
  106. r = img_decode_gif(d, len, opt, m, out);
  107. break;
  108. case IMG_FMT_BMP:
  109. r = img_decode_bmp(d, len, opt, m, out);
  110. break;
  111. case IMG_FMT_TGA:
  112. r = img_decode_tga(d, len, opt, m, out);
  113. break;
  114. case IMG_FMT_PNM:
  115. r = img_decode_pnm(d, len, opt, m, out);
  116. break;
  117. default:
  118. r = IMG_ERR_UNSUPPORTED;
  119. break;
  120. }
  121. if (r == 0 && (!out->px || out->w != info.width ||
  122. out->h != info.height))
  123. return IMG_ERR_CORRUPT;
  124. return r;
  125. }
  126. unsigned int img_estimate(const struct img_info *info)
  127. {
  128. unsigned long long px, bytes;
  129. if (!info || !info->width || !info->height)
  130. return 2u * 1024u * 1024u;
  131. px = (unsigned long long)info->width * info->height;
  132. bytes = px * 2; /* RGB565 output */
  133. if (info->has_alpha)
  134. bytes += px; /* alpha plane */
  135. if (info->format == IMG_FMT_JPEG)
  136. bytes += px * 3; /* worst case: 4:4:4 planes */
  137. bytes += 2u * 1024u * 1024u; /* inflate/row/Huffman scratch */
  138. if (bytes > 0xFFFFFFFFull)
  139. return 0xFFFFFFFFu;
  140. return (unsigned int)bytes;
  141. }
  142. const char *img_format_name(unsigned int format)
  143. {
  144. switch (format) {
  145. case IMG_FMT_PNG: return "PNG";
  146. case IMG_FMT_JPEG: return "JPEG";
  147. case IMG_FMT_GIF: return "GIF";
  148. case IMG_FMT_BMP: return "BMP";
  149. case IMG_FMT_TGA: return "TGA";
  150. case IMG_FMT_PNM: return "PNM";
  151. case IMG_FMT_ICO: return "ICO";
  152. default: return "image";
  153. }
  154. }
  155. const char *img_error_string(int err)
  156. {
  157. switch (err) {
  158. case IMG_ERR_IO: return "Cannot read the file";
  159. case IMG_ERR_FORMAT: return "Unknown image format";
  160. case IMG_ERR_CORRUPT: return "Image data is corrupt or truncated";
  161. case IMG_ERR_NOMEM: return "Not enough memory for the image";
  162. case IMG_ERR_TOOBIG: return "Image is too large";
  163. case IMG_ERR_UNSUPPORTED: return "Image variant is not supported yet";
  164. case IMG_ERR_EMPTY: return "The file is empty";
  165. default: return "Cannot decode the image";
  166. }
  167. }