Minor simplification of rotate_block()
[ffmpeg-lucabe.git] / libavcodec / targaenc.c
blob90dc8cba617f4a35c0d47594b0e42b21512c107d
1 /*
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
21 #include "avcodec.h"
22 #include "rle.h"
24 /**
25 * RLE compress the image, with maximum size of out_size
26 * @param outbuf Output buffer
27 * @param out_size Maximum output size
28 * @param pic Image to compress
29 * @param bpp Bytes per pixel
30 * @param w Image width
31 * @param h Image height
32 * @return Size of output in bytes, or -1 if larger than out_size
34 static int targa_encode_rle(uint8_t *outbuf, int out_size, AVFrame *pic,
35 int bpp, int w, int h)
37 int y,ret;
38 uint8_t *out;
40 out = outbuf;
42 for(y = 0; y < h; y ++) {
43 ret = ff_rle_encode(out, out_size, pic->data[0] + pic->linesize[0] * y, bpp, w, 0x7f, 0, -1, 0);
44 if(ret == -1){
45 return -1;
47 out+= ret;
48 out_size -= ret;
51 return out - outbuf;
54 static int targa_encode_normal(uint8_t *outbuf, AVFrame *pic, int bpp, int w, int h)
56 int i, n = bpp * w;
57 uint8_t *out = outbuf;
58 uint8_t *ptr = pic->data[0];
60 for(i=0; i < h; i++) {
61 memcpy(out, ptr, n);
62 out += n;
63 ptr += pic->linesize[0];
66 return out - outbuf;
69 static int targa_encode_frame(AVCodecContext *avctx,
70 unsigned char *outbuf,
71 int buf_size, void *data){
72 AVFrame *p = data;
73 int bpp, picsize, datasize;
74 uint8_t *out;
76 if(avctx->width > 0xffff || avctx->height > 0xffff) {
77 av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
78 return -1;
80 picsize = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
81 if(buf_size < picsize + 45) {
82 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
83 return -1;
86 p->pict_type= FF_I_TYPE;
87 p->key_frame= 1;
89 /* zero out the header and only set applicable fields */
90 memset(outbuf, 0, 12);
91 AV_WL16(outbuf+12, avctx->width);
92 AV_WL16(outbuf+14, avctx->height);
93 outbuf[17] = 0x20; /* origin is top-left. no alpha */
95 /* TODO: support alpha channel */
96 switch(avctx->pix_fmt) {
97 case PIX_FMT_GRAY8:
98 outbuf[2] = 3; /* uncompressed grayscale image */
99 outbuf[16] = 8; /* bpp */
100 break;
101 case PIX_FMT_RGB555:
102 outbuf[2] = 2; /* uncompresses true-color image */
103 outbuf[16] = 16; /* bpp */
104 break;
105 case PIX_FMT_BGR24:
106 outbuf[2] = 2; /* uncompressed true-color image */
107 outbuf[16] = 24; /* bpp */
108 break;
109 default:
110 return -1;
112 bpp = outbuf[16] >> 3;
114 out = outbuf + 18; /* skip past the header we just output */
116 /* try RLE compression */
117 datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->height);
119 /* if that worked well, mark the picture as RLE compressed */
120 if(datasize >= 0)
121 outbuf[2] |= 8;
123 /* if RLE didn't make it smaller, go back to no compression */
124 else datasize = targa_encode_normal(out, p, bpp, avctx->width, avctx->height);
126 out += datasize;
128 /* The standard recommends including this section, even if we don't use
129 * any of the features it affords. TODO: take advantage of the pixel
130 * aspect ratio and encoder ID fields available? */
131 memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
133 return out + 26 - outbuf;
136 static av_cold int targa_encode_init(AVCodecContext *avctx)
138 return 0;
141 AVCodec targa_encoder = {
142 .name = "targa",
143 .type = CODEC_TYPE_VIDEO,
144 .id = CODEC_ID_TARGA,
145 .priv_data_size = 0,
146 .init = targa_encode_init,
147 .encode = targa_encode_frame,
148 .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB555, PIX_FMT_GRAY8, PIX_FMT_NONE},
149 .long_name= "Truevision Targa image",