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.

286 lines
6.8KB

  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 *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 pnm_probe(AVProbeData *pd)
  206. {
  207. const char *p = pd->buf;
  208. if (pd->buf_size >= 8 &&
  209. p[0] == 'P' &&
  210. p[1] >= '4' && p[1] <= '6' &&
  211. p[2] == '\n')
  212. return AVPROBE_SCORE_MAX;
  213. else
  214. return 0;
  215. }
  216. static int pgmyuv_probe(AVProbeData *pd)
  217. {
  218. if (match_ext(pd->filename, "pgmyuv"))
  219. return AVPROBE_SCORE_MAX;
  220. else
  221. return 0;
  222. }
  223. AVImageFormat pnm_image_format = {
  224. "pnm",
  225. NULL,
  226. pnm_probe,
  227. pnm_read,
  228. 0,
  229. NULL,
  230. };
  231. AVImageFormat pbm_image_format = {
  232. "pbm",
  233. "pbm",
  234. NULL,
  235. NULL,
  236. (1 << PIX_FMT_MONOWHITE),
  237. pnm_write,
  238. };
  239. AVImageFormat pgm_image_format = {
  240. "pgm",
  241. "pgm",
  242. NULL,
  243. NULL,
  244. (1 << PIX_FMT_GRAY8),
  245. pnm_write,
  246. };
  247. AVImageFormat ppm_image_format = {
  248. "ppm",
  249. "ppm",
  250. NULL,
  251. NULL,
  252. (1 << PIX_FMT_RGB24),
  253. pnm_write,
  254. };
  255. AVImageFormat pgmyuv_image_format = {
  256. "pgmyuv",
  257. NULL,
  258. pgmyuv_probe,
  259. pgmyuv_read,
  260. (1 << PIX_FMT_YUV420P),
  261. pnm_write,
  262. };