2 * Targa (.tga) image decoder
3 * Copyright (c) 2006 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
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
32 typedef struct TargaContext {
41 static void targa_decode_rle(AVCodecContext *avctx, TargaContext *s, uint8_t *src, uint8_t *dst, int w, int h, int stride, int bpp)
44 int depth = (bpp + 1) >> 3;
48 diff = stride - w * depth;
52 count = (type & 0x7F) + 1;
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);
58 for(i = 0; i < count; i++){
64 *((uint16_t*)dst) = AV_RL16(src);
72 *((uint32_t*)dst) = AV_RL32(src);
91 static int decode_frame(AVCodecContext *avctx,
92 void *data, int *data_size,
93 uint8_t *buf, int buf_size)
95 TargaContext * const s = avctx->priv_data;
96 AVFrame *picture = data;
97 AVFrame * const p= (AVFrame*)&s->picture;
100 int idlen, pal, compr, x, y, w, h, bpp, flags;
101 int first_clr, colors, csize;
103 /* parse image header */
107 first_clr = AV_RL16(buf); buf += 2;
108 colors = AV_RL16(buf); buf += 2;
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;
116 //skip identifier if any
123 avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
126 avctx->pix_fmt = PIX_FMT_RGB555;
129 avctx->pix_fmt = PIX_FMT_RGB555;
132 avctx->pix_fmt = PIX_FMT_BGR24;
135 avctx->pix_fmt = PIX_FMT_RGB32;
138 av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
142 if(s->picture.data[0])
143 avctx->release_buffer(avctx, &s->picture);
145 if(avcodec_check_dimensions(avctx, w, h))
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");
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];
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;
169 if((colors + first_clr) > 256){
170 av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
174 av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
177 if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
178 buf += colors * ((csize + 1) >> 3);
181 int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
182 for(t = 0; t < colors; t++){
186 *pal++ = (b << 16) | (g << 8) | r;
188 p->palette_has_changed = 1;
189 avctx->palctrl->palette_changed = 0;
192 if((compr & (~TGA_RLE)) == TGA_NODATA)
193 memset(p->data[0], 0, p->linesize[0] * s->height);
196 targa_decode_rle(avctx, s, buf, dst, avctx->width, avctx->height, stride, bpp);
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++)
203 dst16[x] = AV_RL16(buf + x * 2);
204 }else if((s->bpp + 1) >> 3 == 4){
205 uint32_t *dst32 = (uint32_t*)dst;
206 for(x = 0; x < s->width; x++)
207 dst32[x] = AV_RL32(buf + x * 4);
210 memcpy(dst, buf, s->width * ((s->bpp + 1) >> 3));
213 buf += s->width * ((s->bpp + 1) >> 3);
218 *picture= *(AVFrame*)&s->picture;
219 *data_size = sizeof(AVPicture);
224 static int targa_init(AVCodecContext *avctx){
225 TargaContext *s = avctx->priv_data;
227 avcodec_get_frame_defaults((AVFrame*)&s->picture);
228 avctx->coded_frame= (AVFrame*)&s->picture;
229 s->picture.data[0] = NULL;
234 static int targa_end(AVCodecContext *avctx){
235 TargaContext *s = avctx->priv_data;
237 if(s->picture.data[0])
238 avctx->release_buffer(avctx, &s->picture);
243 AVCodec targa_decoder = {
247 sizeof(TargaContext),