aboutsummaryrefslogtreecommitdiffhomepage
path: root/libvo/vo_tga.c
blob: 982c71a270c261fe887356b7c6ba7ebdbf4c7d64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * TARGA video output
 *
 * This video output module writes TARGA uncompressed files in 15, 24 and 32
 * bit BGR format.
 *
 * to select the output format use the format filter:
 *  mplayer -vo tga -vf format=bgr15 ...
 *  mplayer -vo tga -vf format=bgr24 ...
 *  mplayer -vo tga -vf format=bgr32 ...
 *
 * The 16 bit files are loaded without problem from Gimp and ImageMagick but
 * give an error with entice (a visualizer from the enlightenment package
 * that uses the imlib2 package).
 *
 * In 32-bit mode the alpha channel is set to 255 (0xff). For big-endian
 * machines, TGA_ALPHA32 changes from 0xff000000 to 0x000000ff, and
 * TGA_SHIFT32 from 0 to 8.
 *
 * I need to fill the alpha channel because entice considers that alpha
 * channel (and displays nothing, only the background!), but ImageMagick
 * (the program display) or gimp doesn't care.
 *
 * Maybe it is possible (with a compilation switch) to avoid the fill of
 * the alpha channel and work outside MPlayer (if needed).
 *
 *    Daniele Forghieri ( guru@digitalfantasy.it )
 *
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <math.h>

#include "config.h"
#include "mp_msg.h"
#include "video_out.h"
#include "video_out_internal.h"

static const vo_info_t info =
{
	"Targa output",
	"tga",
	"Daniele Forghieri - guru@digitalfantasy.it",
	""
};


const LIBVO_EXTERN (tga)

/* locals vars */
static int      frame_num = 0;

static void tga_make_header(uint8_t *h, int dx, int dy, int bpp)
{

    int  i;

    for(i = 0; i < 18; i++) {
        switch (i) {
        case 2:
            *h = 0x02;
            break;

        case 12:
            *h = dx & 0xff;
            break;

        case 13:
            *h = (dx >> 8) & 0xff;
            break;

        case 14:
            *h = dy & 0xff;
            break;

        case 15:
            *h = (dy >> 8) & 0xff;
            break;

        case 16:
            *h = bpp;
            break;

        case 17:
            *h = 0x20;
            break;

        default:
            *h = 0;
        }
        ++h;
    }

}

static int write_tga( char *file, int bpp, int dx, int dy, uint8_t *buf, int stride)
{
    int   er;
    FILE  *fo;

    fo = fopen(file, "wb");
    if (fo != NULL) {
        uint8_t hdr[18];

        er = 0;
        tga_make_header(hdr, dx, dy, bpp);
        if (fwrite(hdr, sizeof(hdr), 1, fo) == 1) {
            int    wb;

            wb = ((bpp + 7) / 8) * dx;
                while (dy-- > 0) {
                    if (fwrite(buf, wb, 1, fo) != 1) {
                        er = 4;
                        break;
                    }
                    buf += stride;
                }
        }
        else {
            er = 2;
        }

        fclose(fo);
    }
    else {
        er = 1;
    }

    if (er) {
        fprintf(stderr, "Error writing file [%s]\n", file);
    }
    return er;
}

static uint32_t draw_image(mp_image_t* mpi)
{
    char    file[20 + 1];

    snprintf (file, 20, "%08d.tga", ++frame_num);

    write_tga( file,
               mpi->bpp,
               mpi->w,
               mpi->h,
               mpi->planes[0],
               mpi->stride[0]);

    return VO_TRUE;
}

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)
{
    return 0;
}

static void draw_osd(void)
{
}

static void flip_page (void)
{
    return;
}

static int draw_slice(uint8_t *srcimg[], int stride[], int w,int h,int x,int y)
{
    return -1;
}

static int draw_frame(uint8_t * src[])
{
    return -1;
}

static int query_format(uint32_t format)
{
    switch(format){
        case IMGFMT_BGR15LE:
        case IMGFMT_BGR24:
        case IMGFMT_BGRA:
            return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW;
    }
    return 0;
}

static void uninit(void)
{
}

static void check_events(void)
{
}

static int preinit(const char *arg)
{
    if(arg) {
	mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_TGA] Unknown subdevice: %s.\n",arg);
	return ENOSYS;
    }
    return 0;
}

static int control(uint32_t request, void *data)
{
  switch (request) {
      case VOCTRL_DRAW_IMAGE:
          return draw_image(data);

      case VOCTRL_QUERY_FORMAT:
          return query_format(*((uint32_t*)data));
  }
  return VO_NOTIMPL;
}