1 /* author: Tilman Sauerbeck <tsauerbeck@users.sourceforge.net>
2 * based on: XreaL's x_r_img_tga.* (http://www.sourceforge.net/projects/xreal/)
15 #include "libvo/fastmemcpy.h"
17 #include "vd_internal.h"
19 static vd_info_t info
=
23 "Tilman Sauerbeck, A'rpi",
25 "only 24bpp and 32bpp RGB targa files support so far"
44 unsigned short img_type
;
47 unsigned short height
;
50 unsigned char origin
; /* 0 = lower left, 1 = upper left */
51 unsigned short start_row
;
55 static unsigned int out_fmt
= 0;
57 static int last_w
= -1;
58 static int last_h
= -1;
59 static int last_c
= -1;
62 /* to set/get/query special features/parameters */
63 static int control(sh_video_t
*sh
, int cmd
, void *arg
, ...)
67 case VDCTRL_QUERY_FORMAT
:
68 if (*((int *) arg
) == out_fmt
) return CONTROL_TRUE
;
71 return CONTROL_UNKNOWN
;
75 static int init(sh_video_t
*sh
)
77 sh
->context
= (TGAInfo
*) calloc(1, sizeof(TGAInfo
));
85 static void uninit(sh_video_t
*sh
)
87 TGAInfo
*info
= sh
->context
;
93 /* decode a runlength-encoded tga */
94 static void decode_rle_tga(TGAInfo
*info
, unsigned char *data
, mp_image_t
*mpi
)
96 int row
, col
, replen
, i
, num_bytes
= info
->bpp
/ 8;
97 unsigned char repetitions
, packet_header
, *final
;
99 /* see line 207 to see why this loop is set up like this */
100 for (row
= info
->start_row
; (!info
->origin
&& row
) || (info
->origin
&& row
< info
->height
); row
+= info
->increment
)
102 final
= mpi
->planes
[0] + mpi
->stride
[0] * row
;
104 for (col
= 0; col
< info
->width
; col
+= repetitions
)
106 packet_header
= *data
++;
107 repetitions
= (1 + (packet_header
& 0x7f));
108 replen
= repetitions
* num_bytes
;
110 if (packet_header
& 0x80) /* runlength encoded packet */
112 memcpy(final
, data
, num_bytes
);
114 // Note: this will be slow when DR to vram!
117 memcpy(final
+i
,final
,i
);
120 memcpy(final
+i
,final
,replen
-i
);
123 else /* raw packet */
125 fast_memcpy(final
, data
, replen
);
137 static void decode_uncompressed_tga(TGAInfo
*info
, unsigned char *data
, mp_image_t
*mpi
)
139 unsigned char *final
;
140 int row
, num_bytes
= info
->bpp
/ 8;
142 /* see line 207 to see why this loop is set up like this */
143 for (row
= info
->start_row
; (!info
->origin
&& row
) || (info
->origin
&& row
< info
->height
); row
+= info
->increment
)
145 final
= mpi
->planes
[0] + mpi
->stride
[0] * row
;
146 fast_memcpy(final
, data
, info
->width
* num_bytes
);
147 data
+= info
->width
* num_bytes
;
154 static short read_tga_header(unsigned char *buf
, TGAInfo
*info
)
156 info
->id_len
= buf
[0];
158 info
->img_type
= buf
[2];
160 /* targa data is always stored in little endian byte order */
161 info
->width
= le2me_16(*(unsigned short *) &buf
[12]);
162 info
->height
= le2me_16(*(unsigned short *) &buf
[14]);
166 info
->origin
= (buf
[17] & 0x20) >> 5;
168 /* FIXME check for valid targa data */
175 static mp_image_t
*decode(sh_video_t
*sh
, void *raw
, int len
, int flags
)
177 TGAInfo
*info
= sh
->context
;
178 unsigned char *data
= raw
;
183 return NULL
; /* skip frame */
185 read_tga_header(data
, info
); /* read information about the file */
188 out_fmt
= IMGFMT_BGR24
;
189 else if (info
->bpp
== 32)
190 out_fmt
= IMGFMT_BGR32
;
193 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, "Unsupported TGA type! depth=%d\n",info
->bpp
);
197 if (info
->img_type
!= TGA_UNCOMP_TRUECOLOR
&& info
->img_type
!= TGA_RLE_TRUECOLOR
) /* not a true color image */
199 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, "Unsupported TGA type: %i!\n", info
->img_type
);
203 /* if img.origin is 0, we decode from bottom to top. if it's 1, we decode from top to bottom */
204 info
->start_row
= (info
->origin
) ? 0 : info
->height
- 1;
205 info
->increment
= (info
->origin
) ? 1 : -1;
207 /* set data to the beginning of the image data */
208 data
+= 18 + info
->id_len
;
210 /* (re)init libvo if image parameters changed (width/height/colorspace) */
211 if (last_w
!= info
->width
|| last_h
!= info
->height
|| last_c
!= out_fmt
)
213 last_w
= info
->width
;
214 last_h
= info
->height
;
217 if (!out_fmt
|| !mpcodecs_config_vo(sh
, info
->width
, info
->height
, out_fmt
))
221 if (!(mpi
= mpcodecs_get_image(sh
, MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
, info
->width
, info
->height
)))
224 /* finally decode the image */
225 if (info
->img_type
== TGA_UNCOMP_TRUECOLOR
)
226 decode_uncompressed_tga(info
, data
, mpi
);
227 else if (info
->img_type
== TGA_RLE_TRUECOLOR
)
228 decode_rle_tga(info
, data
, mpi
);