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.

456 lines
11KB

  1. /*
  2. * SGI image format
  3. * Todd Kirby <doubleshot@pacbell.net>
  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. #include "avio.h"
  21. /* #define DEBUG */
  22. /* sgi image file signature */
  23. #define SGI_MAGIC 474
  24. #define SGI_HEADER_SIZE 512
  25. #define SGI_GRAYSCALE 1
  26. #define SGI_RGB 3
  27. #define SGI_RGBA 4
  28. #define SGI_SINGLE_CHAN 2
  29. #define SGI_MULTI_CHAN 3
  30. typedef struct SGIInfo{
  31. short magic;
  32. char rle;
  33. char bytes_per_channel;
  34. unsigned short dimension;
  35. unsigned short xsize;
  36. unsigned short ysize;
  37. unsigned short zsize;
  38. } SGIInfo;
  39. static int sgi_probe(AVProbeData *pd)
  40. {
  41. /* test for sgi magic */
  42. if (pd->buf_size >= 2 && BE_16(&pd->buf[0]) == SGI_MAGIC) {
  43. return AVPROBE_SCORE_MAX;
  44. } else {
  45. return 0;
  46. }
  47. }
  48. /* read sgi header fields */
  49. static void read_sgi_header(ByteIOContext *f, SGIInfo *info)
  50. {
  51. info->magic = (unsigned short) get_be16(f);
  52. info->rle = get_byte(f);
  53. info->bytes_per_channel = get_byte(f);
  54. info->dimension = (unsigned short)get_be16(f);
  55. info->xsize = (unsigned short) get_be16(f);
  56. info->ysize = (unsigned short) get_be16(f);
  57. info->zsize = (unsigned short) get_be16(f);
  58. #ifdef DEBUG
  59. printf("sgi header fields:\n");
  60. printf(" magic: %d\n", info->magic);
  61. printf(" rle: %d\n", info->rle);
  62. printf(" bpc: %d\n", info->bytes_per_channel);
  63. printf(" dim: %d\n", info->dimension);
  64. printf(" xsize: %d\n", info->xsize);
  65. printf(" ysize: %d\n", info->ysize);
  66. printf(" zsize: %d\n", info->zsize);
  67. #endif
  68. return;
  69. }
  70. /* read an uncompressed sgi image */
  71. static int read_uncompressed_sgi(const SGIInfo *si,
  72. AVPicture *pict, ByteIOContext *f)
  73. {
  74. int x, y, z, chan_offset, ret = 0;
  75. uint8_t *dest_row;
  76. /* skip header */
  77. url_fseek(f, SGI_HEADER_SIZE, SEEK_SET);
  78. pict->linesize[0] = si->xsize;
  79. for (z = 0; z < si->zsize; z++) {
  80. #ifndef WORDS_BIGENDIAN
  81. /* rgba -> bgra for rgba32 on little endian cpus */
  82. if (si->zsize == 4 && z != 3)
  83. chan_offset = 2 - z;
  84. else
  85. #endif
  86. chan_offset = z;
  87. for (y = si->ysize - 1; y >= 0; y--) {
  88. dest_row = pict->data[0] + (y * si->xsize * si->zsize);
  89. for (x = 0; x < si->xsize; x++) {
  90. dest_row[chan_offset] = get_byte(f);
  91. dest_row += si->zsize;
  92. }
  93. }
  94. }
  95. return ret;
  96. }
  97. /* expand an rle row into a channel */
  98. static int expand_rle_row(ByteIOContext *f, unsigned char *optr,
  99. int chan_offset, int pixelstride)
  100. {
  101. unsigned char pixel, count;
  102. int length = 0;
  103. #ifndef WORDS_BIGENDIAN
  104. /* rgba -> bgra for rgba32 on little endian cpus */
  105. if (pixelstride == 4 && chan_offset != 3) {
  106. chan_offset = 2 - chan_offset;
  107. }
  108. #endif
  109. optr += chan_offset;
  110. while (1) {
  111. pixel = get_byte(f);
  112. if (!(count = (pixel & 0x7f))) {
  113. return length;
  114. }
  115. if (pixel & 0x80) {
  116. while (count--) {
  117. *optr = get_byte(f);
  118. length++;
  119. optr += pixelstride;
  120. }
  121. } else {
  122. pixel = get_byte(f);
  123. while (count--) {
  124. *optr = pixel;
  125. length++;
  126. optr += pixelstride;
  127. }
  128. }
  129. }
  130. }
  131. /* read a run length encoded sgi image */
  132. static int read_rle_sgi(const SGIInfo *sgi_info,
  133. AVPicture *pict, ByteIOContext *f)
  134. {
  135. uint8_t *dest_row;
  136. unsigned long *start_table;
  137. int y, z, xsize, ysize, zsize, tablen;
  138. long start_offset;
  139. int ret = 0;
  140. xsize = sgi_info->xsize;
  141. ysize = sgi_info->ysize;
  142. zsize = sgi_info->zsize;
  143. /* skip header */
  144. url_fseek(f, SGI_HEADER_SIZE, SEEK_SET);
  145. /* size of rle offset and length tables */
  146. tablen = ysize * zsize * sizeof(long);
  147. start_table = (unsigned long *)av_malloc(tablen);
  148. if (!get_buffer(f, (uint8_t *)start_table, tablen)) {
  149. ret = AVERROR_IO;
  150. goto fail;
  151. }
  152. /* skip run length table */
  153. url_fseek(f, tablen, SEEK_CUR);
  154. for (z = 0; z < zsize; z++) {
  155. for (y = 0; y < ysize; y++) {
  156. dest_row = pict->data[0] + (ysize - 1 - y) * (xsize * zsize);
  157. start_offset = BE_32(&start_table[y + z * ysize]);
  158. /* don't seek if already at the next rle start offset */
  159. if (url_ftell(f) != start_offset) {
  160. url_fseek(f, start_offset, SEEK_SET);
  161. }
  162. if (expand_rle_row(f, dest_row, z, zsize) != xsize) {
  163. ret = AVERROR_INVALIDDATA;
  164. goto fail;
  165. }
  166. }
  167. }
  168. fail:
  169. av_free(start_table);
  170. return ret;
  171. }
  172. static int sgi_read(ByteIOContext *f,
  173. int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
  174. {
  175. SGIInfo sgi_info, *s = &sgi_info;
  176. AVImageInfo info1, *info = &info1;
  177. int ret;
  178. read_sgi_header(f, s);
  179. if (s->bytes_per_channel != 1) {
  180. return AVERROR_INVALIDDATA;
  181. }
  182. /* check for supported image dimensions */
  183. if (s->dimension != 2 && s->dimension != 3) {
  184. return AVERROR_INVALIDDATA;
  185. }
  186. if (s->zsize == SGI_GRAYSCALE) {
  187. info->pix_fmt = PIX_FMT_GRAY8;
  188. } else if (s->zsize == SGI_RGB) {
  189. info->pix_fmt = PIX_FMT_RGB24;
  190. } else if (s->zsize == SGI_RGBA) {
  191. info->pix_fmt = PIX_FMT_RGBA32;
  192. } else {
  193. return AVERROR_INVALIDDATA;
  194. }
  195. info->width = s->xsize;
  196. info->height = s->ysize;
  197. ret = alloc_cb(opaque, info);
  198. if (ret)
  199. return ret;
  200. if (s->rle) {
  201. return read_rle_sgi(s, &info->pict, f);
  202. } else {
  203. return read_uncompressed_sgi(s, &info->pict, f);
  204. }
  205. return 0; /* not reached */
  206. }
  207. #ifdef CONFIG_ENCODERS
  208. static void write_sgi_header(ByteIOContext *f, const SGIInfo *info)
  209. {
  210. int i;
  211. put_be16(f, SGI_MAGIC);
  212. put_byte(f, info->rle);
  213. put_byte(f, info->bytes_per_channel);
  214. put_be16(f, info->dimension);
  215. put_be16(f, info->xsize);
  216. put_be16(f, info->ysize);
  217. put_be16(f, info->zsize);
  218. /* The rest are constant in this implementation */
  219. put_be32(f, 0L); /* pixmin */
  220. put_be32(f, 255L); /* pixmax */
  221. put_be32(f, 0L); /* dummy */
  222. /* name */
  223. for (i = 0; i < 80; i++) {
  224. put_byte(f, 0);
  225. }
  226. put_be32(f, 0L); /* colormap */
  227. /* The rest of the 512 byte header is unused. */
  228. for (i = 0; i < 404; i++) {
  229. put_byte(f, 0);
  230. }
  231. }
  232. static int rle_row(ByteIOContext *f, char *row, int stride, int rowsize)
  233. {
  234. int length, count, i, x;
  235. char *start, repeat = 0;
  236. for (x = rowsize, length = 0; x > 0;) {
  237. start = row;
  238. row += (2 * stride);
  239. x -= 2;
  240. while (x > 0 && (row[-2 * stride] != row[-1 * stride] ||
  241. row[-1 * stride] != row[0])) {
  242. row += stride;
  243. x--;
  244. };
  245. row -= (2 * stride);
  246. x += 2;
  247. count = (row - start) / stride;
  248. while (count > 0) {
  249. i = count > 126 ? 126 : count;
  250. count -= i;
  251. put_byte(f, 0x80 | i);
  252. length++;
  253. while (i > 0) {
  254. put_byte(f, *start);
  255. start += stride;
  256. i--;
  257. length++;
  258. };
  259. };
  260. if (x <= 0) {
  261. break;
  262. }
  263. start = row;
  264. repeat = row[0];
  265. row += stride;
  266. x--;
  267. while (x > 0 && *row == repeat) {
  268. row += stride;
  269. x--;
  270. };
  271. count = (row - start) / stride;
  272. while (count > 0) {
  273. i = count > 126 ? 126 : count;
  274. count -= i;
  275. put_byte(f, i);
  276. length++;
  277. put_byte(f, repeat);
  278. length++;
  279. };
  280. };
  281. length++;
  282. put_byte(f, 0);
  283. return (length);
  284. }
  285. static int sgi_write(ByteIOContext *pb, AVImageInfo *info)
  286. {
  287. SGIInfo sgi_info, *si = &sgi_info;
  288. long *offsettab, *lengthtab;
  289. int i, y, z;
  290. int tablesize, chan_offset;
  291. uint8_t *srcrow;
  292. si->xsize = info->width;
  293. si->ysize = info->height;
  294. si->rle = 1;
  295. si->bytes_per_channel = 1;
  296. switch(info->pix_fmt) {
  297. case PIX_FMT_GRAY8:
  298. si->dimension = SGI_SINGLE_CHAN;
  299. si->zsize = SGI_GRAYSCALE;
  300. break;
  301. case PIX_FMT_RGB24:
  302. si->dimension = SGI_MULTI_CHAN;
  303. si->zsize = SGI_RGB;
  304. break;
  305. case PIX_FMT_RGBA32:
  306. si->dimension = SGI_MULTI_CHAN;
  307. si->zsize = SGI_RGBA;
  308. break;
  309. default:
  310. return AVERROR_INVALIDDATA;
  311. }
  312. write_sgi_header(pb, si);
  313. tablesize = si->zsize * si->ysize * sizeof(long);
  314. /* skip rle offset and length tables, write them at the end. */
  315. url_fseek(pb, tablesize * 2, SEEK_CUR);
  316. put_flush_packet(pb);
  317. lengthtab = av_malloc(tablesize);
  318. offsettab = av_malloc(tablesize);
  319. for (z = 0; z < si->zsize; z++) {
  320. #ifndef WORDS_BIGENDIAN
  321. /* rgba -> bgra for rgba32 on little endian cpus */
  322. if (si->zsize == 4 && z != 3)
  323. chan_offset = 2 - z;
  324. else
  325. #endif
  326. chan_offset = z;
  327. srcrow = info->pict.data[0] + chan_offset;
  328. for (y = si->ysize -1; y >= 0; y--) {
  329. offsettab[(z * si->ysize) + y] = url_ftell(pb);
  330. lengthtab[(z * si->ysize) + y] = rle_row(pb, srcrow,
  331. si->zsize, si->xsize);
  332. srcrow += info->pict.linesize[0];
  333. }
  334. }
  335. url_fseek(pb, 512, SEEK_SET);
  336. /* write offset table */
  337. for (i = 0; i < (si->ysize * si->zsize); i++) {
  338. put_be32(pb, offsettab[i]);
  339. }
  340. /* write length table */
  341. for (i = 0; i < (si->ysize * si->zsize); i++) {
  342. put_be32(pb, lengthtab[i]);
  343. }
  344. put_flush_packet(pb);
  345. av_free(lengthtab);
  346. av_free(offsettab);
  347. return 0;
  348. }
  349. #endif // CONFIG_ENCODERS
  350. AVImageFormat sgi_image_format = {
  351. "sgi",
  352. "sgi,rgb,rgba,bw",
  353. sgi_probe,
  354. sgi_read,
  355. (1 << PIX_FMT_GRAY8) | (1 << PIX_FMT_RGB24) | (1 << PIX_FMT_RGBA32),
  356. #ifdef CONFIG_ENCODERS
  357. sgi_write,
  358. #else
  359. NULL,
  360. #endif // CONFIG_ENCODERS
  361. };