annotate sgi.c @ 1212:b4f48eb1f411 libavformat

avoid reading beyond packet size
author bcoudurier
date Tue, 01 Aug 2006 09:56:27 +0000
parents edbe5c3717f9
children 0899bfe4105c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
1 /*
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
2 * SGI image format
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
3 * Todd Kirby <doubleshot@pacbell.net>
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
4 *
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
9 *
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
13 * Lesser General Public License for more details.
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
14 *
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
896
edbe5c3717f9 Update licensing information: The FSF changed postal address.
diego
parents: 885
diff changeset
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
18 */
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
19
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
20 #include "avformat.h"
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
21 #include "avio.h"
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
22
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
23 /* #define DEBUG */
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
24
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
25 /* sgi image file signature */
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
26 #define SGI_MAGIC 474
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
27
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
28 #define SGI_HEADER_SIZE 512
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
29
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
30 #define SGI_GRAYSCALE 1
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
31 #define SGI_RGB 3
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
32 #define SGI_RGBA 4
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
33
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
34 #define SGI_SINGLE_CHAN 2
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
35 #define SGI_MULTI_CHAN 3
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
36
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
37 typedef struct SGIInfo{
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
38 short magic;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
39 char rle;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
40 char bytes_per_channel;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
41 unsigned short dimension;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
42 unsigned short xsize;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
43 unsigned short ysize;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
44 unsigned short zsize;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
45 } SGIInfo;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
46
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
47
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
48 static int sgi_probe(AVProbeData *pd)
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
49 {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
50 /* test for sgi magic */
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
51 if (pd->buf_size >= 2 && BE_16(&pd->buf[0]) == SGI_MAGIC) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
52 return AVPROBE_SCORE_MAX;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
53 } else {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
54 return 0;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
55 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
56 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
57
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
58 /* read sgi header fields */
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
59 static void read_sgi_header(ByteIOContext *f, SGIInfo *info)
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
60 {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
61 info->magic = (unsigned short) get_be16(f);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
62 info->rle = get_byte(f);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
63 info->bytes_per_channel = get_byte(f);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
64 info->dimension = (unsigned short)get_be16(f);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
65 info->xsize = (unsigned short) get_be16(f);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
66 info->ysize = (unsigned short) get_be16(f);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
67 info->zsize = (unsigned short) get_be16(f);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
68
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
69 if(info->zsize > 4096)
639
0b52743104ac integer overflows, heap corruption
michael
parents: 430
diff changeset
70 info->zsize= 0;
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
71
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
72 #ifdef DEBUG
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
73 printf("sgi header fields:\n");
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
74 printf(" magic: %d\n", info->magic);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
75 printf(" rle: %d\n", info->rle);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
76 printf(" bpc: %d\n", info->bytes_per_channel);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
77 printf(" dim: %d\n", info->dimension);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
78 printf(" xsize: %d\n", info->xsize);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
79 printf(" ysize: %d\n", info->ysize);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
80 printf(" zsize: %d\n", info->zsize);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
81 #endif
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
82
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
83 return;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
84 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
85
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
86
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
87 /* read an uncompressed sgi image */
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
88 static int read_uncompressed_sgi(const SGIInfo *si,
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
89 AVPicture *pict, ByteIOContext *f)
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
90 {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
91 int x, y, z, chan_offset, ret = 0;
430
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
92 uint8_t *dest_row;
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
93
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
94 /* skip header */
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
95 url_fseek(f, SGI_HEADER_SIZE, SEEK_SET);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
96
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
97 pict->linesize[0] = si->xsize;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
98
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
99 for (z = 0; z < si->zsize; z++) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
100
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
101 #ifndef WORDS_BIGENDIAN
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
102 /* rgba -> bgra for rgba32 on little endian cpus */
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
103 if (si->zsize == 4 && z != 3)
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
104 chan_offset = 2 - z;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
105 else
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
106 #endif
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
107 chan_offset = z;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
108
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
109 for (y = si->ysize - 1; y >= 0; y--) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
110 dest_row = pict->data[0] + (y * si->xsize * si->zsize);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
111
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
112 for (x = 0; x < si->xsize; x++) {
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
113 dest_row[chan_offset] = get_byte(f);
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
114 dest_row += si->zsize;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
115 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
116 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
117 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
118
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
119 return ret;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
120 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
121
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
122
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
123 /* expand an rle row into a channel */
430
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
124 static int expand_rle_row(ByteIOContext *f, unsigned char *optr,
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
125 int chan_offset, int pixelstride)
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
126 {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
127 unsigned char pixel, count;
430
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
128 int length = 0;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
129
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
130 #ifndef WORDS_BIGENDIAN
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
131 /* rgba -> bgra for rgba32 on little endian cpus */
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
132 if (pixelstride == 4 && chan_offset != 3) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
133 chan_offset = 2 - chan_offset;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
134 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
135 #endif
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
136
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
137 optr += chan_offset;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
138
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
139 while (1) {
430
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
140 pixel = get_byte(f);
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
141
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
142 if (!(count = (pixel & 0x7f))) {
430
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
143 return length;
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
144 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
145 if (pixel & 0x80) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
146 while (count--) {
430
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
147 *optr = get_byte(f);
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
148 length++;
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
149 optr += pixelstride;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
150 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
151 } else {
430
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
152 pixel = get_byte(f);
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
153
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
154 while (count--) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
155 *optr = pixel;
430
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
156 length++;
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
157 optr += pixelstride;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
158 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
159 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
160 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
161 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
162
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
163
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
164 /* read a run length encoded sgi image */
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
165 static int read_rle_sgi(const SGIInfo *sgi_info,
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
166 AVPicture *pict, ByteIOContext *f)
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
167 {
430
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
168 uint8_t *dest_row;
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
169 unsigned long *start_table;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
170 int y, z, xsize, ysize, zsize, tablen;
430
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
171 long start_offset;
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
172 int ret = 0;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
173
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
174 xsize = sgi_info->xsize;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
175 ysize = sgi_info->ysize;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
176 zsize = sgi_info->zsize;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
177
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
178 /* skip header */
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
179 url_fseek(f, SGI_HEADER_SIZE, SEEK_SET);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
180
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
181 /* size of rle offset and length tables */
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
182 tablen = ysize * zsize * sizeof(long);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
183
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
184 start_table = (unsigned long *)av_malloc(tablen);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
185
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
186 if (!get_buffer(f, (uint8_t *)start_table, tablen)) {
430
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
187 ret = AVERROR_IO;
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
188 goto fail;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
189 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
190
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
191 /* skip run length table */
430
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
192 url_fseek(f, tablen, SEEK_CUR);
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
193
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
194 for (z = 0; z < zsize; z++) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
195 for (y = 0; y < ysize; y++) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
196 dest_row = pict->data[0] + (ysize - 1 - y) * (xsize * zsize);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
197
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
198 start_offset = BE_32(&start_table[y + z * ysize]);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
199
430
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
200 /* don't seek if already at the next rle start offset */
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
201 if (url_ftell(f) != start_offset) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
202 url_fseek(f, start_offset, SEEK_SET);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
203 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
204
430
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
205 if (expand_rle_row(f, dest_row, z, zsize) != xsize) {
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
206 ret = AVERROR_INVALIDDATA;
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
207 goto fail;
153985f24e5b patch courtesy of Todd Kirby:
melanson
parents: 386
diff changeset
208 }
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
209 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
210 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
211
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
212 fail:
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
213 av_free(start_table);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
214
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
215 return ret;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
216 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
217
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
218
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
219 static int sgi_read(ByteIOContext *f,
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
220 int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
221 {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
222 SGIInfo sgi_info, *s = &sgi_info;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
223 AVImageInfo info1, *info = &info1;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
224 int ret;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
225
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
226 read_sgi_header(f, s);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
227
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
228 if (s->bytes_per_channel != 1) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
229 return AVERROR_INVALIDDATA;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
230 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
231
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
232 /* check for supported image dimensions */
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
233 if (s->dimension != 2 && s->dimension != 3) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
234 return AVERROR_INVALIDDATA;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
235 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
236
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
237 if (s->zsize == SGI_GRAYSCALE) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
238 info->pix_fmt = PIX_FMT_GRAY8;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
239 } else if (s->zsize == SGI_RGB) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
240 info->pix_fmt = PIX_FMT_RGB24;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
241 } else if (s->zsize == SGI_RGBA) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
242 info->pix_fmt = PIX_FMT_RGBA32;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
243 } else {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
244 return AVERROR_INVALIDDATA;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
245 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
246
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
247 info->width = s->xsize;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
248 info->height = s->ysize;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
249
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
250 ret = alloc_cb(opaque, info);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
251 if (ret)
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
252 return ret;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
253
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
254 if (s->rle) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
255 return read_rle_sgi(s, &info->pict, f);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
256 } else {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
257 return read_uncompressed_sgi(s, &info->pict, f);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
258 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
259
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
260 return 0; /* not reached */
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
261 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
262
858
66cc656ea404 Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents: 639
diff changeset
263 #ifdef CONFIG_MUXERS
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
264 static void write_sgi_header(ByteIOContext *f, const SGIInfo *info)
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
265 {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
266 int i;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
267
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
268 put_be16(f, SGI_MAGIC);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
269 put_byte(f, info->rle);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
270 put_byte(f, info->bytes_per_channel);
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
271 put_be16(f, info->dimension);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
272 put_be16(f, info->xsize);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
273 put_be16(f, info->ysize);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
274 put_be16(f, info->zsize);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
275
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
276 /* The rest are constant in this implementation */
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
277 put_be32(f, 0L); /* pixmin */
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
278 put_be32(f, 255L); /* pixmax */
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
279 put_be32(f, 0L); /* dummy */
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
280
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
281 /* name */
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
282 for (i = 0; i < 80; i++) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
283 put_byte(f, 0);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
284 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
285
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
286 put_be32(f, 0L); /* colormap */
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
287
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
288 /* The rest of the 512 byte header is unused. */
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
289 for (i = 0; i < 404; i++) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
290 put_byte(f, 0);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
291 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
292 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
293
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
294
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
295 static int rle_row(ByteIOContext *f, char *row, int stride, int rowsize)
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
296 {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
297 int length, count, i, x;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
298 char *start, repeat = 0;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
299
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
300 for (x = rowsize, length = 0; x > 0;) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
301 start = row;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
302 row += (2 * stride);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
303 x -= 2;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
304
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
305 while (x > 0 && (row[-2 * stride] != row[-1 * stride] ||
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
306 row[-1 * stride] != row[0])) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
307 row += stride;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
308 x--;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
309 };
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
310
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
311 row -= (2 * stride);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
312 x += 2;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
313
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
314 count = (row - start) / stride;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
315 while (count > 0) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
316 i = count > 126 ? 126 : count;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
317 count -= i;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
318
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
319 put_byte(f, 0x80 | i);
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
320 length++;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
321
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
322 while (i > 0) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
323 put_byte(f, *start);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
324 start += stride;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
325 i--;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
326 length++;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
327 };
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
328 };
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
329
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
330 if (x <= 0) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
331 break;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
332 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
333
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
334 start = row;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
335 repeat = row[0];
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
336
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
337 row += stride;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
338 x--;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
339
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
340 while (x > 0 && *row == repeat) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
341 row += stride;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
342 x--;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
343 };
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
344
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
345 count = (row - start) / stride;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
346 while (count > 0) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
347 i = count > 126 ? 126 : count;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
348 count -= i;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
349
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
350 put_byte(f, i);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
351 length++;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
352
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
353 put_byte(f, repeat);
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
354 length++;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
355 };
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
356 };
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
357
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
358 length++;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
359
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
360 put_byte(f, 0);
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
361 return (length);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
362 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
363
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
364
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
365 static int sgi_write(ByteIOContext *pb, AVImageInfo *info)
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
366 {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
367 SGIInfo sgi_info, *si = &sgi_info;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
368 long *offsettab, *lengthtab;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
369 int i, y, z;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
370 int tablesize, chan_offset;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
371 uint8_t *srcrow;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
372
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
373 si->xsize = info->width;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
374 si->ysize = info->height;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
375 si->rle = 1;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
376 si->bytes_per_channel = 1;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
377
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
378 switch(info->pix_fmt) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
379 case PIX_FMT_GRAY8:
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
380 si->dimension = SGI_SINGLE_CHAN;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
381 si->zsize = SGI_GRAYSCALE;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
382 break;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
383 case PIX_FMT_RGB24:
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
384 si->dimension = SGI_MULTI_CHAN;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
385 si->zsize = SGI_RGB;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
386 break;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
387 case PIX_FMT_RGBA32:
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
388 si->dimension = SGI_MULTI_CHAN;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
389 si->zsize = SGI_RGBA;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
390 break;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
391 default:
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
392 return AVERROR_INVALIDDATA;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
393 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
394
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
395 write_sgi_header(pb, si);
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
396
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
397 tablesize = si->zsize * si->ysize * sizeof(long);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
398
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
399 /* skip rle offset and length tables, write them at the end. */
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
400 url_fseek(pb, tablesize * 2, SEEK_CUR);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
401 put_flush_packet(pb);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
402
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
403 lengthtab = av_malloc(tablesize);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
404 offsettab = av_malloc(tablesize);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
405
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
406 for (z = 0; z < si->zsize; z++) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
407
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
408 #ifndef WORDS_BIGENDIAN
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
409 /* rgba -> bgra for rgba32 on little endian cpus */
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
410 if (si->zsize == 4 && z != 3)
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
411 chan_offset = 2 - z;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
412 else
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
413 #endif
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
414 chan_offset = z;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
415
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
416 srcrow = info->pict.data[0] + chan_offset;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
417
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
418 for (y = si->ysize -1; y >= 0; y--) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
419 offsettab[(z * si->ysize) + y] = url_ftell(pb);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
420 lengthtab[(z * si->ysize) + y] = rle_row(pb, srcrow,
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
421 si->zsize, si->xsize);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
422 srcrow += info->pict.linesize[0];
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
423 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
424 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
425
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
426 url_fseek(pb, 512, SEEK_SET);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
427
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
428 /* write offset table */
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
429 for (i = 0; i < (si->ysize * si->zsize); i++) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
430 put_be32(pb, offsettab[i]);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
431 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
432
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
433 /* write length table */
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
434 for (i = 0; i < (si->ysize * si->zsize); i++) {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
435 put_be32(pb, lengthtab[i]);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
436 }
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
437
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
438 put_flush_packet(pb);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
439
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
440 av_free(lengthtab);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
441 av_free(offsettab);
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
442
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
443 return 0;
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
444 }
858
66cc656ea404 Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents: 639
diff changeset
445 #endif // CONFIG_MUXERS
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
446
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
447 AVImageFormat sgi_image_format = {
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
448 "sgi",
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
449 "sgi,rgb,rgba,bw",
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
450 sgi_probe,
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
451 sgi_read,
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 858
diff changeset
452 (1 << PIX_FMT_GRAY8) | (1 << PIX_FMT_RGB24) | (1 << PIX_FMT_RGBA32),
858
66cc656ea404 Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents: 639
diff changeset
453 #ifdef CONFIG_MUXERS
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
454 sgi_write,
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
455 #else
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
456 NULL,
858
66cc656ea404 Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents: 639
diff changeset
457 #endif // CONFIG_MUXERS
382
37a29b5200d8 added SGI image format, encoding and decoding, courtesy of Todd Kirby
melanson
parents:
diff changeset
458 };