Skip to content

Commit

Permalink
Refactor|Resources: ResourceSystem has ownership of color palettes
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Nov 17, 2013
1 parent fe74d70 commit d8cfa8d
Show file tree
Hide file tree
Showing 16 changed files with 505 additions and 466 deletions.
4 changes: 2 additions & 2 deletions doomsday/client/include/gl/gl_main.h
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, ColorPalette const *palette, int outformat);
int informat, colorpaletteid_t paletteId, int outformat);

/**
* @param method Unique identifier of the smart filtering method to apply.
Expand All @@ -285,7 +285,7 @@ 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,
ColorPalette const *palette, float *brightX, float *brightY,
colorpaletteid_t paletteId, float *brightX, float *brightY,
struct ColorRawf_s *color, float *lumSize);

// Console commands.
Expand Down
23 changes: 11 additions & 12 deletions doomsday/client/include/gl/gl_tex.h
Expand Up @@ -154,8 +154,8 @@ void FindAverageColor(const uint8_t* pixels, int width, int height,
* @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,
ColorPalette const *palette, boolean hasAlpha, ColorRawf* color);
void FindAverageColorIdx(uint8_t const *pixels, int width, int height,
ColorPalette const &palette, boolean hasAlpha, ColorRawf *color);

/**
* @param pixels RGB(a) image to evaluate.
Expand All @@ -165,8 +165,8 @@ void FindAverageColorIdx(const uint8_t* pixels, int width, int height,
* @param line Line to evaluate.
* @param color Determined average color written here.
*/
void FindAverageLineColor(const uint8_t* pixels, int width, int height,
int pixelSize, int line, ColorRawf* color);
void FindAverageLineColor(uint8_t const *pixels, int width, int height,
int pixelSize, int line, ColorRawf *color);

/**
* @param pixels Index-color image to evaluate.
Expand All @@ -177,8 +177,8 @@ void FindAverageLineColor(const uint8_t* pixels, int width, int height,
* @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, ColorPalette const *palette, boolean hasAlpha, ColorRawf* color);
void FindAverageLineColorIdx(uint8_t const *pixels, int width, int height,
int line, ColorPalette const &palette, boolean hasAlpha, ColorRawf *color);

/**
* @param pixels RGB(a) image to evaluate.
Expand All @@ -188,19 +188,18 @@ void FindAverageLineColorIdx(const uint8_t* pixels, int width, int height,
* @param alpha Determined average alpha written here.
* @param coverage Fraction representing the ratio of alpha to non-alpha pixels.
*/
void FindAverageAlpha(const uint8_t* pixels, int width, int height, int pixelSize,
float* alpha, float* coverage);
void FindAverageAlpha(uint8_t const *pixels, int width, int height, int pixelSize,
float *alpha, float *coverage);

/**
* @param pixels Index-color image to evaluate.
* @param width Width of the image in pixels.
* @param height Height of the image in pixels.
* @param palette Color palette to use.
* @param alpha Determined average alpha written here.
* @param coverage Fraction representing the ratio of alpha to non-alpha pixels.
*/
void FindAverageAlphaIdx(const uint8_t* pixels, int width, int height,
ColorPalette const *palette, float* alpha, float* coverage);
void FindAverageAlphaIdx(uint8_t const *pixels, int width, int height, float *alpha,
float *coverage);

