3 * Todd Kirby <doubleshot@pacbell.net>
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.
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.
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
25 /* sgi image file signature */
28 #define SGI_HEADER_SIZE 512
30 #define SGI_GRAYSCALE 1
34 #define SGI_SINGLE_CHAN 2
35 #define SGI_MULTI_CHAN 3
37 typedef struct SGIInfo{
40 char bytes_per_channel;
41 unsigned short dimension;
48 static int sgi_probe(AVProbeData *pd)
50 /* test for sgi magic */
51 if (pd->buf_size >= 2 && BE_16(&pd->buf[0]) == SGI_MAGIC) {
52 return AVPROBE_SCORE_MAX;
58 /* read sgi header fields */
59 static void read_sgi_header(ByteIOContext *f, SGIInfo *info)
61 info->magic = (unsigned short) get_be16(f);
62 info->rle = get_byte(f);
63 info->bytes_per_channel = get_byte(f);
64 info->dimension = (unsigned short)get_be16(f);
65 info->xsize = (unsigned short) get_be16(f);
66 info->ysize = (unsigned short) get_be16(f);
67 info->zsize = (unsigned short) get_be16(f);
69 if(info->zsize > 4096)
73 printf("sgi header fields:\n");
74 printf(" magic: %d\n", info->magic);
75 printf(" rle: %d\n", info->rle);
76 printf(" bpc: %d\n", info->bytes_per_channel);
77 printf(" dim: %d\n", info->dimension);
78 printf(" xsize: %d\n", info->xsize);
79 printf(" ysize: %d\n", info->ysize);
80 printf(" zsize: %d\n", info->zsize);
87 /* read an uncompressed sgi image */
88 static int read_uncompressed_sgi(const SGIInfo *si,
89 AVPicture *pict, ByteIOContext *f)
91 int x, y, z, chan_offset, ret = 0;
95 url_fseek(f, SGI_HEADER_SIZE, SEEK_SET);
97 pict->linesize[0] = si->xsize;
99 for (z = 0; z < si->zsize; z++) {
101 #ifndef WORDS_BIGENDIAN
102 /* rgba -> bgra for rgba32 on little endian cpus */
103 if (si->zsize == 4 && z != 3)
109 for (y = si->ysize - 1; y >= 0; y--) {
110 dest_row = pict->data[0] + (y * si->xsize * si->zsize);
112 for (x = 0; x < si->xsize; x++) {
113 dest_row[chan_offset] = get_byte(f);
114 dest_row += si->zsize;
123 /* expand an rle row into a channel */
124 static int expand_rle_row(ByteIOContext *f, unsigned char *optr,
125 int chan_offset, int pixelstride)
127 unsigned char pixel, count;
130 #ifndef WORDS_BIGENDIAN
131 /* rgba -> bgra for rgba32 on little endian cpus */
132 if (pixelstride == 4 && chan_offset != 3) {
133 chan_offset = 2 - chan_offset;
142 if (!(count = (pixel & 0x7f))) {
164 /* read a run length encoded sgi image */
165 static int read_rle_sgi(const SGIInfo *sgi_info,
166 AVPicture *pict, ByteIOContext *f)
169 unsigned long *start_table;
170 int y, z, xsize, ysize, zsize, tablen;
174 xsize = sgi_info->xsize;
175 ysize = sgi_info->ysize;
176 zsize = sgi_info->zsize;
179 url_fseek(f, SGI_HEADER_SIZE, SEEK_SET);
181 /* size of rle offset and length tables */
182 tablen = ysize * zsize * sizeof(long);
184 start_table = (unsigned long *)av_malloc(tablen);
186 if (!get_buffer(f, (uint8_t *)start_table, tablen)) {
191 /* skip run length table */
192 url_fseek(f, tablen, SEEK_CUR);
194 for (z = 0; z < zsize; z++) {
195 for (y = 0; y < ysize; y++) {
196 dest_row = pict->data[0] + (ysize - 1 - y) * (xsize * zsize);
198 start_offset = BE_32(&start_table[y + z * ysize]);
200 /* don't seek if already at the next rle start offset */
201 if (url_ftell(f) != start_offset) {
202 url_fseek(f, start_offset, SEEK_SET);
205 if (expand_rle_row(f, dest_row, z, zsize) != xsize) {
206 ret = AVERROR_INVALIDDATA;
213 av_free(start_table);
219 static int sgi_read(ByteIOContext *f,
220 int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
222 SGIInfo sgi_info, *s = &sgi_info;
223 AVImageInfo info1, *info = &info1;
226 read_sgi_header(f, s);
228 if (s->bytes_per_channel != 1) {
229 return AVERROR_INVALIDDATA;
232 /* check for supported image dimensions */
233 if (s->dimension != 2 && s->dimension != 3) {
234 return AVERROR_INVALIDDATA;
237 if (s->zsize == SGI_GRAYSCALE) {
238 info->pix_fmt = PIX_FMT_GRAY8;
239 } else if (s->zsize == SGI_RGB) {
240 info->pix_fmt = PIX_FMT_RGB24;
241 } else if (s->zsize == SGI_RGBA) {
242 info->pix_fmt = PIX_FMT_RGBA32;
244 return AVERROR_INVALIDDATA;
247 info->width = s->xsize;
248 info->height = s->ysize;
250 ret = alloc_cb(opaque, info);
255 return read_rle_sgi(s, &info->pict, f);
257 return read_uncompressed_sgi(s, &info->pict, f);
260 return 0; /* not reached */
264 static void write_sgi_header(ByteIOContext *f, const SGIInfo *info)
268 put_be16(f, SGI_MAGIC);
269 put_byte(f, info->rle);
270 put_byte(f, info->bytes_per_channel);
271 put_be16(f, info->dimension);
272 put_be16(f, info->xsize);
273 put_be16(f, info->ysize);
274 put_be16(f, info->zsize);
276 /* The rest are constant in this implementation */
277 put_be32(f, 0L); /* pixmin */
278 put_be32(f, 255L); /* pixmax */
279 put_be32(f, 0L); /* dummy */
282 for (i = 0; i < 80; i++) {
286 put_be32(f, 0L); /* colormap */
288 /* The rest of the 512 byte header is unused. */
289 for (i = 0; i < 404; i++) {
295 static int rle_row(ByteIOContext *f, char *row, int stride, int rowsize)
297 int length, count, i, x;
298 char *start, repeat = 0;
300 for (x = rowsize, length = 0; x > 0;) {
305 while (x > 0 && (row[-2 * stride] != row[-1 * stride] ||
306 row[-1 * stride] != row[0])) {
314 count = (row - start) / stride;
316 i = count > 126 ? 126 : count;
319 put_byte(f, 0x80 | i);
340 while (x > 0 && *row == repeat) {
345 count = (row - start) / stride;
347 i = count > 126 ? 126 : count;
365 static int sgi_write(ByteIOContext *pb, AVImageInfo *info)
367 SGIInfo sgi_info, *si = &sgi_info;
368 long *offsettab, *lengthtab;
370 int tablesize, chan_offset;
373 si->xsize = info->width;
374 si->ysize = info->height;
376 si->bytes_per_channel = 1;
378 switch(info->pix_fmt) {
380 si->dimension = SGI_SINGLE_CHAN;
381 si->zsize = SGI_GRAYSCALE;
384 si->dimension = SGI_MULTI_CHAN;
388 si->dimension = SGI_MULTI_CHAN;
389 si->zsize = SGI_RGBA;
392 return AVERROR_INVALIDDATA;
395 write_sgi_header(pb, si);
397 tablesize = si->zsize * si->ysize * sizeof(long);
399 /* skip rle offset and length tables, write them at the end. */
400 url_fseek(pb, tablesize * 2, SEEK_CUR);
401 put_flush_packet(pb);
403 lengthtab = av_malloc(tablesize);
404 offsettab = av_malloc(tablesize);
406 for (z = 0; z < si->zsize; z++) {
408 #ifndef WORDS_BIGENDIAN
409 /* rgba -> bgra for rgba32 on little endian cpus */
410 if (si->zsize == 4 && z != 3)
416 srcrow = info->pict.data[0] + chan_offset;
418 for (y = si->ysize -1; y >= 0; y--) {
419 offsettab[(z * si->ysize) + y] = url_ftell(pb);
420 lengthtab[(z * si->ysize) + y] = rle_row(pb, srcrow,
421 si->zsize, si->xsize);
422 srcrow += info->pict.linesize[0];
426 url_fseek(pb, 512, SEEK_SET);
428 /* write offset table */
429 for (i = 0; i < (si->ysize * si->zsize); i++) {
430 put_be32(pb, offsettab[i]);
433 /* write length table */
434 for (i = 0; i < (si->ysize * si->zsize); i++) {
435 put_be32(pb, lengthtab[i]);
438 put_flush_packet(pb);
445 #endif // CONFIG_MUXERS
447 AVImageFormat sgi_image_format = {
452 (1 << PIX_FMT_GRAY8) | (1 << PIX_FMT_RGB24) | (1 << PIX_FMT_RGBA32),
457 #endif // CONFIG_MUXERS