diff options
Diffstat (limited to 'libavcodec/targa.c')
-rw-r--r-- | libavcodec/targa.c | 78 |
1 files changed, 46 insertions, 32 deletions
diff --git a/libavcodec/targa.c b/libavcodec/targa.c index 00da758..573992f 100644 --- a/libavcodec/targa.c +++ b/libavcodec/targa.c @@ -2,20 +2,20 @@ * Targa (.tga) image decoder * Copyright (c) 2006 Konstantin Shishkov * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -55,7 +55,7 @@ static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_ type = *src++; count = (type & 0x7F) + 1; type &= 0x80; - if((x + count > w) && (x + count + 1 > (h - y) * w)){ + if(x + count > (h - y) * w){ av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count); return -1; } @@ -70,7 +70,7 @@ static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_ *dst = *src; break; case 2: - *((uint16_t*)dst) = AV_RL16(src); + AV_WN16A(dst, AV_RN16A(src)); break; case 3: dst[0] = src[0]; @@ -78,7 +78,7 @@ static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_ dst[2] = src[2]; break; case 4: - *((uint32_t*)dst) = AV_RL32(src); + AV_WN32A(dst, AV_RN32A(src)); break; } dst += depth; @@ -109,21 +109,26 @@ static int decode_frame(AVCodecContext *avctx, AVFrame * const p= (AVFrame*)&s->picture; uint8_t *dst; int stride; - int idlen, compr, y, w, h, bpp, flags; + int idlen, pal, compr, y, w, h, bpp, flags; int first_clr, colors, csize; /* parse image header */ CHECK_BUFFER_SIZE(buf, buf_end, 18, "header"); idlen = *buf++; - buf++; /* pal */ + pal = *buf++; compr = *buf++; - first_clr = AV_RL16(buf); buf += 2; - colors = AV_RL16(buf); buf += 2; + first_clr = bytestream_get_le16(&buf); + colors = bytestream_get_le16(&buf); csize = *buf++; + if (!pal && (first_clr || colors || csize)) { + av_log(avctx, AV_LOG_WARNING, "File without colormap has colormap information set.\n"); + // specification says we should ignore those value in this case + first_clr = colors = csize = 0; + } buf += 2; /* x */ - y = AV_RL16(buf); buf += 2; - w = AV_RL16(buf); buf += 2; - h = AV_RL16(buf); buf += 2; + y = bytestream_get_le16(&buf); + w = bytestream_get_le16(&buf); + h = bytestream_get_le16(&buf); bpp = *buf++; flags = *buf++; //skip identifier if any @@ -137,16 +142,14 @@ static int decode_frame(AVCodecContext *avctx, avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8; break; case 15: - avctx->pix_fmt = PIX_FMT_RGB555; - break; case 16: - avctx->pix_fmt = PIX_FMT_RGB555; + avctx->pix_fmt = PIX_FMT_RGB555LE; break; case 24: avctx->pix_fmt = PIX_FMT_BGR24; break; case 32: - avctx->pix_fmt = PIX_FMT_RGB32; + avctx->pix_fmt = PIX_FMT_BGRA; break; default: av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp); @@ -198,7 +201,7 @@ static int decode_frame(AVCodecContext *avctx, case 3: /* RGB24 */ for (t = 0; t < colors; t++) - *pal++ = bytestream_get_le24(&buf); + *pal++ = (0xffU<<24) | bytestream_get_le24(&buf); break; case 2: /* RGB555 */ @@ -209,7 +212,7 @@ static int decode_frame(AVCodecContext *avctx, ((v & 0x001F) << 3); /* left bit replication */ v |= (v & 0xE0E0E0U) >> 5; - *pal++ = v; + *pal++ = (0xffU<<24) | v; } break; } @@ -228,18 +231,6 @@ static int decode_frame(AVCodecContext *avctx, size_t img_size = s->width * ((s->bpp + 1) >> 3); CHECK_BUFFER_SIZE(buf, buf_end, img_size, "image data"); for(y = 0; y < s->height; y++){ -#if HAVE_BIGENDIAN - int x; - if((s->bpp + 1) >> 3 == 2){ - uint16_t *dst16 = (uint16_t*)dst; - for(x = 0; x < s->width; x++) - dst16[x] = AV_RL16(buf + x * 2); - }else if((s->bpp + 1) >> 3 == 4){ - uint32_t *dst32 = (uint32_t*)dst; - for(x = 0; x < s->width; x++) - dst32[x] = AV_RL32(buf + x * 4); - }else -#endif memcpy(dst, buf, img_size); dst += stride; @@ -247,6 +238,29 @@ static int decode_frame(AVCodecContext *avctx, } } } + if(flags & 0x10){ // right-to-left, needs horizontal flip + int x; + for(y = 0; y < s->height; y++){ + void *line = &p->data[0][y * p->linesize[0]]; + for(x = 0; x < s->width >> 1; x++){ + switch(s->bpp){ + case 32: + FFSWAP(uint32_t, ((uint32_t *)line)[x], ((uint32_t *)line)[s->width - x - 1]); + break; + case 24: + FFSWAP(uint8_t, ((uint8_t *)line)[3 * x ], ((uint8_t *)line)[3 * s->width - 3 * x - 3]); + FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 1], ((uint8_t *)line)[3 * s->width - 3 * x - 2]); + FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 2], ((uint8_t *)line)[3 * s->width - 3 * x - 1]); + break; + case 16: + FFSWAP(uint16_t, ((uint16_t *)line)[x], ((uint16_t *)line)[s->width - x - 1]); + break; + case 8: + FFSWAP(uint8_t, ((uint8_t *)line)[x], ((uint8_t *)line)[s->width - x - 1]); + } + } + } + } *picture= *(AVFrame*)&s->picture; *data_size = sizeof(AVPicture); |