Skip to content

Commit

Permalink
Refactor|ColorPalette|Resources: Translated ColorPalette to C++
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Nov 16, 2013
1 parent dffd372 commit ed5af01
Show file tree
Hide file tree
Showing 10 changed files with 396 additions and 444 deletions.
12 changes: 6 additions & 6 deletions doomsday/client/include/gl/gl_main.h
Expand Up @@ -19,8 +19,8 @@
* 02110-1301 USA</small>
*/

#ifndef LIBDENG_GL_MAIN_H
#define LIBDENG_GL_MAIN_H
#ifndef DENG_GL_MAIN_H
#define DENG_GL_MAIN_H

#ifndef __CLIENT__
# error "gl only exists in the Client"
Expand All @@ -37,11 +37,11 @@
#include "render/r_main.h"
#include "Texture"

struct colorpalette_s;
struct ColorRawf_s;
struct material_s;
struct texturevariant_s;

class ColorPalette;
class Material;

DENG_EXTERN_C int numTexUnits;
Expand Down Expand Up @@ -262,7 +262,7 @@ int GL_ChooseSmartFilter(int width, int height, int flags);
* 4 = RGBA
*/
uint8_t *GL_ConvertBuffer(uint8_t const *src, int width, int height,
int informat, struct colorpalette_s *palette, int outformat);
int informat, ColorPalette const *palette, int outformat);

/**
* @param method Unique identifier of the smart filtering method to apply.
Expand All @@ -285,10 +285,10 @@ uint8_t *GL_SmartFilter(int method, uint8_t const *src, int width, int height,
* Handles pixel sizes; 1 (==2), 3 and 4.
*/
void GL_CalcLuminance(uint8_t const *buffer, int width, int height, int comps,
struct colorpalette_s *palette, float *brightX, float *brightY,
ColorPalette const *palette, float *brightX, float *brightY,
struct ColorRawf_s *color, float *lumSize);

// Console commands.
D_CMD(UpdateGammaRamp);

#endif /* LIBDENG_GL_MAIN_H */
#endif // DENG_GL_MAIN_H
44 changes: 20 additions & 24 deletions doomsday/client/include/gl/gl_tex.h
@@ -1,9 +1,11 @@
/**
* @file gl_tex.h
* Image manipulation and evaluation algorithms. @ingroup gl
/** @file gl_tex.h Image manipulation and evaluation algorithms.
*
* @ingroup gl
*
* @authors Copyright &copy; 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright &copy; 2006-2013 Daniel Swanson <danij@dengine.net>
* @todo Belongs in the resource domain -- no ties to GL or related components.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
Expand All @@ -20,11 +22,13 @@
* 02110-1301 USA</small>
*/

#ifndef LIBDENG_IMAGE_MANIPULATION_H
#define LIBDENG_IMAGE_MANIPULATION_H
#ifndef DENG_GL_IMAGE_MANIPULATION_H
#define DENG_GL_IMAGE_MANIPULATION_H

#include "color.h"

class ColorPalette;

typedef struct colorpalette_analysis_s {
colorpaletteid_t paletteId;
} colorpalette_analysis_t;
Expand All @@ -43,10 +47,6 @@ typedef struct averagealpha_analysis_s {
float coverage; ///< Fraction representing the ratio of alpha to non-alpha pixels.
} averagealpha_analysis_t;

#ifdef __cplusplus
extern "C" {
#endif

/**
* @param pixels Luminance image to be enhanced.
* @param width Width of the image in pixels.
Expand Down Expand Up @@ -155,7 +155,7 @@ void FindAverageColor(const uint8_t* pixels, int width, int height,
* @param color Determined average color written here.
*/
void FindAverageColorIdx(const uint8_t* pixels, int width, int height,
const struct colorpalette_s* palette, boolean hasAlpha, ColorRawf* color);
ColorPalette const *palette, boolean hasAlpha, ColorRawf* color);

/**
* @param pixels RGB(a) image to evaluate.
Expand All @@ -178,7 +178,7 @@ void FindAverageLineColor(const uint8_t* pixels, int width, int height,
* @param color Determined average color written here.
*/
void FindAverageLineColorIdx(const uint8_t* pixels, int width, int height,
int line, const struct colorpalette_s* palette, boolean hasAlpha, ColorRawf* color);
int line, ColorPalette const *palette, boolean hasAlpha, ColorRawf* color);

/**
* @param pixels RGB(a) image to evaluate.
Expand All @@ -200,7 +200,7 @@ void FindAverageAlpha(const uint8_t* pixels, int width, int height, int pixelSiz
* @param coverage Fraction representing the ratio of alpha to non-alpha pixels.
*/
void FindAverageAlphaIdx(const uint8_t* pixels, int width, int height,
const struct colorpalette_s* palette, float* alpha, float* coverage);
ColorPalette const *palette, float* alpha, float* coverage);

