@@ -154,8 +154,8 @@ bool targa_c::Load(const char* fileName)
154154 // Try to match image type
155155 int ittable[3 ][3 ] = {
156156 3 , 8 , IMGTYPE_GRAY ,
157- 2 , 24 , IMGTYPE_BGR ,
158- 2 , 32 , IMGTYPE_BGRA
157+ 2 , 24 , IMGTYPE_RGB ,
158+ 2 , 32 , IMGTYPE_RGBA
159159 };
160160 int it_m;
161161 for (it_m = 0 ; it_m < 3 ; it_m++) {
@@ -209,117 +209,20 @@ bool targa_c::Load(const char* fileName)
209209 }
210210 }
211211
212+ // Byteswap BGR(A) to RGB(A)
213+ if (comp == 3 || comp == 4 ) {
214+ uint8_t * p = dat;
215+ for (size_t i = 0 ; i < width * height; ++i, p += comp) {
216+ std::swap (p[0 ], p[2 ]);
217+ }
218+ }
219+
212220 return false ;
213221}
214222
215223bool targa_c::Save (const char * fileName)
216224{
217- // Find a suitable image type
218- int ittable[3 ][3 ] = {
219- 1 , IMGTYPE_GRAY , 3 ,
220- 3 , IMGTYPE_BGR , 2 ,
221- 4 , IMGTYPE_BGRA , 2
222- };
223- int it_m;
224- for (it_m = 0 ; it_m < 3 ; it_m++) {
225- if (ittable[it_m][0 ] == comp && ittable[it_m][1 ] == type) break ;
226- }
227- if (it_m == 3 ) {
228- // Image type not supported
229- return true ;
230- }
231-
232- // Open the file
233- fileOutputStream_c out;
234- if (out.FileOpen (fileName, true )) {
235- return true ;
236- }
237-
238- // Write header
239- tgaHeader_s hdr;
240- memset (&hdr, 0 , sizeof (hdr));
241- hdr.width = width;
242- hdr.height = height;
243- hdr.depth = comp << 3 ;
244- hdr.imgType = ittable[it_m][2 ] + rle * 8 ;
245- out.TWrite (hdr);
246-
247- // Write image
248- dword rowSize = width * comp;
249- if (rle) {
250- byte* packet = new byte[comp * 128 + 4 ];
251- dword mask = 0xFFFFFFFF >> ((4 - comp) << 3 );
252- for (int y = height - 1 ; y >= 0 ; y--) {
253- byte* p = dat + y * rowSize;
254- byte hdr = 255 ;
255- dword lastPix;
256- memOutputStream_c line (512 );
257- for (dword x = 0 ; x < width; x++, p+= comp) {
258- dword pix = *(dword*)p & mask;
259- if (hdr == 255 ) {
260- // Start new packet
261- *(dword*)packet = pix;
262- hdr = 0 ;
263- } else if (hdr & 0x80 ) {
264- // Run-length packet, check for continuance
265- if (pix == lastPix) {
266- hdr++;
267- if (hdr == 255 ) {
268- // Max length, write it
269- line.TWrite (hdr);
270- line.Write (packet, comp);
271- }
272- } else {
273- line.TWrite (hdr);
274- line.Write (packet, comp);
275- *(dword*)packet = pix;
276- hdr = 0 ;
277- }
278- } else if (hdr) {
279- // Raw packet, check if a run-length packet could be created with the last pixel
280- if (pix == lastPix) {
281- hdr--;
282- line.TWrite (hdr);
283- line.Write (packet, comp * (hdr + 1 ));
284- *(dword*)packet = pix;
285- hdr = 129 ;
286- } else if (hdr == 127 ) {
287- // Packet is already full, write it
288- line.TWrite (hdr);
289- line.Write (packet, comp * (hdr + 1 ));
290- *(dword*)packet = pix;
291- hdr = 0 ;
292- } else {
293- hdr++;
294- *(dword*)(packet + comp * hdr) = pix;
295- }
296- } else {
297- // New packet, check if this could become a run-length packet
298- if (pix == lastPix) {
299- hdr = 129 ;
300- } else {
301- hdr = 1 ;
302- *(dword*)(packet + comp) = pix;
303- }
304- }
305- lastPix = pix;
306- }
307- if (hdr < 255 ) {
308- // Leftover packet, write it
309- line.TWrite (hdr);
310- line.Write (packet, hdr & 0x80 ? comp : comp * (hdr + 1 ));
311- }
312- line.MemOutput (&out);
313- }
314- delete[] packet;
315- } else {
316- // Raw
317- for (int y = height - 1 ; y >= 0 ; y--) {
318- out.Write (dat + y * rowSize, rowSize);
319- }
320- }
321-
322- return false ;
225+ return true ;
323226}
324227
325228bool targa_c::ImageInfo (const char * fileName, imageInfo_s* info)
0 commit comments