/**
* Calculates a clip region for the image that excludes alpha pixels.
Expand Down Expand Up @@ -270,7 +269,7 @@ boolean GL_QuantizeImageToPalette(uint8_t *out, int outformat,
* looking up the nearest match in the palette. Increases the brightness
* to maximum.
*/
void GL_DeSaturatePalettedImage(uint8_t *buffer, ColorPalette const *palette,
void GL_DeSaturatePalettedImage(uint8_t *buffer, ColorPalette const &palette,
int width, int height);

#endif // DENG_GL_IMAGE_MANIPULATION_H
5 changes: 2 additions & 3 deletions doomsday/client/include/gl/gl_texmanager.h
Expand Up @@ -44,6 +44,8 @@
#include "TextureVariantSpec"
#include "uri.hh"

class ColorPalette;

#define TEXQ_BEST 8
#define MINTEXWIDTH 8
#define MINTEXHEIGHT 8
Expand Down Expand Up @@ -151,9 +153,6 @@ void GL_ReleaseVariantTexture(de::TextureVariant &texture);
*/
void GL_ReleaseVariantTexturesBySpec(de::Texture &texture, texturevariantspecification_t &spec);

/// Release all textures associated with the identified colorpalette @a paletteId.
void GL_ReleaseTexturesByColorPalette(colorpaletteid_t paletteId);

/// Release all textures used with 'Raw Images'.
void GL_ReleaseTexturesForRawImages();

Expand Down
14 changes: 12 additions & 2 deletions doomsday/client/include/resource/colorpalette.h
Expand Up @@ -22,6 +22,8 @@

#include "dd_types.h"
#include <de/Error>
#include <de/Id>
#include <de/Observers>
#include <de/Vector>

/**
Expand All @@ -35,6 +37,9 @@ class ColorPalette
/// Base class for color-format-related errors. @ingroup errors
DENG2_ERROR(ColorFormatError);

/// Notified whenever the color table changes.
DENG2_DEFINE_AUDIENCE(ColorTableChange, void colorPaletteColorTableChanged(ColorPalette &colorPalette))

/// Maximum number of bits per color component.
static int const max_component_bits = 16;

Expand Down Expand Up @@ -66,10 +71,15 @@ class ColorPalette
return color(colorIndex);
}

/**
* Returns the automatically generated, unique identifier of the color palette.
*/
de::Id id() const;

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

/**
* Replace the entire color table.
Expand All @@ -83,7 +93,7 @@ class ColorPalette
* @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],
ColorPalette &loadColorTable(int const compOrder[3], uint8_t const compBits[3],
uint8_t const *colorData, int colorCount);

/**
Expand Down
34 changes: 1 addition & 33 deletions doomsday/client/include/resource/colorpalettes.h
Expand Up @@ -20,50 +20,18 @@
#ifndef DENG_RESOURCE_COLORPALETTES_H
#define DENG_RESOURCE_COLORPALETTES_H

#include "dd_share.h" // For colorpaletteid_t
#include "resource/colorpalette.h"
#include <de/libdeng1.h>

// Maximum number of palette translation tables.
#define NUM_TRANSLATION_CLASSES 3
#define NUM_TRANSLATION_MAPS_PER_CLASS 7
#define NUM_TRANSLATION_TABLES (NUM_TRANSLATION_CLASSES * NUM_TRANSLATION_MAPS_PER_CLASS)

DENG_EXTERN_C colorpaletteid_t defaultColorPalette;
DENG_EXTERN_C byte *translationTables;

void R_InitTranslationTables();
void R_UpdateTranslationTables();

byte const *R_TranslationTable(int tclass, int tmap);

/// Prepare for color palette creation.
void R_InitColorPalettes();

/// To be called when any existing color palettes are no longer required.
void R_DestroyColorPalettes();

/// @return Number of available color palettes.
int R_ColorPaletteCount();

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

DENG_EXTERN_C colorpaletteid_t R_GetColorPaletteNumForName(char const *name);
DENG_EXTERN_C char const *R_GetColorPaletteNameForNum(colorpaletteid_t id);

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

/**
* Change the default color palette.
*
* @param id Id of the color palette to make default.
*
* @return @c true iff successful, else @c NULL.
*/
boolean R_SetDefaultColorPalette(colorpaletteid_t id);

#endif // DENG_RESOURCE_COLORPALETTES_H
58 changes: 57 additions & 1 deletion doomsday/client/include/resource/resourcesystem.h
Expand Up @@ -22,6 +22,7 @@
#include "def_data.h"
#include "resourceclass.h"
#include "resource/animgroup.h"
#include "resource/colorpalette.h"
#ifdef __CLIENT__
# include "Fonts"
#endif
Expand Down Expand Up @@ -58,7 +59,10 @@ class ResourceSystem : public de::System
{
public:
/// An unknown resource class identifier was specified. @ingroup errors
DENG2_ERROR(UnknownResourceClass);
DENG2_ERROR(UnknownResourceClassError);

/// The referenced color paletted could not be found. @ingroup errors
DENG2_ERROR(MissingColorPaletteError);

public:
/**
Expand Down Expand Up @@ -156,6 +160,58 @@ class ResourceSystem : public de::System
*/
de::AnimGroup &newAnimGroup(int flags);

/**
* Returns the total number of color palettes.
*/
int colorPaletteCount() const;

/**
* Destroys all the color palettes.
*/
void clearAllColorPalettes();

/**
* Returns the ColorPalette associated with unique @a id.
*/
ColorPalette &colorPalette(colorpaletteid_t id) const;

/**
* Returns the symbolic name of the specified color @a palette. A zero-length
* string is return if no name is associated.
*/
de::String colorPaletteName(ColorPalette &palette) const;

/**
* Returns @c true iff a ColorPalette with the specified @a name is present.
*/
bool hasColorPalette(de::String name) const;

/**
* Returns the ColorPalette associated with @a name.
*
* @see hasColorPalette()
*/
ColorPalette &colorPalette(de::String name) const;

/**
* @param newPalette Color palette to add. Ownership of the palette is given
* to the resource system.
* @param name Symbolic name of the color palette.
*/
void addColorPalette(ColorPalette &newPalette, de::String const &name = de::String());

/**
* Returns the unique identifier of the current default color palette.
*/
colorpaletteid_t defaultColorPalette() const;

/**
* Change the default color palette.
*
* @param newDefaultPalette The color palette to make default.
*/
void setDefaultColorPalette(ColorPalette *newDefaultPalette);

public: /// @todo Should be private:
void initCompositeTextures();
void initFlatTextures();
Expand Down
5 changes: 2 additions & 3 deletions doomsday/client/src/dd_main.cpp
Expand Up @@ -1410,8 +1410,6 @@ bool App_ChangeGame(Game &game, bool allowReload)
B_BindDefaults();
B_InitialContextActivations();
#endif
App_ResourceSystem().clearAllAnimGroups();

// Reset the world back to it's initial state (unload the map, reset players, etc...).
App_World().reset();

Expand All @@ -1420,9 +1418,10 @@ bool App_ChangeGame(Game &game, bool allowReload)
P_ShutdownMapEntityDefs();

R_ShutdownSvgs();
R_DestroyColorPalettes();

App_ResourceSystem().clearAllRuntimeResources();
App_ResourceSystem().clearAllAnimGroups();
App_ResourceSystem().clearAllColorPalettes();

Sfx_InitLogical();

Expand Down
37 changes: 13 additions & 24 deletions doomsday/client/src/gl/gl_main.cpp
Expand Up @@ -388,7 +388,7 @@ void GL_ShutdownRefresh()
{
initFullGLOk = false;

R_DestroyColorPalettes();
App_ResourceSystem().clearAllColorPalettes();

GL_ShutdownTextureManager();
}
Expand Down Expand Up @@ -990,32 +990,25 @@ uint8_t* GL_SmartFilter(int method, const uint8_t* src, int width, int height,
}

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

uint8_t *out;

if(width <= 0 || height <= 0)
if(informat == outformat)
{
Con_Error("GL_ConvertBuffer: Attempt to convert zero-sized image.");
exit(1); // Unreachable.
// No conversion necessary.
return (uint8_t*)in;
}

if(informat <= 2 && palette == NULL)
if(width <= 0 || height <= 0)
{
Con_Error("GL_ConvertBuffer: Cannot process image of pixelsize==1 without palette.");
Con_Error("GL_ConvertBuffer: Attempt to convert zero-sized image.");
exit(1); // Unreachable.
}

if(informat == outformat)
{ // No conversion necessary.
return (uint8_t*)in;
}
ColorPalette *palette = (informat <= 2? &App_ResourceSystem().colorPalette(paletteId) : 0);

if(NULL == (out = (uint8_t*) malloc(outformat * width * height)))
Con_Error("GL_ConvertBuffer: Failed on allocation of %lu bytes for "
"conversion buffer.", (unsigned long) (outformat * width * height));
uint8_t *out = (uint8_t*) M_Malloc(outformat * width * height);

// Conversion from pal8(a) to RGB(A).
if(informat <= 2 && outformat >= 3)
Expand All @@ -1034,8 +1027,8 @@ uint8_t *GL_ConvertBuffer(uint8_t const *in, int width, int height, int informat
if(informat == 3 && outformat == 4)
{
long i, numPels = width * height;
const uint8_t* src = in;
uint8_t* dst = out;
uint8_t const *src = in;
uint8_t *dst = out;
for(i = 0; i < numPels; ++i)
{
dst[CR] = src[CR];
Expand All @@ -1051,7 +1044,7 @@ uint8_t *GL_ConvertBuffer(uint8_t const *in, int width, int height, int informat
}

void GL_CalcLuminance(uint8_t const *buffer, int width, int height, int pixelSize,
ColorPalette const *palette, float *retBrightX, float *retBrightY,
colorpaletteid_t paletteId, float *retBrightX, float *retBrightY,
ColorRawf *retColor, float *retLumSize)
{
DENG_ASSERT(buffer && retBrightX && retBrightY && retColor && retLumSize);
Expand All @@ -1065,11 +1058,7 @@ void GL_CalcLuminance(uint8_t const *buffer, int width, int height, int pixelSiz
long bright[2];
uint8_t rgb[3];

if(pixelSize == 1 && !palette)
{
Con_Error("GL_CalcLuminance: Cannot process image of pixelsize==1 without palette.");
exit(1); // Unreachable.
}
ColorPalette *palette = (pixelSize == 1? &App_ResourceSystem().colorPalette(paletteId) : 0);

// Apply the defaults.
// Default to the center of the texture.
Expand Down

0 comments on commit d8cfa8d

Please sign in to comment.