2 * Targa (.tga) image encoder
3 * Copyright (c) 2007 Bobby Bingham
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
24 static int targa_encode_normal(uint8_t *outbuf, AVFrame *pic, int bpp, int w, int h)
27 uint8_t *out = outbuf;
28 uint8_t *ptr = pic->data[0];
30 for(i=0; i < h; i++) {
33 ptr += pic->linesize[0];
39 static int targa_encode_frame(AVCodecContext *avctx,
40 unsigned char *outbuf,
41 int buf_size, void *data){
46 if(avctx->width > 0xffff || avctx->height > 0xffff) {
47 av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
50 if(buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 45) {
51 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
55 p->pict_type= FF_I_TYPE;
58 /* zero out the header and only set applicable fields */
59 memset(outbuf, 0, 11);
60 AV_WL16(outbuf+12, avctx->width);
61 AV_WL16(outbuf+14, avctx->height);
62 outbuf[17] = 0x20; /* origin is top-left. no alpha */
64 /* TODO: support alpha channel and RLE */
65 switch(avctx->pix_fmt) {
67 outbuf[2] = 3; /* uncompressed grayscale image */
68 outbuf[16] = 8; /* bpp */
71 outbuf[2] = 2; /* uncompresses true-color image */
72 outbuf[16] = 16; /* bpp */
75 outbuf[2] = 2; /* uncompressed true-color image */
76 outbuf[16] = 24; /* bpp */
81 bpp = outbuf[16] >> 3;
83 out = outbuf + 18; /* skip past the header we just output */
85 out += targa_encode_normal(out, p, bpp, avctx->width, avctx->height);
87 /* The standard recommends including this section, even if we don't use
88 * any of the features it affords. TODO: take advantage of the pixel
89 * aspect ratio and encoder ID fields available? */
90 memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
92 return out + 26 - outbuf;
95 static int targa_encode_init(AVCodecContext *avctx)
100 AVCodec targa_encoder = {
102 .type = CODEC_TYPE_VIDEO,
103 .id = CODEC_ID_TARGA,
105 .init = targa_encode_init,
106 .encode = targa_encode_frame,
107 .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB555, PIX_FMT_GRAY8, -1},