Mercurial > libavcodec.hg
annotate targa.c @ 4653:1d88da25349b libavcodec
Combine the window and overlap loops,
and get rid of the data swapping.
Patch by Ian Braithwaite <dk dot braithwaite at ian>
author | banan |
---|---|
date | Tue, 13 Mar 2007 19:44:02 +0000 |
parents | ce643a22f049 |
children | 2b72f9bc4f06 |
rev | line source |
---|---|
3986 | 1 /* |
2 * Targa (.tga) image decoder | |
3 * Copyright (c) 2006 Konstantin Shishkov | |
4 * | |
5 * This file is part of FFmpeg. | |
6 * | |
7 * FFmpeg is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU Lesser General Public | |
9 * License as published by the Free Software Foundation; either | |
10 * version 2.1 of the License, or (at your option) any later version. | |
11 * | |
12 * FFmpeg is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
18 * License along with FFmpeg; if not, write to the Free Software | |
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 * | |
21 */ | |
22 #include "avcodec.h" | |
23 | |
24 enum TargaCompr{ | |
25 TGA_NODATA = 0, // no image data | |
26 TGA_PAL = 1, // palettized | |
27 TGA_RGB = 2, // true-color | |
28 TGA_BW = 3, // black & white or grayscale | |
29 TGA_RLE = 8, // flag pointing that data is RLE-coded | |
30 }; | |
31 | |
32 typedef struct TargaContext { | |
33 AVFrame picture; | |
34 | |
35 int width, height; | |
36 int bpp; | |
37 int color_type; | |
38 int compression_type; | |
39 } TargaContext; | |
40 | |
41 static void targa_decode_rle(AVCodecContext *avctx, TargaContext *s, uint8_t *src, uint8_t *dst, int w, int h, int stride, int bpp) | |
42 { | |
43 int i, x, y; | |
44 int depth = (bpp + 1) >> 3; | |
45 int type, count; | |
46 int diff; | |
47 | |
48 diff = stride - w * depth; | |
49 x = y = 0; | |
50 while(y < h){ | |
51 type = *src++; | |
52 count = (type & 0x7F) + 1; | |
53 type &= 0x80; | |
54 if((x + count > w) && (x + count + 1 > (h - y) * w)){ | |
55 av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count); | |
56 return; | |
57 } | |
58 for(i = 0; i < count; i++){ | |
59 switch(depth){ | |
60 case 1: | |
61 *dst = *src; | |
62 break; | |
63 case 2: | |
4364 | 64 *((uint16_t*)dst) = AV_RL16(src); |
3986 | 65 break; |
66 case 3: | |
67 dst[0] = src[0]; | |
68 dst[1] = src[1]; | |
69 dst[2] = src[2]; | |
70 break; | |
4132 | 71 case 4: |
4364 | 72 *((uint32_t*)dst) = AV_RL32(src); |
4132 | 73 break; |
3986 | 74 } |
75 dst += depth; | |
76 if(!type) | |
77 src += depth; | |
78 | |
79 x++; | |
80 if(x == w){ | |
81 x = 0; | |
82 y++; | |
83 dst += diff; | |
84 } | |
85 } | |
86 if(type) | |
87 src += depth; | |
88 } | |
89 } | |
90 | |
91 static int decode_frame(AVCodecContext *avctx, | |
92 void *data, int *data_size, | |
93 uint8_t *buf, int buf_size) | |
94 { | |
95 TargaContext * const s = avctx->priv_data; | |
96 AVFrame *picture = data; | |
97 AVFrame * const p= (AVFrame*)&s->picture; | |
98 uint8_t *dst; | |
99 int stride; | |
100 int idlen, pal, compr, x, y, w, h, bpp, flags; | |
101 int first_clr, colors, csize; | |
102 | |
103 /* parse image header */ | |
104 idlen = *buf++; | |
105 pal = *buf++; | |
106 compr = *buf++; | |
4364 | 107 first_clr = AV_RL16(buf); buf += 2; |
108 colors = AV_RL16(buf); buf += 2; | |
3986 | 109 csize = *buf++; |
4364 | 110 x = AV_RL16(buf); buf += 2; |
111 y = AV_RL16(buf); buf += 2; | |
112 w = AV_RL16(buf); buf += 2; | |
113 h = AV_RL16(buf); buf += 2; | |
3986 | 114 bpp = *buf++; |
115 flags = *buf++; | |
116 //skip identifier if any | |
117 buf += idlen; | |
118 s->bpp = bpp; | |
119 s->width = w; | |
120 s->height = h; | |
121 switch(s->bpp){ | |
122 case 8: | |
123 avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8; | |
124 break; | |
125 case 15: | |
126 avctx->pix_fmt = PIX_FMT_RGB555; | |
127 break; | |
128 case 16: | |
129 avctx->pix_fmt = PIX_FMT_RGB555; | |
130 break; | |
131 case 24: | |
132 avctx->pix_fmt = PIX_FMT_BGR24; | |
133 break; | |
4132 | 134 case 32: |
4494
ce643a22f049
Replace deprecated PIX_FMT names by the newer variants.
diego
parents:
4364
diff
changeset
|
135 avctx->pix_fmt = PIX_FMT_RGB32; |
4132 | 136 break; |
3986 | 137 default: |
4128 | 138 av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp); |
3986 | 139 return -1; |
140 } | |
141 | |
142 if(s->picture.data[0]) | |
143 avctx->release_buffer(avctx, &s->picture); | |
144 | |
145 if(avcodec_check_dimensions(avctx, w, h)) | |
146 return -1; | |
147 if(w != avctx->width || h != avctx->height) | |
148 avcodec_set_dimensions(avctx, w, h); | |
149 if(avctx->get_buffer(avctx, p) < 0){ | |
150 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
151 return -1; | |
152 } | |
153 if(flags & 0x20){ | |
154 dst = p->data[0]; | |
155 stride = p->linesize[0]; | |
156 }else{ //image is upside-down | |
157 dst = p->data[0] + p->linesize[0] * (h - 1); | |
158 stride = -p->linesize[0]; | |
159 } | |
160 | |
161 if(avctx->pix_fmt == PIX_FMT_PAL8 && avctx->palctrl){ | |
162 memcpy(p->data[1], avctx->palctrl->palette, AVPALETTE_SIZE); | |
163 if(avctx->palctrl->palette_changed){ | |
164 p->palette_has_changed = 1; | |
165 avctx->palctrl->palette_changed = 0; | |
166 } | |
167 } | |
168 if(colors){ | |
169 if((colors + first_clr) > 256){ | |
170 av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr); | |
171 return -1; | |
172 } | |
173 if(csize != 24){ | |
174 av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize); | |
175 return -1; | |
176 } | |
177 if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway | |
178 buf += colors * ((csize + 1) >> 3); | |
179 else{ | |
180 int r, g, b, t; | |
181 int32_t *pal = ((int32_t*)p->data[1]) + first_clr; | |
182 for(t = 0; t < colors; t++){ | |
183 r = *buf++; | |
184 g = *buf++; | |
185 b = *buf++; | |
186 *pal++ = (b << 16) | (g << 8) | r; | |
187 } | |
188 p->palette_has_changed = 1; | |
189 avctx->palctrl->palette_changed = 0; | |
190 } | |
191 } | |
192 if((compr & (~TGA_RLE)) == TGA_NODATA) | |
193 memset(p->data[0], 0, p->linesize[0] * s->height); | |
194 else{ | |
195 if(compr & TGA_RLE) | |
196 targa_decode_rle(avctx, s, buf, dst, avctx->width, avctx->height, stride, bpp); | |
197 else{ | |
198 for(y = 0; y < s->height; y++){ | |
199 #ifdef WORDS_BIGENDIAN | |
200 if((s->bpp + 1) >> 3 == 2){ | |
201 uint16_t *dst16 = (uint16_t*)dst; | |
202 for(x = 0; x < s->width; x++) | |
4364 | 203 dst16[x] = AV_RL16(buf + x * 2); |
4132 | 204 }else if((s->bpp + 1) >> 3 == 4){ |
205 uint32_t *dst32 = (uint32_t*)dst; | |
206 for(x = 0; x < s->width; x++) | |
4364 | 207 dst32[x] = AV_RL32(buf + x * 4); |
3986 | 208 }else |
209 #endif | |
210 memcpy(dst, buf, s->width * ((s->bpp + 1) >> 3)); | |
211 | |
212 dst += stride; | |
213 buf += s->width * ((s->bpp + 1) >> 3); | |
214 } | |
215 } | |
216 } | |
217 | |
218 *picture= *(AVFrame*)&s->picture; | |
219 *data_size = sizeof(AVPicture); | |
220 | |
221 return buf_size; | |
222 } | |
223 | |
224 static int targa_init(AVCodecContext *avctx){ | |
225 TargaContext *s = avctx->priv_data; | |
226 | |
227 avcodec_get_frame_defaults((AVFrame*)&s->picture); | |
228 avctx->coded_frame= (AVFrame*)&s->picture; | |
229 s->picture.data[0] = NULL; | |
230 | |
231 return 0; | |
232 } | |
233 | |
234 static int targa_end(AVCodecContext *avctx){ | |
235 TargaContext *s = avctx->priv_data; | |
236 | |
237 if(s->picture.data[0]) | |
238 avctx->release_buffer(avctx, &s->picture); | |
239 | |
240 return 0; | |
241 } | |
242 | |
243 AVCodec targa_decoder = { | |
244 "targa", | |
245 CODEC_TYPE_VIDEO, | |
246 CODEC_ID_TARGA, | |
247 sizeof(TargaContext), | |
248 targa_init, | |
249 NULL, | |
250 targa_end, | |
251 decode_frame, | |
252 0, | |
253 NULL | |
254 }; |