IMG_tga.c
changeset 117 e613cf987897
parent 98 9f94c4674cc9
child 118 c5e736a47ad2
equal deleted inserted replaced
116:df32aeaf0b10 117:e613cf987897
    81 
    81 
    82 /* read/write unaligned little-endian 16-bit ints */
    82 /* read/write unaligned little-endian 16-bit ints */
    83 #define LE16(p) ((p)[0] + ((p)[1] << 8))
    83 #define LE16(p) ((p)[0] + ((p)[1] << 8))
    84 #define SETLE16(p, v) ((p)[0] = (v), (p)[1] = (v) >> 8)
    84 #define SETLE16(p, v) ((p)[0] = (v), (p)[1] = (v) >> 8)
    85 
    85 
    86 static void unsupported(void)
       
    87 {
       
    88     IMG_SetError("unsupported TGA format");
       
    89 }
       
    90 
       
    91 /* Load a TGA type image from an SDL datasource */
    86 /* Load a TGA type image from an SDL datasource */
    92 SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
    87 SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
    93 {
    88 {
       
    89     int start;
    94     struct TGAheader hdr;
    90     struct TGAheader hdr;
    95     int rle = 0;
    91     int rle = 0;
    96     int alpha = 0;
    92     int alpha = 0;
    97     int indexed = 0;
    93     int indexed = 0;
    98     int grey = 0;
    94     int grey = 0;
   110     if ( !src ) {
   106     if ( !src ) {
   111         /* The error message has been set in SDL_RWFromFile */
   107         /* The error message has been set in SDL_RWFromFile */
   112         return NULL;
   108         return NULL;
   113     }
   109     }
   114 
   110 
       
   111     start = SDL_RWtell(src);
   115     if(!SDL_RWread(src, &hdr, sizeof(hdr), 1))
   112     if(!SDL_RWread(src, &hdr, sizeof(hdr), 1))
   116 	goto error;
   113 	goto error;
   117     ncols = LE16(hdr.cmap_len);
   114     ncols = LE16(hdr.cmap_len);
   118     switch(hdr.type) {
   115     switch(hdr.type) {
   119     case TGA_TYPE_RLE_INDEXED:
   116     case TGA_TYPE_RLE_INDEXED:
   141 	/* Treat greyscale as 8bpp indexed images */
   138 	/* Treat greyscale as 8bpp indexed images */
   142 	indexed = grey = 1;
   139 	indexed = grey = 1;
   143 	break;
   140 	break;
   144 
   141 
   145     default:
   142     default:
   146         unsupported();
   143         goto unsupported;
   147 	return NULL;
       
   148     }
   144     }
   149 
   145 
   150     bpp = (hdr.pixel_bits + 7) >> 3;
   146     bpp = (hdr.pixel_bits + 7) >> 3;
   151     rmask = gmask = bmask = amask = 0;
   147     rmask = gmask = bmask = amask = 0;
   152     switch(hdr.pixel_bits) {
   148     switch(hdr.pixel_bits) {
   153     case 8:
   149     case 8:
   154 	if(!indexed) {
   150 	if(!indexed) {
   155 	    unsupported();
   151             goto unsupported;
   156 	    return NULL;
       
   157 	}
   152 	}
   158 	break;
   153 	break;
   159 
   154 
   160     case 15:
   155     case 15:
   161     case 16:
   156     case 16:
   183 	    bmask = 0x000000ff;
   178 	    bmask = 0x000000ff;
   184 	}
   179 	}
   185 	break;
   180 	break;
   186 
   181 
   187     default:
   182     default:
   188 	unsupported();
   183         goto unsupported;
   189 	return NULL;
       
   190     }
   184     }
   191 
   185 
   192     if((hdr.flags & TGA_INTERLEAVE_MASK) != TGA_INTERLEAVE_NONE
   186     if((hdr.flags & TGA_INTERLEAVE_MASK) != TGA_INTERLEAVE_NONE
   193        || hdr.flags & TGA_ORIGIN_RIGHT) {
   187        || hdr.flags & TGA_ORIGIN_RIGHT) {
   194 	unsupported();
   188         goto unsupported;
   195 	return NULL;
       
   196     }
   189     }
   197     
   190     
   198     SDL_RWseek(src, hdr.infolen, SEEK_CUR); /* skip info field */
   191     SDL_RWseek(src, hdr.infolen, SEEK_CUR); /* skip info field */
   199 
   192 
   200     w = LE16(hdr.width);
   193     w = LE16(hdr.width);
   309 	dst += lstep;
   302 	dst += lstep;
   310     }
   303     }
   311     return img;
   304     return img;
   312 
   305 
   313 error:
   306 error:
       
   307     SDL_RWseek(src, start, SEEK_SET);
   314     IMG_SetError("Error reading TGA data");
   308     IMG_SetError("Error reading TGA data");
       
   309     return NULL;
       
   310 
       
   311 unsupported:
       
   312     SDL_RWseek(src, start, SEEK_SET);
       
   313     IMG_SetError("unsupported TGA format");
   315     return NULL;
   314     return NULL;
   316 }
   315 }
   317 
   316 
   318 #else
   317 #else
   319 
   318