2 * Targa (.tga) image decoder
3 * Copyright (c) 2006 Konstantin Shishkov
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/imgutils.h"
27 typedef struct TargaContext {
36 #define CHECK_BUFFER_SIZE(buf, buf_end, needed, where) \
37 if(needed > buf_end - buf){ \
38 av_log(avctx, AV_LOG_ERROR, "Problem: unexpected end of data while reading " where "\n"); \
42 static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_t *src, int src_size, uint8_t *dst, int w, int h, int stride, int bpp)
45 int depth = (bpp + 1) >> 3;
48 const uint8_t *src_end = src + src_size;
50 diff = stride - w * depth;
53 CHECK_BUFFER_SIZE(src, src_end, 1, "image type");
55 count = (type & 0x7F) + 1;
57 if((x + count > w) && (x + count + 1 > (h - y) * w)){
58 av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);
62 CHECK_BUFFER_SIZE(src, src_end, depth, "image data");
64 CHECK_BUFFER_SIZE(src, src_end, count * depth, "image data");
66 for(i = 0; i < count; i++){
72 *((uint16_t*)dst) = AV_RL16(src);
80 *((uint32_t*)dst) = AV_RL32(src);
100 static int decode_frame(AVCodecContext *avctx,
101 void *data, int *data_size,
104 const uint8_t *buf = avpkt->data;
105 const uint8_t *buf_end = avpkt->data + avpkt->size;
106 TargaContext * const s = avctx->priv_data;
107 AVFrame *picture = data;
108 AVFrame * const p= (AVFrame*)&s->picture;
111 int idlen, compr, y, w, h, bpp, flags;
112 int first_clr, colors, csize;
114 /* parse image header */
115 CHECK_BUFFER_SIZE(buf, buf_end, 18, "header");
119 first_clr = AV_RL16(buf); buf += 2;
120 colors = AV_RL16(buf); buf += 2;
123 y = AV_RL16(buf); buf += 2;
124 w = AV_RL16(buf); buf += 2;
125 h = AV_RL16(buf); buf += 2;
128 //skip identifier if any
129 CHECK_BUFFER_SIZE(buf, buf_end, idlen, "identifiers");
136 avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
139 avctx->pix_fmt = PIX_FMT_RGB555;
142 avctx->pix_fmt = PIX_FMT_RGB555;
145 avctx->pix_fmt = PIX_FMT_BGR24;
148 avctx->pix_fmt = PIX_FMT_RGB32;
151 av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
155 if(s->picture.data[0])
156 avctx->release_buffer(avctx, &s->picture);
158 if(av_image_check_size(w, h, 0, avctx))
160 if(w != avctx->width || h != avctx->height)
161 avcodec_set_dimensions(avctx, w, h);
162 if(avctx->get_buffer(avctx, p) < 0){
163 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
168 stride = p->linesize[0];
169 }else{ //image is upside-down
170 dst = p->data[0] + p->linesize[0] * (h - 1);
171 stride = -p->linesize[0];
176 if((colors + first_clr) > 256){
177 av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
181 av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
184 pal_size = colors * ((csize + 1) >> 3);
185 CHECK_BUFFER_SIZE(buf, buf_end, pal_size, "color table");
186 if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
190 int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
191 for(t = 0; t < colors; t++){
195 *pal++ = (b << 16) | (g << 8) | r;
197 p->palette_has_changed = 1;
200 if((compr & (~TGA_RLE)) == TGA_NODATA)
201 memset(p->data[0], 0, p->linesize[0] * s->height);
204 int res = targa_decode_rle(avctx, s, buf, buf_end - buf, dst, avctx->width, avctx->height, stride, bpp);
209 size_t img_size = s->width * ((s->bpp + 1) >> 3);
210 CHECK_BUFFER_SIZE(buf, buf_end, img_size, "image data");
211 for(y = 0; y < s->height; y++){
214 if((s->bpp + 1) >> 3 == 2){
215 uint16_t *dst16 = (uint16_t*)dst;
216 for(x = 0; x < s->width; x++)
217 dst16[x] = AV_RL16(buf + x * 2);
218 }else if((s->bpp + 1) >> 3 == 4){
219 uint32_t *dst32 = (uint32_t*)dst;
220 for(x = 0; x < s->width; x++)
221 dst32[x] = AV_RL32(buf + x * 4);
224 memcpy(dst, buf, img_size);
232 *picture= *(AVFrame*)&s->picture;
233 *data_size = sizeof(AVPicture);
238 static av_cold int targa_init(AVCodecContext *avctx){
239 TargaContext *s = avctx->priv_data;
241 avcodec_get_frame_defaults((AVFrame*)&s->picture);
242 avctx->coded_frame= (AVFrame*)&s->picture;
247 static av_cold int targa_end(AVCodecContext *avctx){
248 TargaContext *s = avctx->priv_data;
250 if(s->picture.data[0])
251 avctx->release_buffer(avctx, &s->picture);
256 AVCodec ff_targa_decoder = {
260 sizeof(TargaContext),
267 .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),