img_pam.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* PAM (P7): binary grayscale/RGB with optional alpha. */
  2. #include "img_internal.h"
  3. struct pam_hdr {
  4. unsigned int w, h, depth, maxval, data;
  5. };
  6. static int pam_line(const unsigned char *d, unsigned int n, unsigned int *pos,
  7. const unsigned char **start, unsigned int *len)
  8. {
  9. unsigned int p = *pos, s;
  10. if (p >= n)
  11. return -1;
  12. s = p;
  13. while (p < n && d[p] != '\n')
  14. p++;
  15. *start = d + s;
  16. *len = p - s;
  17. if (p < n)
  18. p++;
  19. *pos = p;
  20. if (*len && (*start)[*len - 1] == '\r')
  21. (*len)--;
  22. return 0;
  23. }
  24. static int pam_uint(const unsigned char *s, unsigned int len, unsigned int *v)
  25. {
  26. unsigned int i = 0, val = 0;
  27. int any = 0;
  28. while (i < len && (s[i] == ' ' || s[i] == '\t'))
  29. i++;
  30. while (i < len && s[i] >= '0' && s[i] <= '9') {
  31. val = val * 10 + (s[i] - '0');
  32. if (val > 65535)
  33. return -1;
  34. i++;
  35. any = 1;
  36. }
  37. if (!any)
  38. return -1;
  39. while (i < len && (s[i] == ' ' || s[i] == '\t'))
  40. i++;
  41. if (i != len)
  42. return -1;
  43. *v = val;
  44. return 0;
  45. }
  46. static int pam_key(const unsigned char *s, unsigned int len, const char *key)
  47. {
  48. unsigned int i = 0, k = 0;
  49. while (i < len && s[i] == ' ')
  50. i++;
  51. while (key[k]) {
  52. if (i >= len || s[i] != (unsigned char)key[k])
  53. return 0;
  54. i++;
  55. k++;
  56. }
  57. return i == len || s[i] == ' ' || s[i] == '\t';
  58. }
  59. static int pam_header(const unsigned char *d, unsigned int n, struct pam_hdr *p)
  60. {
  61. unsigned int pos = 2;
  62. int done = 0;
  63. p->w = p->h = p->depth = p->maxval = 0;
  64. while (pos < n) {
  65. const unsigned char *s;
  66. unsigned int len, v;
  67. if (pam_line(d, n, &pos, &s, &len))
  68. return -1;
  69. if (!len || s[0] == '#')
  70. continue;
  71. if (len == 6 && s[0] == 'E' && s[1] == 'N' && s[2] == 'D' &&
  72. s[3] == 'H' && s[4] == 'D' && s[5] == 'R') {
  73. done = 1;
  74. break;
  75. }
  76. if (pam_key(s, len, "WIDTH")) {
  77. if (pam_uint(s + 5, len - 5, &v))
  78. return -1;
  79. p->w = v;
  80. } else if (pam_key(s, len, "HEIGHT")) {
  81. if (pam_uint(s + 6, len - 6, &v))
  82. return -1;
  83. p->h = v;
  84. } else if (pam_key(s, len, "DEPTH")) {
  85. if (pam_uint(s + 5, len - 5, &v))
  86. return -1;
  87. p->depth = v;
  88. } else if (pam_key(s, len, "MAXVAL")) {
  89. if (pam_uint(s + 6, len - 6, &v))
  90. return -1;
  91. p->maxval = v;
  92. } else if (pam_key(s, len, "TUPLTYPE")) {
  93. /* The channel count from DEPTH is sufficient. */
  94. } else {
  95. return -1;
  96. }
  97. }
  98. if (!done || p->w == 0 || p->h == 0 || p->w > 32768 ||
  99. p->h > 32768 || p->depth < 1 || p->depth > 4 ||
  100. p->maxval == 0 || p->maxval > 65535)
  101. return -1;
  102. p->data = pos;
  103. return 0;
  104. }
  105. int img_sniff_pam(const unsigned char *d, unsigned int n, struct img_info *out)
  106. {
  107. struct pam_hdr p;
  108. if (pam_header(d, n, &p))
  109. return IMG_FMT_UNKNOWN;
  110. out->format = IMG_FMT_PAM;
  111. out->width = p.w;
  112. out->height = p.h;
  113. out->has_alpha = (p.depth == 2 || p.depth == 4);
  114. out->progressive = 0;
  115. return IMG_FMT_PAM;
  116. }
  117. static unsigned int pam_scale(unsigned int v, unsigned int mv)
  118. {
  119. if (mv == 255)
  120. return v;
  121. if (mv == 1)
  122. return v ? 255 : 0;
  123. return (v * 255u + mv / 2) / mv;
  124. }
  125. int img_decode_pam(const unsigned char *d, unsigned int n,
  126. const struct img_options *opt, struct img_mem *m,
  127. struct img_image *out)
  128. {
  129. struct pam_hdr p;
  130. unsigned int x, y, c;
  131. unsigned int step;
  132. int alpha, rcode;
  133. if (pam_header(d, n, &p))
  134. return IMG_ERR_FORMAT;
  135. if (!img_size_ok(p.w, p.h, opt))
  136. return IMG_ERR_TOOBIG;
  137. alpha = (p.depth == 2 || p.depth == 4);
  138. rcode = img_alloc(m, p.w, p.h, IMG_FMT_PAM, alpha, out);
  139. if (rcode)
  140. return rcode;
  141. step = p.maxval > 255 ? 2 : 1;
  142. for (y = 0; y < p.h; y++) {
  143. for (x = 0; x < p.w; x++) {
  144. unsigned int s[4] = { 0, 0, 0, 0 };
  145. unsigned int r, g, b, a = 255;
  146. for (c = 0; c < p.depth; c++) {
  147. if (p.data + step > n)
  148. return IMG_ERR_CORRUPT;
  149. if (step == 1) {
  150. s[c] = d[p.data++];
  151. } else {
  152. s[c] = img_be16(d + p.data);
  153. p.data += 2;
  154. }
  155. if (s[c] > p.maxval)
  156. return IMG_ERR_CORRUPT;
  157. s[c] = pam_scale(s[c], p.maxval);
  158. }
  159. if (p.depth <= 2) {
  160. r = g = b = s[0];
  161. if (p.depth == 2)
  162. a = s[1];
  163. } else {
  164. r = s[0];
  165. g = s[1];
  166. b = s[2];
  167. if (p.depth == 4)
  168. a = s[3];
  169. }
  170. out->px[y * p.w + x] = img_rgb565(r, g, b);
  171. if (out->alpha)
  172. out->alpha[y * p.w + x] = (unsigned char)a;
  173. }
  174. }
  175. return 0;
  176. }