Skip to content

Commit

Permalink
Refactor: All routines in gl_tex.c/h which use ColorPalettes are now …
Browse files Browse the repository at this point in the history
…passed pointers to them rather than their associated indices in the list owned by the refresh module. R_ToColorPalette now results in a fatal error if given an invalid palette index.
  • Loading branch information
danij-deng committed Apr 7, 2011
1 parent b66f933 commit 8aeb8d2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 61 deletions.
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/colorpalette.h
Expand Up @@ -85,7 +85,7 @@ void ColorPalette_ReplaceColorTable(colorpalette_t* pal, const int compOrder[3],
* @param colorIdx Index of the color to lookup.
* @param rgb Associated R8G8B8 color triplet is written here.
*/
void ColorPalette_Color(colorpalette_t* pal, int colorIdx, uint8_t rgb[3]);
void ColorPalette_Color(const colorpalette_t* pal, int colorIdx, uint8_t rgb[3]);

/**
* Given an R8G8B8 color triplet return the closet matching color index.
Expand Down
17 changes: 9 additions & 8 deletions doomsday/engine/portable/include/gl_tex.h
Expand Up @@ -124,12 +124,12 @@ void FindAverageColor(const uint8_t* pixels, int width, int height,
* @param pixels Index-color image to evaluate.
* @param width Logical width of the image in pixels.
* @param height Logical height of the image in pixels.
* @param paletteIdx Index of the color palette to use else @c 0
* @param palette Color palette to use.
* @param hasAlpha @c true == @a pixels includes alpha data.
* @param color Determined average color written here.
*/
void FindAverageColorIdx(const uint8_t* pixels, int width, int height,
int paletteIdx, boolean hasAlpha, float color[3]);
const struct colorpalette_s* palette, boolean hasAlpha, float color[3]);

/**
* @param pixels RGB(a) image to evaluate.
Expand All @@ -144,12 +144,12 @@ void FindAverageLineColor(const uint8_t* pixels, int width, int height,
* @param pixels Index-color image to evaluate.
* @param width Logical width of the image in pixels.
* @param height Logical height of the image in pixels.
* @param paletteIdx Index of the color palette to use else @c 0
* @param palette Color palette to use.
* @param hasAlpha @c true == @a pixels includes alpha data.
* @param color Determined average color written here.
*/
void FindAverageLineColorIdx(const uint8_t* pixels, int width, int height,
int line, int paletteIdx, boolean hasAlpha, float color[3]);
int line, const struct colorpalette_s* palette, boolean hasAlpha, float color[3]);

/**
* Calculates a clip region for the image that excludes alpha pixels.
Expand Down Expand Up @@ -202,17 +202,18 @@ void GL_DownMipmap32(uint8_t* pixels, int width, int height, int pixelSize);
void GL_DownMipmap8(uint8_t* in, uint8_t* fadedOut, int width, int height,
float fade);

boolean GL_PalettizeImage(uint8_t* out, int outformat, int paletteIdx,
boolean GL_PalettizeImage(uint8_t* out, int outformat, const struct colorpalette_s* palette,
boolean gammaCorrect, const uint8_t* in, int informat, int width, int height);

boolean GL_QuantizeImageToPalette(uint8_t* out, int outformat, int paletteIdx,
const uint8_t* in, int informat, int width, int height);
boolean GL_QuantizeImageToPalette(uint8_t* out, int outformat,
struct colorpalette_s* palette, const uint8_t* in, int informat, int width, int height);

/**
* Desaturates the texture in the dest buffer by averaging the colour then
* looking up the nearest match in the palette. Increases the brightness
* to maximum.
*/
void GL_DeSaturatePalettedImage(uint8_t* buffer, int paletteIdx, int width, int height);
void GL_DeSaturatePalettedImage(uint8_t* buffer, struct colorpalette_s* palette,
int width, int height);

#endif /* LIBDENG_IMAGE_MANIPULATION_H */
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/colorpalette.c
Expand Up @@ -76,7 +76,7 @@ void ColorPalette_ReplaceColorTable(colorpalette_t* pal, const int compOrder[3],
prepareColorTable(pal, compOrder, compBits, colorData, colorCount);
}

