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.

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