svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r21908:22075
[blender.git] / source / blender / imbuf / intern / targa.c
1 /**
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  * $Id$
28  */
29
30 #ifdef WIN32
31 #include <io.h>
32 #endif
33 #include "BLI_blenlib.h"
34
35 #include "imbuf.h"
36 #include "imbuf_patch.h"
37
38 #include "IMB_imbuf_types.h"
39 #include "IMB_imbuf.h"
40
41 #include "IMB_allocimbuf.h"
42 #include "IMB_cmap.h"
43 #include "IMB_targa.h"
44
45
46 /* this one is only def-ed once, strangely... related to GS? */
47 #define GSS(x) (((uchar *)(x))[1] << 8 | ((uchar *)(x))[0])
48
49 /***/
50
51 typedef struct TARGA 
52 {
53         unsigned char numid;    
54         unsigned char maptyp;
55         unsigned char imgtyp;   
56         short maporig;
57         short mapsize;
58         unsigned char mapbits;
59         short xorig;
60         short yorig;
61         short xsize;
62         short ysize;
63         unsigned char pixsize;
64         unsigned char imgdes;
65 } TARGA;
66
67 /***/
68
69 static int tga_out1(unsigned int data, FILE *file)
70 {
71         uchar *p;
72
73         p = (uchar *) & data;
74         if (putc(p[0],file) == EOF) return(EOF);
75         return (~EOF);
76 }
77
78 static int tga_out2(unsigned int data, FILE * file)
79 {
80         uchar *p;
81
82         p = (uchar *) & data;
83         if (putc(p[0],file) == EOF) return(EOF);
84         if (putc(p[1],file) == EOF) return(EOF);
85         return (~EOF);
86 }
87
88
89 static int tga_out3(unsigned int data, FILE * file)
90 {
91         uchar *p;
92
93         p = (uchar *) & data;
94         if (putc(p[2],file) == EOF) return(EOF);
95         if (putc(p[1],file) == EOF) return(EOF);
96         if (putc(p[0],file) == EOF) return(EOF);
97         return (~EOF);
98 }
99
100
101 static int tga_out4(unsigned int data, FILE * file)
102 {
103         uchar *p;
104
105         p = (uchar *) & data;
106         /* order = bgra */
107         if (putc(p[2],file) == EOF) return(EOF);
108         if (putc(p[1],file) == EOF) return(EOF);
109         if (putc(p[0],file) == EOF) return(EOF);
110         if (putc(p[3],file) == EOF) return(EOF);
111         return (~EOF);
112 }
113
114 static short makebody_tga(ImBuf * ibuf, FILE * file, int (*out)(unsigned int, FILE*))
115 {
116         register int last,this;
117         register int copy, bytes;
118         register unsigned int *rect, *rectstart, *temp;
119         int y;
120         
121         for (y = 0; y < ibuf->y; y++) {
122                 bytes = ibuf->x - 1;
123                 rectstart = rect = ibuf->rect + (y * ibuf->x);
124                 last = *rect++;
125                 this = *rect++;
126                 copy = last^this;
127                 while (bytes > 0){
128                         if (copy){
129                                 do{
130                                         last = this;
131                                         this = *rect++;
132                                         if (last == this){
133                                                 if (this == rect[-3]){  /* three the same? */
134                                                         bytes --;               /* set bytes */
135                                                         break;
136                                                 }
137                                         }
138                                 }while (--bytes != 0);
139
140                                 copy = rect-rectstart;
141                                 copy --;
142                                 if (bytes) copy -= 2;
143
144                                 temp = rect;
145                                 rect = rectstart;
146
147                                 while (copy){
148                                         last = copy;
149                                         if (copy>=128) last = 128;
150                                         copy -= last;
151                                         if (fputc(last-1,file) == EOF) return(0);
152                                         do{
153                                                 if (out(*rect++,file) == EOF) return(0);
154                                         }while(--last != 0);
155                                 }
156                                 rectstart = rect;
157                                 rect = temp;
158                                 last = this;
159
160                                 copy = FALSE;
161                         } else {
162                                 while (*rect++ == this){                /* seek for first different byte */
163                                         if (--bytes == 0) break;        /* oor end of line */
164                                 }
165                                 rect --;
166                                 copy = rect-rectstart;
167                                 rectstart = rect;
168                                 bytes --;
169                                 this = *rect++;
170
171                                 while (copy){
172                                         if (copy>128){
173                                                 if (fputc(255,file) == EOF) return(0);
174                                                 copy -= 128;
175                                         } else {
176                                                 if (copy == 1){
177                                                         if (fputc(0,file) == EOF) return(0);
178                                                 } else if (fputc(127 + copy,file) == EOF) return(0);
179                                                 copy = 0;
180                                         }
181                                         if (out(last,file) == EOF) return(0);
182                                 }
183                                 copy=TRUE;
184                         }
185                 }
186         }
187         return (1);
188 }
189
190 static int dumptarga(struct ImBuf * ibuf, FILE * file)
191 {
192         int size;
193         uchar *rect;
194
195         if (ibuf == 0) return (0);
196         if (ibuf->rect == 0) return (0);
197
198         size = ibuf->x * ibuf->y;
199         rect = (uchar *) ibuf->rect;
200
201         if (ibuf->depth <= 8) {
202                 while(size > 0){
203                         if (putc(*rect, file) == EOF) return (0);
204                         size--;
205                         rect += 4;
206                 }
207         } else if (ibuf->depth <= 16) {
208                 while(size > 0){
209                         putc(rect[0], file);
210                         if (putc(rect[1], file) == EOF) return (0);
211                         size--;
212                         rect += 4;
213                 }
214         } else if (ibuf->depth <= 24) {
215                 while(size > 0){
216                         putc(rect[2], file);
217                         putc(rect[1], file);
218                         if (putc(rect[0], file) == EOF) return (0);
219                         size--;
220                         rect += 4;
221                 }
222         } else if (ibuf->depth <= 32) {
223                 while(size > 0){
224                         putc(rect[2], file);
225                         putc(rect[1], file);
226                         putc(rect[0], file);
227                         if (putc(rect[3], file) == EOF) return (0);
228                         size--;
229                         rect += 4;
230                 }
231         } else return (0);
232         
233         return (1);
234 }
235
236
237 short imb_savetarga(struct ImBuf * ibuf, char *name, int flags)
238 {
239         char buf[20];
240         FILE *fildes;
241         int i;
242         short ok = 0;
243
244         if (ibuf == 0) return (0);
245         if (ibuf->rect == 0) return (0);
246
247         memset(buf,0,sizeof(buf));
248
249         /* buf[0] = 0;  length string */
250
251         buf[16] = (ibuf->depth + 0x7 ) & ~0x7;
252         if (ibuf->cmap) {
253                 buf[1] = 1;
254                 buf[2] = 9;
255                 buf[3] = ibuf->mincol & 0xff;
256                 buf[4] = ibuf->mincol >> 8;
257                 buf[5] = ibuf->maxcol & 0xff;
258                 buf[6] = ibuf->maxcol >> 8;
259                 buf[7] = 24;
260                 if ((flags & IB_ttob) == 0) {
261                         IMB_flipy(ibuf);
262                         buf[17] = 0x20;
263                 }
264         } else if (ibuf->depth > 8 ){
265                 buf[2] = 10;
266         } else{
267                 buf[2] = 11;
268         }
269
270         if (ibuf->ftype == RAWTGA) buf[2] &= ~8;
271         
272         buf[8] = ibuf->xorig & 0xff;
273         buf[9] = ibuf->xorig >> 8;
274         buf[10] = ibuf->yorig & 0xff;
275         buf[11] = ibuf->yorig >> 8;
276
277         buf[12] = ibuf->x & 0xff;
278         buf[13] = ibuf->x >> 8;
279         buf[14] = ibuf->y & 0xff;
280         buf[15] = ibuf->y >> 8;
281
282         if (flags & IB_ttob) buf[17] ^= 0x20;
283
284         /* Don't forget to indicate that your 32 bit
285          * targa uses 8 bits for the alpha channel! */
286         if (ibuf->depth==32) {
287                 buf[17] |= 0x08;
288         }
289         fildes = fopen(name,"wb");
290         if (!fildes) return 0;
291
292         if (fwrite(buf, 1, 18,fildes) != 18) return (0);
293
294         if (ibuf->cmap){
295                 for (i = 0 ; i<ibuf->maxcol ; i++){
296                         if (fwrite(((uchar *)(ibuf->cmap + i)) + 1,1,3,fildes) != 3) return (0);
297                 }
298         }
299         
300         if (ibuf->cmap && (flags & IB_cmap) == 0) IMB_converttocmap(ibuf);
301         
302         if (ibuf->ftype == RAWTGA) {
303                 ok = dumptarga(ibuf, fildes);
304         } else {                
305                 switch((ibuf->depth + 7) >> 3){
306                 case 1:
307                         ok = makebody_tga(ibuf, fildes, tga_out1);
308                         break;
309                 case 2:
310                         ok = makebody_tga(ibuf, fildes, tga_out2);
311                         break;
312                 case 3:
313                         ok = makebody_tga(ibuf, fildes, tga_out3);
314                         break;
315                 case 4:
316                         ok = makebody_tga(ibuf, fildes, tga_out4);
317                         break;
318                 }
319         }
320         
321         fclose(fildes);
322         return (ok);
323 }
324
325
326 static int checktarga(TARGA *tga, unsigned char *mem)
327 {
328         tga->numid = mem[0];
329         tga->maptyp = mem[1];
330         tga->imgtyp = mem[2];
331
332         tga->maporig = GSS(mem+3);
333         tga->mapsize = GSS(mem+5);
334         tga->mapbits = mem[7];
335         tga->xorig = GSS(mem+8);
336         tga->yorig = GSS(mem+10);
337         tga->xsize = GSS(mem+12);
338         tga->ysize = GSS(mem+14);
339         tga->pixsize = mem[16];
340         tga->imgdes = mem[17];
341
342         if (tga->maptyp > 1) return(0);
343         switch (tga->imgtyp){
344         case 1:                 /* raw cmap */
345         case 2:                 /* raw rgb */
346         case 3:                 /* raw b&w */
347         case 9:                 /* cmap */
348         case 10:                        /* rgb */
349         case 11:                        /* b&w */
350                 break;
351         default:
352                 return(0);
353         }
354         if (tga->mapsize && tga->mapbits > 32) return(0);
355         if (tga->xsize <= 0 || tga->xsize >= 8192) return(0);
356         if (tga->ysize <= 0 || tga->ysize >= 8192) return(0);
357         if (tga->pixsize > 32) return(0);
358         if (tga->pixsize == 0) return(0);
359         return(1);
360 }
361
362 int imb_is_a_targa(void *buf) {
363         TARGA tga;
364         
365         return checktarga(&tga, buf);
366 }
367
368 static void complete_partial_load(struct ImBuf *ibuf, unsigned int *rect)
369 {
370         int size = (ibuf->x * ibuf->y) - (rect - ibuf->rect);
371         if(size) {
372                 printf("decodetarga: incomplete file, %.1f%% missing\n", 100*((float)size / (ibuf->x * ibuf->y)));
373
374                 /* not essential but makes displaying partially rendered TGA's less ugly  */
375                 memset(rect, 0, size);
376         }
377         else {
378                 /* shouldnt happen */
379                 printf("decodetarga: incomplete file, all pixels written\n");
380         }
381 }
382
383 static void decodetarga(struct ImBuf *ibuf, unsigned char *mem, int mem_size, int psize)
384 {
385         unsigned char *mem_end = mem+mem_size;
386         int count, col, size;
387         unsigned int *rect;
388         uchar * cp = (uchar *) &col;
389         
390         if (ibuf == 0) return;
391         if (ibuf->rect == 0) return;
392
393         size = ibuf->x * ibuf->y;
394         rect = ibuf->rect;
395         
396         /* set alpha */
397         cp[0] = 0xff;
398         cp[1] = cp[2] = 0;
399
400         while(size > 0){
401                 count = *mem++;
402
403                 if(mem>mem_end)
404                         goto partial_load;
405
406                 if (count >= 128) {
407                         /*if (count == 128) printf("TARGA: 128 in file !\n");*/
408                         count -= 127;
409
410                         if (psize & 2){
411                                 if (psize & 1){
412                                         /* order = bgra */
413                                         cp[0] = mem[3];
414                                         cp[1] = mem[0];
415                                         cp[2] = mem[1];
416                                         cp[3] = mem[2];
417                                         /*col = (mem[3] << 24) + (mem[0] << 16) + (mem[1] << 8) + mem[2];*/
418                                         mem += 4;
419                                 } else{
420                                         cp[1] = mem[0];
421                                         cp[2] = mem[1];
422                                         cp[3] = mem[2];
423                                         /*col = 0xff000000 + (mem[0] << 16) + (mem[1] << 8) + mem[2];*/
424                                         mem += 3;
425                                 }
426                         } else{
427                                 if (psize & 1){
428                                         cp[0] = mem[0];
429                                         cp[1] = mem[1];
430                                         mem += 2;
431                                 } else{
432                                         col = *mem++;
433                                 }
434                         }
435
436                         size -= count;
437                         if (size >= 0) {
438                                 while (count > 0) {
439                                         *rect++ = col;
440                                         count--;
441                                 }
442                         }
443                 } else{
444                         count ++;
445                         size -= count;
446                         if (size >= 0) {
447                                 while (count > 0){
448                                         if (psize & 2){
449                                                 if (psize & 1){
450                                                         /* order = bgra */
451                                                         cp[0] = mem[3];
452                                                         cp[1] = mem[0];
453                                                         cp[2] = mem[1];
454                                                         cp[3] = mem[2];
455                                                         /*col = (mem[3] << 24) + (mem[0] << 16) + (mem[1] << 8) + mem[2];*/
456                                                         mem += 4;
457                                                 } else{
458                                                         cp[1] = mem[0];
459                                                         cp[2] = mem[1];
460                                                         cp[3] = mem[2];
461                                                         /*col = 0xff000000 + (mem[0] << 16) + (mem[1] << 8) + mem[2];*/
462                                                         mem += 3;
463                                                 }
464                                         } else{
465                                                 if (psize & 1){
466                                                         cp[0] = mem[0];
467                                                         cp[1] = mem[1];
468                                                         mem += 2;
469                                                 } else{
470                                                         col = *mem++;
471                                                 }
472                                         }
473                                         *rect++ = col;
474                                         count --;
475
476                                         if(mem>mem_end)
477                                                 goto partial_load;
478                                 }
479
480                                 if(mem>mem_end)
481                                         goto partial_load;
482                         }
483                 }
484         }
485         if (size) {
486                 printf("decodetarga: count would overwrite %d pixels\n", -size);
487         }
488         return;
489
490 partial_load:
491         complete_partial_load(ibuf, rect);
492 }
493
494 static void ldtarga(struct ImBuf * ibuf,unsigned char * mem, int mem_size, int psize)
495 {
496         unsigned char *mem_end = mem+mem_size;
497         int col,size;
498         unsigned int *rect;
499         uchar * cp = (uchar *) &col;
500
501         if (ibuf == 0) return;
502         if (ibuf->rect == 0) return;
503
504         size = ibuf->x * ibuf->y;
505         rect = ibuf->rect;
506
507         /* set alpha */
508         cp[0] = 0xff;
509         cp[1] = cp[2] = 0;
510
511         while(size > 0){
512                 if(mem>mem_end)
513                         goto partial_load;
514
515                 if (psize & 2){
516                         if (psize & 1){
517                                 /* order = bgra */
518                                 cp[0] = mem[3];
519                                 cp[1] = mem[0];
520                                 cp[2] = mem[1];
521                                 cp[3] = mem[2];
522                                 /*col = (mem[3] << 24) + (mem[0] << 16) + (mem[1] << 8) + mem[2];*/
523                                 mem += 4;
524                         } else{
525                                 /* set alpha for 24 bits colors */
526                                 cp[1] = mem[0];
527                                 cp[2] = mem[1];
528                                 cp[3] = mem[2];
529                                 /*col = 0xff000000 + (mem[0] << 16) + (mem[1] << 8) + mem[2];*/
530                                 mem += 3;
531                         }
532                 } else{
533                         if (psize & 1){
534                                 cp[0] = mem[0];
535                                 cp[1] = mem[1];
536                                 mem += 2;
537                         } else{
538                                 col = *mem++;
539                         }
540                 }
541                 *rect++ = col;
542                 size--;
543         }
544         return;
545
546 partial_load:
547         complete_partial_load(ibuf, rect);
548 }
549
550
551 struct ImBuf *imb_loadtarga(unsigned char *mem, int mem_size, int flags)
552 {
553         TARGA tga;
554         struct ImBuf * ibuf;
555         int col, count, size;
556         unsigned int * rect;
557         uchar * cp = (uchar *) &col;
558         
559         if (checktarga(&tga,mem) == 0) return(0);
560
561         if (flags & IB_test) ibuf = IMB_allocImBuf(tga.xsize,tga.ysize,tga.pixsize, 0, 0);
562         else ibuf = IMB_allocImBuf(tga.xsize,tga.ysize,(tga.pixsize + 0x7) & ~0x7, IB_rect, 0);
563
564         if (ibuf == 0) return(0);
565         ibuf->ftype = TGA;
566         ibuf->xorig = tga.xorig;
567         ibuf->yorig = tga.yorig;
568         mem = mem + 18 + tga.numid;
569         
570         cp[0] = 0xff;
571         cp[1] = cp[2] = 0;
572         
573         if (tga.mapsize){
574                 ibuf->mincol = tga.maporig;
575                 ibuf->maxcol = tga.mapsize;
576                 imb_addcmapImBuf(ibuf);
577                 ibuf->cbits = 8;
578                 for (count = 0 ; count < ibuf->maxcol ; count ++) {
579                         switch (tga.mapbits >> 3) {
580                                 case 4:
581                                         cp[0] = mem[3];
582                                         cp[1] = mem[0];
583                                         cp[2] = mem[1];
584                                         cp[3] = mem[2];
585                                         mem += 4;
586                                         break;
587                                 case 3:
588                                         cp[1] = mem[0];
589                                         cp[2] = mem[1];
590                                         cp[3] = mem[2];
591                                         mem += 3;
592                                         break;
593                                 case 2:
594                                         cp[1] = mem[1];
595                                         cp[0] = mem[0];
596                                         mem += 2;
597                                         break;
598                                 case 1:
599                                         col = *mem++;
600                                         break;
601                         }
602                         ibuf->cmap[count] = col;
603                 }
604                 
605                 size = 0;
606                 for (col = ibuf->maxcol - 1; col > 0; col >>= 1) size++;
607                 ibuf->depth = size;
608
609                 if (tga.mapbits != 32) {        /* set alpha bits  */
610                         ibuf->cmap[0] &= BIG_LONG(0x00ffffff);
611                 }
612         }
613         
614         if (flags & IB_test) return (ibuf);
615
616         if (tga.imgtyp != 1 && tga.imgtyp != 9) IMB_freecmapImBuf(ibuf); /* happens sometimes (beuh) */
617
618         switch(tga.imgtyp){
619         case 1:
620         case 2:
621         case 3:
622                 if (tga.pixsize <= 8) ldtarga(ibuf,mem,mem_size,0);
623                 else if (tga.pixsize <= 16) ldtarga(ibuf,mem,mem_size,1);
624                 else if (tga.pixsize <= 24) ldtarga(ibuf,mem,mem_size,2);
625                 else if (tga.pixsize <= 32) ldtarga(ibuf,mem,mem_size,3);
626                 break;
627         case 9:
628         case 10:
629         case 11:
630                 if (tga.pixsize <= 8) decodetarga(ibuf,mem,mem_size,0);
631                 else if (tga.pixsize <= 16) decodetarga(ibuf,mem,mem_size,1);
632                 else if (tga.pixsize <= 24) decodetarga(ibuf,mem,mem_size,2);
633                 else if (tga.pixsize <= 32) decodetarga(ibuf,mem,mem_size,3);
634                 break;
635         }
636         
637         if (ibuf->cmap){
638                 if ((flags & IB_cmap) == 0) IMB_applycmap(ibuf);
639         }
640         
641         if (tga.pixsize == 16 && ibuf->cmap == 0){
642                 rect = ibuf->rect;
643                 for (size = ibuf->x * ibuf->y; size > 0; --size, ++rect){
644                         col = *rect;
645                         cp = (uchar*)rect; 
646                         mem = (uchar*)&col;
647
648                         cp[3] = ((mem[1] << 1) & 0xf8);
649                         cp[2] = ((mem[0] & 0xe0) >> 2) + ((mem[1] & 0x03) << 6);
650                         cp[1] = ((mem[0] << 3) & 0xf8);
651                         cp[1] += cp[1] >> 5;
652                         cp[2] += cp[2] >> 5;
653                         cp[3] += cp[3] >> 5;
654                         cp[0] = 0xff;
655                 }
656                 ibuf->depth = 24;
657         }
658         
659         if (tga.imgtyp == 3 || tga.imgtyp == 11){
660                 uchar *crect;
661                 unsigned int *lrect, col;
662                 
663                 crect = (uchar *) ibuf->rect;
664                 lrect = (unsigned int *) ibuf->rect;
665                 
666                 for (size = ibuf->x * ibuf->y; size > 0; size --){
667                         col = *lrect++;
668                         
669                         crect[0] = 255;
670                         crect[1] = crect[2] = crect[3] = col;
671                         crect += 4;
672                 }
673         }
674         
675         if (flags & IB_ttob) tga.imgdes ^= 0x20;
676         if (tga.imgdes & 0x20) IMB_flipy(ibuf);
677
678         if (ibuf) {
679                 if (ibuf->rect && (flags & IB_cmap)==0) 
680                         IMB_convert_rgba_to_abgr(ibuf);
681         }
682         
683         return(ibuf);
684 }