Blender V2.27

targa.c

00001 
00030 #ifdef WIN32
00031 #include <io.h>
00032 #endif
00033 #include "BLI_blenlib.h"
00034 #include "MEM_guardedalloc.h"
00035 
00036 #include "imbuf.h"
00037 
00038 #include "IMB_imbuf_types.h"
00039 #include "IMB_imbuf.h"
00040 
00041 #include "IMB_allocimbuf.h"
00042 #include "IMB_filetype.h"
00043 
00044 
00045 /* this one is only def-ed once, strangely... related to GS? */
00046 #define GSS(x) (((uchar *)(x))[1] << 8 | ((uchar *)(x))[0])
00047 
00048 /***/
00049 
00050 typedef struct TARGA 
00051 {
00052         unsigned char numid;    
00053         unsigned char maptyp;
00054         unsigned char imgtyp;   
00055         short maporig;
00056         short mapsize;
00057         unsigned char mapbits;
00058         short xorig;
00059         short yorig;
00060         short xsize;
00061         short ysize;
00062         unsigned char pixsize;
00063         unsigned char imgdes;
00064 } TARGA;
00065 
00066 /***/
00067 
00068 static int tga_out1(unsigned int data, FILE *file)
00069 {
00070         uchar *p;
00071 
00072         p = (uchar *) & data;
00073         if (putc(p[0],file) == EOF) return(EOF);
00074         return (~EOF);
00075 }
00076 
00077 static int tga_out2(unsigned int data, FILE * file)
00078 {
00079         uchar *p;
00080 
00081         p = (uchar *) & data;
00082         if (putc(p[0],file) == EOF) return(EOF);
00083         if (putc(p[1],file) == EOF) return(EOF);
00084         return (~EOF);
00085 }
00086 
00087 
00088 static int tga_out3(unsigned int data, FILE * file)
00089 {
00090         uchar *p;
00091 
00092         p = (uchar *) & data;
00093         if (putc(p[2],file) == EOF) return(EOF);
00094         if (putc(p[1],file) == EOF) return(EOF);
00095         if (putc(p[0],file) == EOF) return(EOF);
00096         return (~EOF);
00097 }
00098 
00099 
00100 static int tga_out4(unsigned int data, FILE * file)
00101 {
00102         uchar *p;
00103 
00104         p = (uchar *) & data;
00105         /* order = bgra */
00106         if (putc(p[2],file) == EOF) return(EOF);
00107         if (putc(p[1],file) == EOF) return(EOF);
00108         if (putc(p[0],file) == EOF) return(EOF);
00109         if (putc(p[3],file) == EOF) return(EOF);
00110         return (~EOF);
00111 }
00112 
00113 static short makebody_tga(ImBuf * ibuf, FILE * file, int (*out)(unsigned int, FILE*))
00114 {
00115         register int last,this;
00116         register int copy, bytes;
00117         register unsigned int *rect, *rectstart, *temp;
00118         int y;
00119         
00120         for (y = 0; y < ibuf->y; y++) {
00121                 bytes = ibuf->x - 1;
00122                 rectstart = rect = ibuf->rect + (y * ibuf->x);
00123                 last = *rect++;
00124                 this = *rect++;
00125                 copy = last^this;
00126                 while (bytes > 0){
00127                         if (copy){
00128                                 do{
00129                                         last = this;
00130                                         this = *rect++;
00131                                         if (last == this){
00132                                                 if (this == rect[-3]){  /* three the same? */
00133                                                         bytes --;               /* set bytes */
00134                                                         break;
00135                                                 }
00136                                         }
00137                                 }while (--bytes != 0);
00138 
00139                                 copy = rect-rectstart;
00140                                 copy --;
00141                                 if (bytes) copy -= 2;
00142 
00143                                 temp = rect;
00144                                 rect = rectstart;
00145 
00146                                 while (copy){
00147                                         last = copy;
00148                                         if (copy>=128) last = 128;
00149                                         copy -= last;
00150                                         if (fputc(last-1,file) == EOF) return(0);
00151                                         do{
00152                                                 if (out(*rect++,file) == EOF) return(0);
00153                                         }while(--last != 0);
00154                                 }
00155                                 rectstart = rect;
00156                                 rect = temp;
00157                                 last = this;
00158 
00159                                 copy = FALSE;
00160                         } else {
00161                                 while (*rect++ == this){                /* seek for first different byte */
00162                                         if (--bytes == 0) break;        /* oor end of line */
00163                                 }
00164                                 rect --;
00165                                 copy = rect-rectstart;
00166                                 rectstart = rect;
00167                                 bytes --;
00168                                 this = *rect++;
00169 
00170                                 while (copy){
00171                                         if (copy>128){
00172                                                 if (fputc(255,file) == EOF) return(0);
00173                                                 copy -= 128;
00174                                         } else {
00175                                                 if (copy == 1){
00176                                                         if (fputc(0,file) == EOF) return(0);
00177                                                 } else if (fputc(127 + copy,file) == EOF) return(0);
00178                                                 copy = 0;
00179                                         }
00180                                         if (out(last,file) == EOF) return(0);
00181                                 }
00182                                 copy=TRUE;
00183                         }
00184                 }
00185         }
00186         return (1);
00187 }
00188 
00189 static int dumptarga(struct ImBuf * ibuf, FILE * file)
00190 {
00191         int size;
00192         uchar *rect;
00193 
00194         if (ibuf == 0) return (0);
00195         if (ibuf->rect == 0) return (0);
00196 
00197         size = ibuf->x * ibuf->y;
00198         rect = (uchar *) ibuf->rect;
00199 
00200         if (ibuf->depth <= 8) {
00201                 while(size > 0){
00202                         if (putc(*rect, file) == EOF) return (0);
00203                         size--;
00204                         rect += 4;
00205                 }
00206         } else if (ibuf->depth <= 16) {
00207                 while(size > 0){
00208                         putc(rect[0], file);
00209                         if (putc(rect[1], file) == EOF) return (0);
00210                         size--;
00211                         rect += 4;
00212                 }
00213         } else if (ibuf->depth <= 24) {
00214                 while(size > 0){
00215                         putc(rect[2], file);
00216                         putc(rect[1], file);
00217                         if (putc(rect[0], file) == EOF) return (0);
00218                         size--;
00219                         rect += 4;
00220                 }
00221         } else if (ibuf->depth <= 32) {
00222                 while(size > 0){
00223                         putc(rect[2], file);
00224                         putc(rect[1], file);
00225                         putc(rect[0], file);
00226                         if (putc(rect[3], file) == EOF) return (0);
00227                         size--;
00228                         rect += 4;
00229                 }
00230         } else return (0);
00231         
00232         return (1);
00233 }
00234 
00235 
00236 int imb_savetarga(struct ImBuf * ibuf, const char *name, int flags)
00237 {
00238         char buf[20]= {0};
00239         FILE *fildes;
00240         short ok = 0;
00241         
00242         (void)flags; /* unused */
00243 
00244         buf[16] = (ibuf->depth + 0x7 ) & ~0x7;
00245         if (ibuf->depth > 8 ){
00246                 buf[2] = 10;
00247         } else{
00248                 buf[2] = 11;
00249         }
00250 
00251         if (ibuf->ftype == RAWTGA) buf[2] &= ~8;
00252         
00253         buf[8] = 0;
00254         buf[9] = 0;
00255         buf[10] = 0;
00256         buf[11] = 0;
00257 
00258         buf[12] = ibuf->x & 0xff;
00259         buf[13] = ibuf->x >> 8;
00260         buf[14] = ibuf->y & 0xff;
00261         buf[15] = ibuf->y >> 8;
00262 
00263         /* Don't forget to indicate that your 32 bit
00264          * targa uses 8 bits for the alpha channel! */
00265         if (ibuf->depth==32) {
00266                 buf[17] |= 0x08;
00267         }
00268         fildes = fopen(name,"wb");
00269                 if (!fildes) return 0;
00270 
00271         if (fwrite(buf, 1, 18,fildes) != 18) {
00272                 fclose(fildes);
00273                 return (0);
00274         }
00275 
00276         if (ibuf->ftype == RAWTGA) {
00277                 ok = dumptarga(ibuf, fildes);
00278         } else {                
00279                 switch((ibuf->depth + 7) >> 3){
00280                 case 1:
00281                         ok = makebody_tga(ibuf, fildes, tga_out1);
00282                         break;
00283                 case 2:
00284                         ok = makebody_tga(ibuf, fildes, tga_out2);
00285                         break;
00286                 case 3:
00287                         ok = makebody_tga(ibuf, fildes, tga_out3);
00288                         break;
00289                 case 4:
00290                         ok = makebody_tga(ibuf, fildes, tga_out4);
00291                         break;
00292                 }
00293         }
00294         
00295         fclose(fildes);
00296         return (ok);
00297 }
00298 
00299 
00300 static int checktarga(TARGA *tga, unsigned char *mem)
00301 {
00302         tga->numid = mem[0];
00303         tga->maptyp = mem[1];
00304         tga->imgtyp = mem[2];
00305 
00306         tga->maporig = GSS(mem+3);
00307         tga->mapsize = GSS(mem+5);
00308         tga->mapbits = mem[7];
00309         tga->xorig = GSS(mem+8);
00310         tga->yorig = GSS(mem+10);
00311         tga->xsize = GSS(mem+12);
00312         tga->ysize = GSS(mem+14);
00313         tga->pixsize = mem[16];
00314         tga->imgdes = mem[17];
00315 
00316         if (tga->maptyp > 1) return(0);
00317         switch (tga->imgtyp){
00318         case 1:                 /* raw cmap */
00319         case 2:                 /* raw rgb */
00320         case 3:                 /* raw b&w */
00321         case 9:                 /* cmap */
00322         case 10:                        /* rgb */
00323         case 11:                        /* b&w */
00324                 break;
00325         default:
00326                 return(0);
00327         }
00328         if (tga->mapsize && tga->mapbits > 32) return(0);
00329         if (tga->xsize <= 0 || tga->xsize >= 8192) return(0);
00330         if (tga->ysize <= 0 || tga->ysize >= 8192) return(0);
00331         if (tga->pixsize > 32) return(0);
00332         if (tga->pixsize == 0) return(0);
00333         return(1);
00334 }
00335 
00336 int imb_is_a_targa(unsigned char *buf) {
00337         TARGA tga;
00338         
00339         return checktarga(&tga, buf);
00340 }
00341 
00342 static void complete_partial_load(struct ImBuf *ibuf, unsigned int *rect)
00343 {
00344         int size = (ibuf->x * ibuf->y) - (rect - ibuf->rect);
00345         if(size) {
00346                 printf("decodetarga: incomplete file, %.1f%% missing\n", 100*((float)size / (ibuf->x * ibuf->y)));
00347 
00348                 /* not essential but makes displaying partially rendered TGA's less ugly  */
00349                 memset(rect, 0, size);
00350         }
00351         else {
00352                 /* shouldnt happen */
00353                 printf("decodetarga: incomplete file, all pixels written\n");
00354         }
00355 }
00356 
00357 static void decodetarga(struct ImBuf *ibuf, unsigned char *mem, size_t mem_size, int psize)
00358 {
00359         unsigned char *mem_end = mem+mem_size;
00360         int count, col, size;
00361         unsigned int *rect;
00362         uchar * cp = (uchar *) &col;
00363         
00364         if (ibuf == 0) return;
00365         if (ibuf->rect == 0) return;
00366 
00367         size = ibuf->x * ibuf->y;
00368         rect = ibuf->rect;
00369         
00370         /* set alpha */
00371         cp[0] = 0xff;
00372         cp[1] = cp[2] = 0;
00373 
00374         while(size > 0){
00375                 count = *mem++;
00376 
00377                 if(mem>mem_end)
00378                         goto partial_load;
00379 
00380                 if (count >= 128) {
00381                         /*if (count == 128) printf("TARGA: 128 in file !\n");*/
00382                         count -= 127;
00383 
00384                         if (psize & 2){
00385                                 if (psize & 1){
00386                                         /* order = bgra */
00387                                         cp[0] = mem[3];
00388                                         cp[1] = mem[0];
00389                                         cp[2] = mem[1];
00390                                         cp[3] = mem[2];
00391                                         /*col = (mem[3] << 24) + (mem[0] << 16) + (mem[1] << 8) + mem[2];*/
00392                                         mem += 4;
00393                                 } else{
00394                                         cp[1] = mem[0];
00395                                         cp[2] = mem[1];
00396                                         cp[3] = mem[2];
00397                                         /*col = 0xff000000 + (mem[0] << 16) + (mem[1] << 8) + mem[2];*/
00398                                         mem += 3;
00399                                 }
00400                         } else{
00401                                 if (psize & 1){
00402                                         cp[0] = mem[0];
00403                                         cp[1] = mem[1];
00404                                         mem += 2;
00405                                 } else{
00406                                         col = *mem++;
00407                                 }
00408                         }
00409 
00410                         size -= count;
00411                         if (size >= 0) {
00412                                 while (count > 0) {
00413                                         *rect++ = col;
00414                                         count--;
00415                                 }
00416                         }
00417                 } else{
00418                         count ++;
00419                         size -= count;
00420                         if (size >= 0) {
00421                                 while (count > 0){
00422                                         if (psize & 2){
00423                                                 if (psize & 1){
00424                                                         /* order = bgra */
00425                                                         cp[0] = mem[3];
00426                                                         cp[1] = mem[0];
00427                                                         cp[2] = mem[1];
00428                                                         cp[3] = mem[2];
00429                                                         /*col = (mem[3] << 24) + (mem[0] << 16) + (mem[1] << 8) + mem[2];*/
00430                                                         mem += 4;
00431                                                 } else{
00432                                                         cp[1] = mem[0];
00433                                                         cp[2] = mem[1];
00434                                                         cp[3] = mem[2];
00435                                                         /*col = 0xff000000 + (mem[0] << 16) + (mem[1] << 8) + mem[2];*/
00436                                                         mem += 3;
00437                                                 }
00438                                         } else{
00439                                                 if (psize & 1){
00440                                                         cp[0] = mem[0];
00441                                                         cp[1] = mem[1];
00442                                                         mem += 2;
00443                                                 } else{
00444                                                         col = *mem++;
00445                                                 }
00446                                         }
00447                                         *rect++ = col;
00448                                         count --;
00449 
00450                                         if(mem>mem_end)
00451                                                 goto partial_load;
00452                                 }
00453 
00454                                 if(mem>mem_end)
00455                                         goto partial_load;
00456                         }
00457                 }
00458         }
00459         if (size) {
00460                 printf("decodetarga: count would overwrite %d pixels\n", -size);
00461         }
00462         return;
00463 
00464 partial_load:
00465         complete_partial_load(ibuf, rect);
00466 }
00467 
00468 static void ldtarga(struct ImBuf * ibuf,unsigned char * mem, size_t mem_size, int psize)
00469 {
00470         unsigned char *mem_end = mem+mem_size;
00471         int col,size;
00472         unsigned int *rect;
00473         uchar * cp = (uchar *) &col;
00474 
00475         if (ibuf == 0) return;
00476         if (ibuf->rect == 0) return;
00477 
00478         size = ibuf->x * ibuf->y;
00479         rect = ibuf->rect;
00480 
00481         /* set alpha */
00482         cp[0] = 0xff;
00483         cp[1] = cp[2] = 0;
00484 
00485         while(size > 0){
00486                 if(mem>mem_end)
00487                         goto partial_load;
00488 
00489                 if (psize & 2){
00490                         if (psize & 1){
00491                                 /* order = bgra */
00492                                 cp[0] = mem[3];
00493                                 cp[1] = mem[0];
00494                                 cp[2] = mem[1];
00495                                 cp[3] = mem[2];
00496                                 /*col = (mem[3] << 24) + (mem[0] << 16) + (mem[1] << 8) + mem[2];*/
00497                                 mem += 4;
00498                         } else{
00499                                 /* set alpha for 24 bits colors */
00500                                 cp[1] = mem[0];
00501                                 cp[2] = mem[1];
00502                                 cp[3] = mem[2];
00503                                 /*col = 0xff000000 + (mem[0] << 16) + (mem[1] << 8) + mem[2];*/
00504                                 mem += 3;
00505                         }
00506                 } else{
00507                         if (psize & 1){
00508                                 cp[0] = mem[0];
00509                                 cp[1] = mem[1];
00510                                 mem += 2;
00511                         } else{
00512                                 col = *mem++;
00513                         }
00514                 }
00515                 *rect++ = col;
00516                 size--;
00517         }
00518         return;
00519 
00520 partial_load:
00521         complete_partial_load(ibuf, rect);
00522 }
00523 
00524 
00525 struct ImBuf *imb_loadtarga(unsigned char *mem, size_t mem_size, int flags)
00526 {
00527         TARGA tga;
00528         struct ImBuf * ibuf;
00529         int col, count, size;
00530         unsigned int *rect, *cmap= NULL, mincol= 0, maxcol= 0;
00531         uchar * cp = (uchar *) &col;
00532         
00533         if (checktarga(&tga,mem) == 0) return(0);
00534 
00535         if (flags & IB_test) ibuf = IMB_allocImBuf(tga.xsize,tga.ysize,tga.pixsize, 0);
00536         else ibuf = IMB_allocImBuf(tga.xsize,tga.ysize,(tga.pixsize + 0x7) & ~0x7, IB_rect);
00537 
00538         if (ibuf == 0) return(0);
00539         ibuf->ftype = TGA;
00540         ibuf->profile = IB_PROFILE_SRGB;
00541         mem = mem + 18 + tga.numid;
00542         
00543         cp[0] = 0xff;
00544         cp[1] = cp[2] = 0;
00545         
00546         if (tga.mapsize){
00547                 /* load color map */
00548                 mincol = tga.maporig;
00549                 maxcol = tga.mapsize;
00550                 cmap = MEM_callocN(sizeof(unsigned int)*maxcol, "targa cmap");
00551 
00552                 for (count = 0 ; count < maxcol ; count ++) {
00553                         switch (tga.mapbits >> 3) {
00554                                 case 4:
00555                                         cp[0] = mem[3];
00556                                         cp[1] = mem[0];
00557                                         cp[2] = mem[1];
00558                                         cp[3] = mem[2];
00559                                         mem += 4;
00560                                         break;
00561                                 case 3:
00562                                         cp[1] = mem[0];
00563                                         cp[2] = mem[1];
00564                                         cp[3] = mem[2];
00565                                         mem += 3;
00566                                         break;
00567                                 case 2:
00568                                         cp[1] = mem[1];
00569                                         cp[0] = mem[0];
00570                                         mem += 2;
00571                                         break;
00572                                 case 1:
00573                                         col = *mem++;
00574                                         break;
00575                         }
00576                         cmap[count] = col;
00577                 }
00578                 
00579                 size = 0;
00580                 for (col = maxcol - 1; col > 0; col >>= 1) size++;
00581                 ibuf->depth = size;
00582 
00583                 if (tga.mapbits != 32) {        /* set alpha bits  */
00584                         cmap[0] &= BIG_LONG(0x00ffffff);
00585                 }
00586         }
00587         
00588         if (flags & IB_test) return (ibuf);
00589 
00590         if (tga.imgtyp != 1 && tga.imgtyp != 9) { /* happens sometimes (beuh) */
00591                 if(cmap) {
00592                         MEM_freeN(cmap); 
00593                         cmap= NULL;
00594                 }
00595         }
00596 
00597         switch(tga.imgtyp){
00598         case 1:
00599         case 2:
00600         case 3:
00601                 if (tga.pixsize <= 8) ldtarga(ibuf,mem,mem_size,0);
00602                 else if (tga.pixsize <= 16) ldtarga(ibuf,mem,mem_size,1);
00603                 else if (tga.pixsize <= 24) ldtarga(ibuf,mem,mem_size,2);
00604                 else if (tga.pixsize <= 32) ldtarga(ibuf,mem,mem_size,3);
00605                 break;
00606         case 9:
00607         case 10:
00608         case 11:
00609                 if (tga.pixsize <= 8) decodetarga(ibuf,mem,mem_size,0);
00610                 else if (tga.pixsize <= 16) decodetarga(ibuf,mem,mem_size,1);
00611                 else if (tga.pixsize <= 24) decodetarga(ibuf,mem,mem_size,2);
00612                 else if (tga.pixsize <= 32) decodetarga(ibuf,mem,mem_size,3);
00613                 break;
00614         }
00615         
00616         if(cmap) {
00617                 /* apply color map */
00618                 rect = ibuf->rect;
00619                 for(size = ibuf->x * ibuf->y; size>0; --size, ++rect) {
00620                         col = *rect;
00621                         if (col >= 0 && col < maxcol) *rect = cmap[col];
00622                 }
00623 
00624                 MEM_freeN(cmap);
00625         }
00626         
00627         if (tga.pixsize == 16) {
00628                 rect = ibuf->rect;
00629                 for (size = ibuf->x * ibuf->y; size > 0; --size, ++rect){
00630                         col = *rect;
00631                         cp = (uchar*)rect; 
00632                         mem = (uchar*)&col;
00633 
00634                         cp[3] = ((mem[1] << 1) & 0xf8);
00635                         cp[2] = ((mem[0] & 0xe0) >> 2) + ((mem[1] & 0x03) << 6);
00636                         cp[1] = ((mem[0] << 3) & 0xf8);
00637                         cp[1] += cp[1] >> 5;
00638                         cp[2] += cp[2] >> 5;
00639                         cp[3] += cp[3] >> 5;
00640                         cp[0] = 0xff;
00641                 }
00642                 ibuf->depth = 24;
00643         }
00644         
00645         if (tga.imgtyp == 3 || tga.imgtyp == 11){
00646                 uchar *crect;
00647                 unsigned int *lrect, col;
00648                 
00649                 crect = (uchar *) ibuf->rect;
00650                 lrect = (unsigned int *) ibuf->rect;
00651                 
00652                 for (size = ibuf->x * ibuf->y; size > 0; size --){
00653                         col = *lrect++;
00654                         
00655                         crect[0] = 255;
00656                         crect[1] = crect[2] = crect[3] = col;
00657                         crect += 4;
00658                 }
00659         }
00660         
00661         if (tga.imgdes & 0x20) IMB_flipy(ibuf);
00662 
00663         if (ibuf && ibuf->rect)
00664                 IMB_convert_rgba_to_abgr(ibuf);
00665         
00666         return(ibuf);
00667 }