/**
* Calculates a clip region for the image that excludes alpha pixels.
Expand Down Expand Up @@ -259,22 +259,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, const struct colorpalette_s* palette,
boolean gammaCorrect, const uint8_t* in, int informat, int width, int height);
boolean GL_PalettizeImage(uint8_t *out, int outformat, ColorPalette const *palette,
boolean gammaCorrect, uint8_t const *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);
boolean GL_QuantizeImageToPalette(uint8_t *out, int outformat,
ColorPalette const *palette, uint8_t const *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, struct colorpalette_s* palette,
void GL_DeSaturatePalettedImage(uint8_t *buffer, ColorPalette const *palette,
int width, int height);

#ifdef __cplusplus
} // extern "C"
#endif

#endif // LIBDENG_IMAGE_MANIPULATION_H
#endif // DENG_GL_IMAGE_MANIPULATION_H
176 changes: 84 additions & 92 deletions doomsday/client/include/resource/colorpalette.h
@@ -1,7 +1,6 @@
/**
* @file colorpalette.h Color Palette.
/** @file colorpalette.h Color palette resource.
*
* @author Copyright &copy; 2009-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2009-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
Expand All @@ -18,99 +17,92 @@
* 02110-1301 USA</small>
*/

#ifndef LIBDENG_RESOURCE_COLORPALETTE_H
#define LIBDENG_RESOURCE_COLORPALETTE_H

#ifdef __cplusplus
extern "C" {
#endif

/**
* @defgroup colorPaletteFlags Color Palette Flags
* @ingroup flags
*/
///@{
#define CPF_UPDATE_18TO8 0x1 /// The 18To8 LUT needs updating.
///@}
#ifndef DENG_RESOURCE_COLORPALETTE_H
#define DENG_RESOURCE_COLORPALETTE_H

#define COLORPALETTE_MAX_COMPONENT_BITS (16) // Max bits per component.
#include "dd_types.h"
#include <de/Vector>

/**
* Color Palette.
*
* @ingroup resource
*/
typedef struct colorpalette_s {
/// @ref colorPaletteFlags
uint8_t _flags;

/// R8G8B88 color triplets [_num * 3].
uint8_t *_colorData;
int _colorCount;

/// Nearest color lookup table.
int *_18To8LUT;
} colorpalette_t;

colorpalette_t* ColorPalette_New(void);

/**
* Constructs a new color palette.
*
* @param compOrder Component order. Examples:
* <pre> [0,1,2] == RGB
* [2,1,0] == BGR</pre>
* @param compBits Number of bits per component [R,G,B].
* @param colorData Color triplets (at least @a numColors * 3).
* @param colorCount Number of color triplets.
*/
colorpalette_t *ColorPalette_NewWithColorTable(int const compOrder[3],
uint8_t const compBits[3], uint8_t const *colorData, int colorCount);

void ColorPalette_Delete(colorpalette_t *pal);

/// @return Number of colors in the palette.
ushort ColorPalette_Size(colorpalette_t *pal);

/**
* Replace the entire color table.
*
* @param pal Color palette.
* @param compOrder Component order. Examples:
* <pre> [0,1,2] == RGB
* [2,1,0] == BGR</pre>
* @param compBits Number of bits per component [R,G,B].
* @param colorData Color triplets (at least @a numColors * 3).
* @param colorCount Number of color triplets.
*/
void ColorPalette_ReplaceColorTable(colorpalette_t *pal, int const compOrder[3],
uint8_t const compBits[3], uint8_t const *colorData, int colorCount);

/**
* Lookup a color in the palette.
*
* @note If the specified color index is out of range it will be clamped to
* a valid value before use.
*
* @param pal Color palette.
* @param colorIdx Index of the color to lookup.
* @param rgb Associated R8G8B8 color triplet is written here.
*/
void ColorPalette_Color(colorpalette_t const *pal, int colorIdx, uint8_t rgb[3]);

/**
* Given an R8G8B8 color triplet return the closet matching color index.
*
* @param pal Color palette.
* @param rgb R8G8B8 color to be matched.
*
* @return Closet matching color index or @c -1 if no colors in the palette.
*/
int ColorPalette_NearestIndexv(colorpalette_t *pal, uint8_t const rgb[3]);
int ColorPalette_NearestIndex(colorpalette_t *pal, uint8_t red, uint8_t green, uint8_t blue);

#ifdef __cplusplus
} // extern "C"
#endif

