Simplify, do not duplicate buffer size calculation
[mplayer/glamo.git] / libvo / vo_tga.c
blob63f7be2284ea723c14f7d152f78235c205fcf98b
1 /*
2 * vo_tga.c: targa output
4 * this video output module write targa uncompressed file in 15, 24 and 32 bit bgr format.
6 * to select the output format use the format filter:
7 * mplayer -vo tga -vf format=bgr15 ...
8 * mplayer -vo tga -vf format=bgr24 ...
9 * mplayer -vo tga -vf format=bgr32 ...
11 * The 16 bit file are loaded without problem from Gimp and ImageMagick but give an error
12 * with entice (a visualizer from the enlightenment package that use the imlib2 package).
14 * In 32 bit mode the alpha channel is set to 255 (0xff). For big endian
15 * machines, TGA_ALPHA32 changes from 0xff000000 to 0x000000ff, and TGA_SHIFT32 from 0 to 8.
17 * I need to fill the alpha channel because entice consider that alpha channel (and displays
18 * nothing, only the background!), but ImageMacick (the program display) or gimp doesn't
19 * care.
21 * maybe is possible (with a compilation switch) to avoid the fill of the alpha channel
22 * and work outside mplayer (if needed)
24 * Daniele Forghieri ( guru@digitalfantasy.it )
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <math.h>
34 #include "config.h"
35 #include "mp_msg.h"
36 #include "help_mp.h"
37 #include "video_out.h"
38 #include "video_out_internal.h"
40 static const vo_info_t info =
42 "Targa output",
43 "tga",
44 "Daniele Forghieri - guru@digitalfantasy.it",
49 const LIBVO_EXTERN (tga)
51 /* locals vars */
52 static int frame_num = 0;
53 static void *line_buff;
55 static void tga_make_header(uint8_t *h, int dx, int dy, int bpp)
58 int i;
60 for(i = 0; i < 18; i++) {
61 switch (i) {
62 case 2:
63 *h = 0x02;
64 break;
66 case 12:
67 *h = dx & 0xff;
68 break;
70 case 13:
71 *h = (dx >> 8) & 0xff;
72 break;
74 case 14:
75 *h = dy & 0xff;
76 break;
78 case 15:
79 *h = (dy >> 8) & 0xff;
80 break;
82 case 16:
83 *h = bpp;
84 break;
86 case 17:
87 *h = 0x20;
88 break;
90 default:
91 *h = 0;
93 ++h;
98 static int write_tga( char *file, int bpp, int dx, int dy, uint8_t *buf, int stride)
100 int er;
101 FILE *fo;
103 fo = fopen(file, "wb");
104 if (fo != NULL) {
105 uint8_t hdr[18];
107 er = 0;
108 tga_make_header(hdr, dx, dy, bpp);
109 if (fwrite(hdr, sizeof(hdr), 1, fo) == 1) {
110 int wb;
112 wb = ((bpp + 7) / 8) * dx;
113 if (bpp == 32) {
114 /* Setup the alpha channel for every pixel */
115 while (dy-- > 0) {
116 uint8_t *d;
117 uint8_t *s;
118 int x;
120 s = buf;
121 d = line_buff;
122 for(x = 0; x < dx; x++) {
123 #ifdef WORDS_BIGENDIAN
124 d[0] = s[3];
125 d[1] = s[2];
126 d[2] = s[1];
127 d[3] = 0xff;
128 #else
129 d[0] = 0xff;
130 d[1] = s[1];
131 d[2] = s[2];
132 d[3] = s[3];
133 #endif
134 d+=4;
135 s+=4;
137 if (fwrite(line_buff, wb, 1, fo) != 1) {
138 er = 4;
139 break;
141 buf += stride;
145 else {
146 while (dy-- > 0) {
147 if (fwrite(buf, wb, 1, fo) != 1) {
148 er = 4;
149 break;
151 buf += stride;
155 else {
156 er = 2;
159 fclose(fo);
161 else {
162 er = 1;
165 if (er) {
166 fprintf(stderr, "Error writing file [%s]\n", file);
168 return er;
171 static uint32_t draw_image(mp_image_t* mpi)
173 char file[20 + 1];
175 snprintf (file, 20, "%08d.tga", ++frame_num);
177 write_tga( file,
178 mpi->bpp,
179 mpi->w,
180 mpi->h,
181 mpi->planes[0],
182 mpi->stride[0]);
184 return VO_TRUE;
187 static int config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
189 /* buffer for alpha */
190 if(line_buff){ free(line_buff); line_buff=NULL; }
191 if (format == (IMGFMT_BGR | 32)) {
192 line_buff = malloc(width * 4);
194 return 0;
197 static void draw_osd(void)
201 static void flip_page (void)
203 return;
206 static int draw_slice(uint8_t *srcimg[], int stride[], int w,int h,int x,int y)
208 return -1;
211 static int draw_frame(uint8_t * src[])
213 return -1;
216 static int query_format(uint32_t format)
218 switch(format){
219 case IMGFMT_BGR|15:
220 case IMGFMT_BGR|24:
221 case IMGFMT_BGR|32:
222 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW;
224 return 0;
227 static void uninit(void)
229 if(line_buff){ free(line_buff); line_buff=NULL; }
232 static void check_events(void)
236 static int preinit(const char *arg)
238 if(arg) {
239 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_TGA_UnknownSubdevice,arg);
240 return ENOSYS;
242 return 0;
245 static int control(uint32_t request, void *data, ...)
247 switch (request) {
248 case VOCTRL_DRAW_IMAGE:
249 return draw_image(data);
251 case VOCTRL_QUERY_FORMAT:
252 return query_format(*((uint32_t*)data));
254 return VO_NOTIMPL;