You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

477 lines
12KB

  1. /*
  2. * PNM image format
  3. * Copyright (c) 2002, 2003 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. static inline int pnm_space(int c)
  21. {
  22. return (c == ' ' || c == '\n' || c == '\r' || c == '\t');
  23. }
  24. static void pnm_get(ByteIOContext *f, char *str, int buf_size)
  25. {
  26. char *s;
  27. int c;
  28. /* skip spaces and comments */
  29. for(;;) {
  30. c = url_fgetc(f);
  31. if (c == '#') {
  32. do {
  33. c = url_fgetc(f);
  34. } while (c != '\n' && c != URL_EOF);
  35. } else if (!pnm_space(c)) {
  36. break;
  37. }
  38. }
  39. s = str;
  40. while (c != URL_EOF && !pnm_space(c)) {
  41. if ((s - str) < buf_size - 1)
  42. *s++ = c;
  43. c = url_fgetc(f);
  44. }
  45. *s = '\0';
  46. }
  47. static int pnm_read1(ByteIOContext *f,
  48. int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque,
  49. int allow_yuv)
  50. {
  51. int i, n, linesize, h;
  52. char buf1[32];
  53. unsigned char *ptr;
  54. AVImageInfo info1, *info = &info1;
  55. int ret;
  56. pnm_get(f, buf1, sizeof(buf1));
  57. if (!strcmp(buf1, "P4")) {
  58. info->pix_fmt = PIX_FMT_MONOWHITE;
  59. } else if (!strcmp(buf1, "P5")) {
  60. if (allow_yuv)
  61. info->pix_fmt = PIX_FMT_YUV420P;
  62. else
  63. info->pix_fmt = PIX_FMT_GRAY8;
  64. } else if (!strcmp(buf1, "P6")) {
  65. info->pix_fmt = PIX_FMT_RGB24;
  66. } else {
  67. return AVERROR_INVALIDDATA;
  68. }
  69. pnm_get(f, buf1, sizeof(buf1));
  70. info->width = atoi(buf1);
  71. if (info->width <= 0)
  72. return AVERROR_INVALIDDATA;
  73. pnm_get(f, buf1, sizeof(buf1));
  74. info->height = atoi(buf1);
  75. if (info->height <= 0)
  76. return AVERROR_INVALIDDATA;
  77. if (info->pix_fmt != PIX_FMT_MONOWHITE) {
  78. pnm_get(f, buf1, sizeof(buf1));
  79. }
  80. /* more check if YUV420 */
  81. if (info->pix_fmt == PIX_FMT_YUV420P) {
  82. if ((info->width & 1) != 0)
  83. return AVERROR_INVALIDDATA;
  84. h = (info->height * 2);
  85. if ((h % 3) != 0)
  86. return AVERROR_INVALIDDATA;
  87. h /= 3;
  88. info->height = h;
  89. }
  90. ret = alloc_cb(opaque, info);
  91. if (ret)
  92. return ret;
  93. switch(info->pix_fmt) {
  94. default:
  95. return AVERROR_INVALIDDATA;
  96. case PIX_FMT_RGB24:
  97. n = info->width * 3;
  98. goto do_read;
  99. case PIX_FMT_GRAY8:
  100. n = info->width;
  101. goto do_read;
  102. case PIX_FMT_MONOWHITE:
  103. n = (info->width + 7) >> 3;
  104. do_read:
  105. ptr = info->pict.data[0];
  106. linesize = info->pict.linesize[0];
  107. for(i = 0; i < info->height; i++) {
  108. get_buffer(f, ptr, n);
  109. ptr += linesize;
  110. }
  111. break;
  112. case PIX_FMT_YUV420P:
  113. {
  114. unsigned char *ptr1, *ptr2;
  115. n = info->width;
  116. ptr = info->pict.data[0];
  117. linesize = info->pict.linesize[0];
  118. for(i = 0; i < info->height; i++) {
  119. get_buffer(f, ptr, n);
  120. ptr += linesize;
  121. }
  122. ptr1 = info->pict.data[1];
  123. ptr2 = info->pict.data[2];
  124. n >>= 1;
  125. h = info->height >> 1;
  126. for(i = 0; i < h; i++) {
  127. get_buffer(f, ptr1, n);
  128. get_buffer(f, ptr2, n);
  129. ptr1 += info->pict.linesize[1];
  130. ptr2 += info->pict.linesize[2];
  131. }
  132. }
  133. break;
  134. }
  135. return 0;
  136. }
  137. static int pnm_read(ByteIOContext *f,
  138. int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
  139. {
  140. return pnm_read1(f, alloc_cb, opaque, 0);
  141. }
  142. static int pgmyuv_read(ByteIOContext *f,
  143. int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
  144. {
  145. return pnm_read1(f, alloc_cb, opaque, 1);
  146. }
  147. static int pnm_write(ByteIOContext *pb, AVImageInfo *info)
  148. {
  149. int i, h, h1, c, n, linesize;
  150. char buf[100];
  151. uint8_t *ptr, *ptr1, *ptr2;
  152. h = info->height;
  153. h1 = h;
  154. switch(info->pix_fmt) {
  155. case PIX_FMT_MONOWHITE:
  156. c = '4';
  157. n = (info->width + 7) >> 3;
  158. break;
  159. case PIX_FMT_GRAY8:
  160. c = '5';
  161. n = info->width;
  162. break;
  163. case PIX_FMT_RGB24:
  164. c = '6';
  165. n = info->width * 3;
  166. break;
  167. case PIX_FMT_YUV420P:
  168. c = '5';
  169. n = info->width;
  170. h1 = (h * 3) / 2;
  171. break;
  172. default:
  173. return AVERROR_INVALIDDATA;
  174. }
  175. snprintf(buf, sizeof(buf),
  176. "P%c\n%d %d\n",
  177. c, info->width, h1);
  178. put_buffer(pb, buf, strlen(buf));
  179. if (info->pix_fmt != PIX_FMT_MONOWHITE) {
  180. snprintf(buf, sizeof(buf),
  181. "%d\n", 255);
  182. put_buffer(pb, buf, strlen(buf));
  183. }
  184. ptr = info->pict.data[0];
  185. linesize = info->pict.linesize[0];
  186. for(i=0;i<h;i++) {
  187. put_buffer(pb, ptr, n);
  188. ptr += linesize;
  189. }
  190. if (info->pix_fmt == PIX_FMT_YUV420P) {
  191. h >>= 1;
  192. n >>= 1;
  193. ptr1 = info->pict.data[1];
  194. ptr2 = info->pict.data[2];
  195. for(i=0;i<h;i++) {
  196. put_buffer(pb, ptr1, n);
  197. put_buffer(pb, ptr2, n);
  198. ptr1 += info->pict.linesize[1];
  199. ptr2 += info->pict.linesize[2];
  200. }
  201. }
  202. put_flush_packet(pb);
  203. return 0;
  204. }
  205. static int pam_read(ByteIOContext *f,
  206. int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
  207. {
  208. int i, n, linesize, h, w, depth, maxval;
  209. char buf1[32], tuple_type[32];
  210. unsigned char *ptr;
  211. AVImageInfo info1, *info = &info1;
  212. int ret;
  213. pnm_get(f, buf1, sizeof(buf1));
  214. if (strcmp(buf1, "P7") != 0)
  215. return AVERROR_INVALIDDATA;
  216. w = -1;
  217. h = -1;
  218. maxval = -1;
  219. depth = -1;
  220. tuple_type[0] = '\0';
  221. for(;;) {
  222. pnm_get(f, buf1, sizeof(buf1));
  223. if (!strcmp(buf1, "WIDTH")) {
  224. pnm_get(f, buf1, sizeof(buf1));
  225. w = strtol(buf1, NULL, 10);
  226. } else if (!strcmp(buf1, "HEIGHT")) {
  227. pnm_get(f, buf1, sizeof(buf1));
  228. h = strtol(buf1, NULL, 10);
  229. } else if (!strcmp(buf1, "DEPTH")) {
  230. pnm_get(f, buf1, sizeof(buf1));
  231. depth = strtol(buf1, NULL, 10);
  232. } else if (!strcmp(buf1, "MAXVAL")) {
  233. pnm_get(f, buf1, sizeof(buf1));
  234. maxval = strtol(buf1, NULL, 10);
  235. } else if (!strcmp(buf1, "TUPLETYPE")) {
  236. pnm_get(f, buf1, sizeof(buf1));
  237. pstrcpy(tuple_type, sizeof(tuple_type), buf1);
  238. } else if (!strcmp(buf1, "ENDHDR")) {
  239. break;
  240. } else {
  241. return AVERROR_INVALIDDATA;
  242. }
  243. }
  244. /* check that all tags are present */
  245. if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0')
  246. return AVERROR_INVALIDDATA;
  247. info->width = w;
  248. info->height = h;
  249. if (depth == 1) {
  250. if (maxval == 1)
  251. info->pix_fmt = PIX_FMT_MONOWHITE;
  252. else
  253. info->pix_fmt = PIX_FMT_GRAY8;
  254. } else if (depth == 3) {
  255. info->pix_fmt = PIX_FMT_RGB24;
  256. } else if (depth == 4) {
  257. info->pix_fmt = PIX_FMT_RGBA32;
  258. } else {
  259. return AVERROR_INVALIDDATA;
  260. }
  261. ret = alloc_cb(opaque, info);
  262. if (ret)
  263. return ret;
  264. switch(info->pix_fmt) {
  265. default:
  266. return AVERROR_INVALIDDATA;
  267. case PIX_FMT_RGB24:
  268. n = info->width * 3;
  269. goto do_read;
  270. case PIX_FMT_GRAY8:
  271. n = info->width;
  272. goto do_read;
  273. case PIX_FMT_MONOWHITE:
  274. n = (info->width + 7) >> 3;
  275. do_read:
  276. ptr = info->pict.data[0];
  277. linesize = info->pict.linesize[0];
  278. for(i = 0; i < info->height; i++) {
  279. get_buffer(f, ptr, n);
  280. ptr += linesize;
  281. }
  282. break;
  283. case PIX_FMT_RGBA32:
  284. ptr = info->pict.data[0];
  285. linesize = info->pict.linesize[0];
  286. for(i = 0; i < info->height; i++) {
  287. int j, r, g, b, a;
  288. for(j = 0;j < w; j++) {
  289. r = get_byte(f);
  290. g = get_byte(f);
  291. b = get_byte(f);
  292. a = get_byte(f);
  293. ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
  294. }
  295. ptr += linesize;
  296. }
  297. break;
  298. }
  299. return 0;
  300. }
  301. static int pam_write(ByteIOContext *pb, AVImageInfo *info)
  302. {
  303. int i, h, w, n, linesize, depth, maxval;
  304. const char *tuple_type;
  305. char buf[100];
  306. uint8_t *ptr;
  307. h = info->height;
  308. w = info->width;
  309. switch(info->pix_fmt) {
  310. case PIX_FMT_MONOWHITE:
  311. n = (info->width + 7) >> 3;
  312. depth = 1;
  313. maxval = 1;
  314. tuple_type = "BLACKANDWHITE";
  315. break;
  316. case PIX_FMT_GRAY8:
  317. n = info->width;
  318. depth = 1;
  319. maxval = 255;
  320. tuple_type = "GRAYSCALE";
  321. break;
  322. case PIX_FMT_RGB24:
  323. n = info->width * 3;
  324. depth = 3;
  325. maxval = 255;
  326. tuple_type = "RGB";
  327. break;
  328. case PIX_FMT_RGBA32:
  329. n = info->width * 4;
  330. depth = 4;
  331. maxval = 255;
  332. tuple_type = "RGB_ALPHA";
  333. break;
  334. default:
  335. return AVERROR_INVALIDDATA;
  336. }
  337. snprintf(buf, sizeof(buf),
  338. "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLETYPE %s\nENDHDR\n",
  339. w, h, depth, maxval, tuple_type);
  340. put_buffer(pb, buf, strlen(buf));
  341. ptr = info->pict.data[0];
  342. linesize = info->pict.linesize[0];
  343. if (info->pix_fmt == PIX_FMT_RGBA32) {
  344. int j;
  345. unsigned int v;
  346. for(i=0;i<h;i++) {
  347. for(j=0;j<w;j++) {
  348. v = ((uint32_t *)ptr)[j];
  349. put_byte(pb, (v >> 16) & 0xff);
  350. put_byte(pb, (v >> 8) & 0xff);
  351. put_byte(pb, (v) & 0xff);
  352. put_byte(pb, (v >> 24) & 0xff);
  353. }
  354. ptr += linesize;
  355. }
  356. } else {
  357. for(i=0;i<h;i++) {
  358. put_buffer(pb, ptr, n);
  359. ptr += linesize;
  360. }
  361. }
  362. put_flush_packet(pb);
  363. return 0;
  364. }
  365. static int pnm_probe(AVProbeData *pd)
  366. {
  367. const char *p = pd->buf;
  368. if (pd->buf_size >= 8 &&
  369. p[0] == 'P' &&
  370. p[1] >= '4' && p[1] <= '6' &&
  371. pnm_space(p[2]) )
  372. return AVPROBE_SCORE_MAX - 1; /* to permit pgmyuv probe */
  373. else
  374. return 0;
  375. }
  376. static int pgmyuv_probe(AVProbeData *pd)
  377. {
  378. if (match_ext(pd->filename, "pgmyuv"))
  379. return AVPROBE_SCORE_MAX;
  380. else
  381. return 0;
  382. }
  383. static int pam_probe(AVProbeData *pd)
  384. {
  385. const char *p = pd->buf;
  386. if (pd->buf_size >= 8 &&
  387. p[0] == 'P' &&
  388. p[1] == '7' &&
  389. p[2] == '\n')
  390. return AVPROBE_SCORE_MAX;
  391. else
  392. return 0;
  393. }
  394. AVImageFormat pnm_image_format = {
  395. "pnm",
  396. NULL,
  397. pnm_probe,
  398. pnm_read,
  399. 0,
  400. NULL,
  401. };
  402. AVImageFormat pbm_image_format = {
  403. "pbm",
  404. "pbm",
  405. NULL,
  406. NULL,
  407. (1 << PIX_FMT_MONOWHITE),
  408. pnm_write,
  409. };
  410. AVImageFormat pgm_image_format = {
  411. "pgm",
  412. "pgm",
  413. NULL,
  414. NULL,
  415. (1 << PIX_FMT_GRAY8),
  416. pnm_write,
  417. };
  418. AVImageFormat ppm_image_format = {
  419. "ppm",
  420. "ppm",
  421. NULL,
  422. NULL,
  423. (1 << PIX_FMT_RGB24),
  424. pnm_write,
  425. };
  426. AVImageFormat pam_image_format = {
  427. "pam",
  428. "pam",
  429. pam_probe,
  430. pam_read,
  431. (1 << PIX_FMT_MONOWHITE) | (1 << PIX_FMT_GRAY8) | (1 << PIX_FMT_RGB24) |
  432. (1 << PIX_FMT_RGBA32),
  433. pam_write,
  434. };
  435. AVImageFormat pgmyuv_image_format = {
  436. "pgmyuv",
  437. "pgmyuv",
  438. pgmyuv_probe,
  439. pgmyuv_read,
  440. (1 << PIX_FMT_YUV420P),
  441. pnm_write,
  442. };