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