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