#endif /* LIBDENG_RESOURCE_COLORPALETTE_H */
class ColorPalette
{
public:
/// Maximum number of bits per color component.
static int const max_component_bits = 16;

public:
/**
* Construct a new empty color palette.
*/
ColorPalette();

/**
* Constructs a new color palette using the specified color table.
*
* @param compOrder Component order. Examples:
* <pre> [0,1,2] == RGB
* [2,1,0] == BGR
* </pre>
*
* @param compBits Number of bits per component [R,G,B].
* @param colorData Color triplets (at least @a numColors * 3).
* @param colorCount Number of color triplets.
*/
ColorPalette(int const compOrder[3], uint8_t const compBits[3],
uint8_t const *colorData, int colorCount);

/**
* Returns the total number of colors in the palette.
*/
int size() const;

/**
* Replace the entire color table.
*
* @param compOrder Component order. Examples:
* <pre> [0,1,2] == RGB
* [2,1,0] == BGR
* </pre>
*
* @param compBits Number of bits per component [R,G,B].
* @param colorData Color triplets (at least @a numColors * 3).
* @param colorCount Number of color triplets.
*/
void replaceColorTable(int const compOrder[3], uint8_t const compBits[3],
uint8_t const *colorData, int colorCount);

/**
* Lookup a color in the palette by @a colorIndex. If the specified index is
* out of valid [0..colorCount) range it will be clamped.
*
* @param colorIndex Index of the color in the palette.
*
* @return Associated R8G8B8 color triplet.
*
* @see colorf()
*/
de::Vector3ub color(int colorIndex) const;

/**
* Same as @ref color() except the color is returned in [0..1] floating-point.
*/
de::Vector3f colorf(int colorIndex) const;

/**
* Given an R8G8B8 color triplet return the closet matching color index.
*
* @param rgb R8G8B8 color to be matched.
*
* @return Closet matching color index or @c -1 if no colors in the palette.
*/
int nearestIndex(de::Vector3ub const &rgb) const;

private:
DENG2_PRIVATE(d)
};

#endif // DENG_RESOURCE_COLORPALETTE_H
4 changes: 2 additions & 2 deletions doomsday/client/include/resource/colorpalettes.h
Expand Up @@ -51,13 +51,13 @@ void R_DestroyColorPalettes(void);
int R_ColorPaletteCount(void);

/// @return ColorPalette associated with unique @a id, else @c NULL.
colorpalette_t *R_ToColorPalette(colorpaletteid_t id);
ColorPalette *R_ToColorPalette(colorpaletteid_t id);

/**
* Given a color palette list index return the ColorPalette.
* @return ColorPalette if found else @c NULL
*/
colorpalette_t *R_GetColorPaletteByIndex(int paletteIdx);
ColorPalette *R_GetColorPaletteByIndex(int paletteIdx);

/**
* Change the default color palette.
Expand Down
18 changes: 10 additions & 8 deletions doomsday/client/src/gl/gl_main.cpp
Expand Up @@ -989,12 +989,12 @@ uint8_t* GL_SmartFilter(int method, const uint8_t* src, int width, int height,
return out;
}

uint8_t* GL_ConvertBuffer(const uint8_t* in, int width, int height, int informat,
colorpalette_t* palette, int outformat)
uint8_t *GL_ConvertBuffer(uint8_t const *in, int width, int height, int informat,
ColorPalette const *palette, int outformat)
{
assert(in);
{
uint8_t* out;
DENG2_ASSERT(in != 0);

uint8_t *out;

if(width <= 0 || height <= 0)
{
Expand Down Expand Up @@ -1048,11 +1048,10 @@ uint8_t* GL_ConvertBuffer(const uint8_t* in, int width, int height, int informat
}
}
return out;
}
}

void GL_CalcLuminance(uint8_t const *buffer, int width, int height, int pixelSize,
colorpalette_t *palette, float *retBrightX, float *retBrightY,
ColorPalette const *palette, float *retBrightX, float *retBrightY,
ColorRawf *retColor, float *retLumSize)
{
DENG_ASSERT(buffer && retBrightX && retBrightY && retColor && retLumSize);
Expand Down Expand Up @@ -1133,7 +1132,10 @@ void GL_CalcLuminance(uint8_t const *buffer, int width, int height, int pixelSiz

if(pixelSize == 1)
{
ColorPalette_Color(palette, *src, rgb);
Vector3ub palColor = palette->color(*src);
rgb[CR] = palColor.x;
rgb[CG] = palColor.y;
rgb[CB] = palColor.z;
}
else if(pixelSize >= 3)
{
Expand Down

0 comments on commit ed5af01

Please sign in to comment.