void ColorPalette_Color(colorpalette_t* pal, int colorIdx, uint8_t rgb[3])
void ColorPalette_Color(const colorpalette_t* pal, int colorIdx, uint8_t rgb[3])
{
assert(pal && rgb);
{
Expand Down
6 changes: 2 additions & 4 deletions doomsday/engine/portable/src/gl_main.c
Expand Up @@ -1154,14 +1154,14 @@ uint8_t* GL_ConvertBuffer(const uint8_t* in, int width, int height, int informat
// Conversion from pal8(a) to RGB(A).
if(informat <= 2 && outformat >= 3)
{
GL_PalettizeImage(out, outformat, paletteIdx, false, in, informat, width, height);
GL_PalettizeImage(out, outformat, R_ToColorPalette(paletteIdx), false, in, informat, width, height);
return out;
}

// Conversion from RGB(A) to pal8(a), using pal18To8.
if(informat >= 3 && outformat <= 2)
{
GL_QuantizeImageToPalette(out, outformat, paletteIdx, in, informat, width, height);
GL_QuantizeImageToPalette(out, outformat, R_ToColorPalette(paletteIdx), in, informat, width, height);
return out;
}

Expand Down Expand Up @@ -1202,8 +1202,6 @@ void GL_CalcLuminance(const uint8_t* buffer, int width, int height, int pixelSiz
if(pixelSize == 1)
{
pal = R_ToColorPalette(paletteIdx);
if(NULL == pal)
Con_Error("GL_CalcLuminance: Failed to locate ColorPalette for index %i.", paletteIdx);
}

for(i = 0; i < 3; ++i)
Expand Down
54 changes: 16 additions & 38 deletions doomsday/engine/portable/src/gl_tex.c
Expand Up @@ -752,10 +752,10 @@ void GL_DownMipmap8(uint8_t* in, uint8_t* fadedOut, int width, int height, float
}
}

boolean GL_PalettizeImage(uint8_t* out, int outformat, int paletteIdx,
boolean GL_PalettizeImage(uint8_t* out, int outformat, const colorpalette_t* palette,
boolean applyTexGamma, const uint8_t* in, int informat, int width, int height)
{
assert(in && out);
assert(in && out && palette);

if(0 >= width || 0 >= height)
return false;
Expand All @@ -765,15 +765,11 @@ boolean GL_PalettizeImage(uint8_t* out, int outformat, int paletteIdx,
long numPels = width * height;
int inSize = (informat == 2 ? 1 : informat);
int outSize = (outformat == 2 ? 1 : outformat);
colorpalette_t* pal = R_ToColorPalette(paletteIdx);

if(NULL == pal)
Con_Error("GL_PalettizeImage: Failed to locate ColorPalette for index %i.", paletteIdx);

{ long i;
for(i = 0; i < numPels; ++i)
{
ColorPalette_Color(pal, *in, out);
ColorPalette_Color(palette, *in, out);
if(applyTexGamma)
{
out[CR] = gammaTable[out[CR]];
Expand All @@ -797,24 +793,20 @@ boolean GL_PalettizeImage(uint8_t* out, int outformat, int paletteIdx,
return false;
}

boolean GL_QuantizeImageToPalette(uint8_t* out, int outformat, int paletteIdx,
boolean GL_QuantizeImageToPalette(uint8_t* out, int outformat, colorpalette_t* palette,
const uint8_t* in, int informat, int width, int height)
{
assert(out && in);
assert(out && in && palette);
if(informat >= 3 && outformat <= 2 && width > 0 && height > 0)
{
int inSize = (informat == 2 ? 1 : informat);
int outSize = (outformat == 2 ? 1 : outformat);
int i, numPixels = width * height;
colorpalette_t* pal = R_ToColorPalette(paletteIdx);

if(NULL == pal)
Con_Error("GL_QuantizeImageToPalette: Failed to locate ColorPalette for index %i.", paletteIdx);

for(i = 0; i < numPixels; ++i, in += inSize, out += outSize)
{
// Convert the color value.
*out = ColorPalette_NearestIndexv(pal, in);
*out = ColorPalette_NearestIndexv(palette, in);

// Alpha channel?
if(outformat == 2)
Expand All @@ -830,28 +822,24 @@ boolean GL_QuantizeImageToPalette(uint8_t* out, int outformat, int paletteIdx,
return false;
}

void GL_DeSaturatePalettedImage(uint8_t* buffer, int paletteIdx, int width, int height)
void GL_DeSaturatePalettedImage(uint8_t* buffer, colorpalette_t* palette,
int width, int height)
{
assert(buffer);
assert(buffer && palette);
{
const long numPels = width * height;
colorpalette_t* pal;
uint8_t rgb[3];
int max, temp;

if(width == 0 || height == 0)
return; // Nothing to do.

pal = R_ToColorPalette(paletteIdx);
if(NULL == pal)
Con_Error("GL_DeSaturatePalettedImage: Failed to locate ColorPalette for index %i.", paletteIdx);

// What is the maximum color value?
max = 0;
{ long i;
for(i = 0; i < numPels; ++i)
{
ColorPalette_Color(pal, buffer[i], rgb);
ColorPalette_Color(palette, buffer[i], rgb);
if(rgb[CR] == rgb[CG] && rgb[CR] == rgb[CB])
{
if(rgb[CR] > max)
Expand All @@ -867,28 +855,27 @@ void GL_DeSaturatePalettedImage(uint8_t* buffer, int paletteIdx, int width, int
{ long i;
for(i = 0; i < numPels; ++i)
{
ColorPalette_Color(pal, buffer[i], rgb);
ColorPalette_Color(palette, buffer[i], rgb);
if(rgb[CR] == rgb[CG] && rgb[CR] == rgb[CB])
continue;

// Calculate a weighted average.
temp = (2 * (int)rgb[CR] + 4 * (int)rgb[CG] + 3 * (int)rgb[CB]) / 9;
if(max)
temp *= 255.f / max;
buffer[i] = ColorPalette_NearestIndex(pal, temp, temp, temp);
buffer[i] = ColorPalette_NearestIndex(palette, temp, temp, temp);
}}
}
}

void FindAverageLineColorIdx(const uint8_t* data, int w, int h, int line,
int paletteIdx, boolean hasAlpha, float col[3])
const colorpalette_t* palette, boolean hasAlpha, float col[3])
{
assert(data && col);
{
long count, numpels, avg[3] = { 0, 0, 0 };
const uint8_t* start, *alphaStart;
DGLubyte rgbUBV[3];
colorpalette_t* pal;

if(w <= 0 || h <= 0)
{
Expand All @@ -906,10 +893,6 @@ void FindAverageLineColorIdx(const uint8_t* data, int w, int h, int line,
return;
}

pal = R_ToColorPalette(paletteIdx);
if(NULL == pal)
Con_Error("FindAverageLineColorIdx: Failed to locate ColorPalette for index %i.", paletteIdx);

numpels = w * h;
start = data + w * line;
alphaStart = data + numpels + w * line;
Expand All @@ -919,7 +902,7 @@ void FindAverageLineColorIdx(const uint8_t* data, int w, int h, int line,
{
if(!hasAlpha || alphaStart[i])
{
ColorPalette_Color(pal, start[i], rgbUBV);
ColorPalette_Color(palette, start[i], rgbUBV);
avg[CR] += rgbUBV[CR];
avg[CG] += rgbUBV[CG];
avg[CB] += rgbUBV[CB];
Expand Down Expand Up @@ -1015,14 +998,13 @@ void FindAverageColor(const uint8_t* pixels, int width, int height,
}
}

void FindAverageColorIdx(const uint8_t* data, int w, int h, int paletteIdx,
void FindAverageColorIdx(const uint8_t* data, int w, int h, const colorpalette_t* palette,
boolean hasAlpha, float col[3])
{
assert(data && col);
{
long numpels, count, avg[3] = { 0, 0, 0 };
const uint8_t* alphaStart;
colorpalette_t* pal;
DGLubyte rgb[3];

if(w <= 0 || h <= 0)
Expand All @@ -1031,10 +1013,6 @@ void FindAverageColorIdx(const uint8_t* data, int w, int h, int paletteIdx,
return;
}

pal = R_ToColorPalette(paletteIdx);
if(NULL == pal)
Con_Error("FindAverageColorIdx: Failed to locate ColorPalette for index %i.", paletteIdx);

numpels = w * h;
alphaStart = data + numpels;
count = 0;
Expand All @@ -1043,7 +1021,7 @@ void FindAverageColorIdx(const uint8_t* data, int w, int h, int paletteIdx,
{
if(!hasAlpha || alphaStart[i])
{
ColorPalette_Color(pal, data[i], rgb);
ColorPalette_Color(palette, data[i], rgb);
avg[CR] += rgb[CR];
avg[CG] += rgb[CG];
avg[CB] += rgb[CB];
Expand Down
6 changes: 3 additions & 3 deletions doomsday/engine/portable/src/gl_texmanager.c
Expand Up @@ -753,7 +753,7 @@ static void prepareVariant(texturevariant_t* tex, image_t* image)
else if(0 != image->palette)
{
if(monochrome && !scaleSharp)
GL_DeSaturatePalettedImage(image->pixels, image->palette, image->width, image->height);
GL_DeSaturatePalettedImage(image->pixels, R_ToColorPalette(image->palette), image->width, image->height);

if(scaleSharp)
{
Expand Down Expand Up @@ -963,7 +963,7 @@ static void prepareVariant(texturevariant_t* tex, image_t* image)
}
else
{
FindAverageLineColorIdx(image->pixels, image->width, image->height, 0, image->palette, false, avgTopColor->color);
FindAverageLineColorIdx(image->pixels, image->width, image->height, 0, R_ToColorPalette(image->palette), false, avgTopColor->color);
}
}

Expand Down Expand Up @@ -1000,7 +1000,7 @@ static void prepareVariant(texturevariant_t* tex, image_t* image)
}
else
{
FindAverageColorIdx(image->pixels, image->width, image->height, image->palette, false, al->color);
FindAverageColorIdx(image->pixels, image->width, image->height, R_ToColorPalette(image->palette), false, al->color);
}
memcpy(al->colorAmplified, al->color, sizeof(al->colorAmplified));
amplify(al->colorAmplified);
Expand Down
16 changes: 10 additions & 6 deletions doomsday/engine/portable/src/r_data.c
Expand Up @@ -272,6 +272,8 @@ colorpalette_t* R_ToColorPalette(int paletteIdx)
assert(initedColorPalettes);
if(paletteIdx > 0 && numColorPalettes >= paletteIdx)
return colorPalettes[paletteIdx-1];
Con_Error("R_ToColorPalette: Failed to locate palette for index #%i", paletteIdx);
// Unreachable.
return NULL;
}

Expand Down Expand Up @@ -457,9 +459,10 @@ void R_GetColorPaletteRGBubv(colorpaletteid_t id, int colorIdx, uint8_t rgb[3],
return;
}

{ colorpalette_t* pal;
if(NULL != (pal = R_ToColorPalette(R_FindColorPaletteIndexForId(id))))
{ int paletteIdx = R_FindColorPaletteIndexForId(id);
if(0 != paletteIdx)
{
colorpalette_t* pal = R_ToColorPalette(paletteIdx);
ColorPalette_Color(pal, colorIdx, rgb);
if(applyTexGamma)
{
Expand All @@ -470,7 +473,7 @@ void R_GetColorPaletteRGBubv(colorpaletteid_t id, int colorIdx, uint8_t rgb[3],
return;
}}

Con_Error("R_GetColorPaletteRGBubv: Failed to locate ColorPalette for id %u.", (uint) id);
Con_Message("Warning:R_GetColorPaletteRGBubv: Failed to locate ColorPalette for id %i.\n", id);
}

void R_GetColorPaletteRGBf(colorpaletteid_t id, int colorIdx, float rgb[3],
Expand All @@ -487,9 +490,10 @@ void R_GetColorPaletteRGBf(colorpaletteid_t id, int colorIdx, float rgb[3],
return;
}

{ colorpalette_t* pal;
if(NULL != (pal = R_ToColorPalette(R_FindColorPaletteIndexForId(id))))
{ int paletteIdx = R_FindColorPaletteIndexForId(id);
if(0 != paletteIdx)
{
colorpalette_t* pal = R_ToColorPalette(paletteIdx);
uint8_t ubv[3];
ColorPalette_Color(pal, colorIdx, ubv);
if(applyTexGamma)
Expand All @@ -504,7 +508,7 @@ void R_GetColorPaletteRGBf(colorpaletteid_t id, int colorIdx, float rgb[3],
return;
}}

Con_Error("R_GetColorPaletteRGBf: Failed to locate ColorPalette for id %u.", (uint) id);
Con_Message("Warning:R_GetColorPaletteRGBf: Failed to locate ColorPalette for id %i.\n", id);
}

void R_InfoRendVerticesPool(void)
Expand Down

0 comments on commit 8aeb8d2

Please sign in to comment.