150 lines
5.0 KiB
C
150 lines
5.0 KiB
C
#include "RS_images.h"
|
|
|
|
|
|
//extern int _options; // Global options settings variable
|
|
|
|
void decodePixels(RS_IMAGE *img) {
|
|
int size;
|
|
|
|
//img->pixels = NULL;
|
|
if (!(img->type_ == 0 ||
|
|
img->type_ == 1 ||
|
|
img->type_ == 2 ||
|
|
img->type_ == 3 ||
|
|
img->type_ == 4 ||
|
|
img->type_ == 5)) return;
|
|
|
|
size = img->height * img->width;
|
|
switch (img->sampleBits) {
|
|
case 32:
|
|
img->pixels = calloc(1, size * sizeof(PIXEL_A));
|
|
memcpy(img->pixels, img->samples, size * sizeof(PIXEL_A));
|
|
break;
|
|
case 4:
|
|
img->pixels = calloc(1, size * sizeof(PIXEL_A));
|
|
if (img->paletteEntries == 0) {
|
|
convert4bitsGreyTo32bitsRGBA(img->samples, img->pixels, size, &(img->alpha_color));
|
|
} else if (img->paletteEntries == 16) {
|
|
convert4bitsTo32bitsRGBA(img->samples, img->pixels, size, img->palette, &(img->alpha_color));
|
|
}
|
|
break;
|
|
case 8:
|
|
if (img->paletteEntries == 0) {
|
|
img->pixels = calloc(1, size);
|
|
memcpy(img->pixels, img->samples, size);
|
|
} else if (img->paletteEntries == 256) {
|
|
img->pixels = calloc(1, size * sizeof(PIXEL_A));
|
|
convert8bitsTo32bitsRGBA(img->samples, img->pixels, size, img->palette, &(img->alpha_color));
|
|
}
|
|
break;
|
|
case 16:
|
|
img->pixels = calloc(1, size);
|
|
useOddBytes(img->samples, img->pixels, size);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
int isTransparentColor(PIXEL_A *testColor, PIXEL_A *transp_color) {
|
|
if (transp_color == NULL || testColor == NULL) return -2;
|
|
|
|
if (!(testColor->_red == transp_color->_red)) return -1;
|
|
if (!(testColor->_green == transp_color->_green)) return -1;
|
|
if (!(testColor->_blue == transp_color->_blue)) return -1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
void convert4bitsGreyTo32bitsRGBA(unsigned char *samples_tab, PIXEL_A *pixels_tab, int sampling, PIXEL_A *transp_color) {
|
|
int i;
|
|
unsigned char v;
|
|
|
|
for(i=0; i<div(sampling,2).quot; i++) {
|
|
v = samples_tab[i];
|
|
pixels_tab[i*2]._red = div((v >> 4 & 0xF) * 256, 16).quot;
|
|
pixels_tab[i*2]._green = div((v >> 4 & 0xF) * 256, 16).quot;
|
|
pixels_tab[i*2]._blue = div((v >> 4 & 0xF) * 256, 16).quot;
|
|
if (isTransparentColor(&(pixels_tab[i*2]), transp_color) == 0) pixels_tab[i*2]._alpha = 0xFF - transp_color->_alpha; else pixels_tab[i*2]._alpha = 0xFF; // Test if color is a transparent color and adjust alpha layer
|
|
pixels_tab[i*2+1]._red = div((v & 0xF) * 256, 16).quot;
|
|
pixels_tab[i*2+1]._green = div((v & 0xF) * 256, 16).quot;
|
|
pixels_tab[i*2+1]._blue = div((v & 0xF) * 256, 16).quot;
|
|
if (isTransparentColor(&(pixels_tab[i*2+1]), transp_color) == 0) pixels_tab[i*2+1]._alpha = 0xFF - transp_color->_alpha; else pixels_tab[i*2+1]._alpha = 0xFF; // Test if color is a transparent color and adjust alpha layer
|
|
}
|
|
}
|
|
|
|
void convert4bitsTo32bitsRGBA(unsigned char *samples_tab, PIXEL_A *pixels_tab, int size, PIXEL *pal, PIXEL_A *transp_color) {
|
|
int i,index;
|
|
|
|
for(i=0; i<div(size,2).quot; i++) {
|
|
index = samples_tab[i];
|
|
pixels_tab[i*2]._red = pal[(index >> 4) & 0xF]._red;
|
|
pixels_tab[i*2]._green = pal[(index >> 4) & 0xF]._green;
|
|
pixels_tab[i*2]._blue = pal[(index >> 4) & 0xF]._blue;
|
|
if (isTransparentColor(&(pixels_tab[i*2]), transp_color) == 0) pixels_tab[i*2]._alpha = 0xFF - transp_color->_alpha; else pixels_tab[i*2]._alpha = 0xFF; // Test if color is a transparent color and adjust alpha layer
|
|
pixels_tab[i*2+1]._red = pal[index & 0xF]._red;
|
|
pixels_tab[i*2+1]._green = pal[index & 0xF]._green;
|
|
pixels_tab[i*2+1]._blue = pal[index & 0xF]._blue;
|
|
if (isTransparentColor(&(pixels_tab[i*2+1]), transp_color) == 0) pixels_tab[i*2]._alpha = 0xFF - transp_color->_alpha; else pixels_tab[i*2+1]._alpha = 0xFF; // Test if color is a transparent color and adjust alpha layer
|
|
}
|
|
}
|
|
|
|
void convert8bitsTo32bitsRGBA(unsigned char *samples_tab, PIXEL_A *pixels_tab, int size, PIXEL *pal, PIXEL_A *transp_color) {
|
|
int i,index;
|
|
|
|
for(i=0; i<size; i++) {
|
|
index = samples_tab[i];
|
|
pixels_tab[i]._red = pal[index]._red;
|
|
pixels_tab[i]._green = pal[index]._green;
|
|
pixels_tab[i]._blue = pal[index]._blue;
|
|
if (isTransparentColor(&(pixels_tab[i]), transp_color) == 0) pixels_tab[i]._alpha = 0xFF - transp_color->_alpha; else pixels_tab[i]._alpha = 0xFF; // Test if color is a transparent color and adjust alpha layer
|
|
}
|
|
}
|
|
|
|
void useOddBytes(unsigned char *src, PIXEL_A *dst, int size) {
|
|
int i;
|
|
|
|
for(i=0; i<size; i++) {
|
|
//dst[i] = src[i*2+1];
|
|
}
|
|
}
|
|
|
|
PIXEL_A *pixelAt(RS_IMAGE *img, int posX , int posY) {
|
|
return img->pixels + img->width * posY + posX;
|
|
}
|
|
|
|
RS_IMAGE_DESC getImageDescFromType(unsigned char type) {
|
|
RS_IMAGE_DESC desc;
|
|
|
|
switch(type) {
|
|
case 0:
|
|
desc.palette_entries = 16;
|
|
desc.sample_bits = 4;
|
|
break;
|
|
case 1:
|
|
desc.palette_entries = 256;
|
|
desc.sample_bits = 8;
|
|
break;
|
|
case 2:
|
|
desc.palette_entries = 0;
|
|
desc.sample_bits = 16;
|
|
break;
|
|
case 3:
|
|
desc.palette_entries = 0;
|
|
desc.sample_bits = 32;
|
|
break;
|
|
case 4:
|
|
desc.palette_entries = 0;
|
|
desc.sample_bits = 4;
|
|
break;
|
|
case 5:
|
|
desc.palette_entries = 0;
|
|
desc.sample_bits = 8;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return desc;
|
|
}
|