Permalink
Browse files

Add support for palettes with more less (and more) than 256 colors (fix

#668)

With this commit we've removed doc::Palette::MaxColors constant.
  • Loading branch information...
dacap committed Jun 18, 2015
1 parent 312e3f9 commit 89e0392daa3a1a824d26355c6c9349105485b730
@@ -14,11 +14,7 @@
<separator text="Color Mode:" left="true" horizontal="true" />
<box vertical="true" homogeneous="true" childspacing="0">
<box horizontal="true">
<radio id="indexed_mode" text="&amp;Indexed color" group="1" tooltip="Using a palette of 256 colors&#10;(8 bits per pixel)" />
<!-- <entry id="colors" maxsize="8" tooltip="Maximum number of colors&#10;(only for Indexed images)&#10;&#10;This field cannot be modified in this beta version." disabled="true" /> -->
<!-- <label text="Colors" /> -->
</box>
<radio id="indexed_mode" text="&amp;Indexed color" group="1" tooltip="Using a palette of 256 colors&#10;(8 bits per pixel)" />
<radio id="rgb_mode" text="&amp;RGB color" group="1" tooltip="RGBA color mode&#10;(32 bits per pixel)" />
<radio id="grayscale_mode" text="&amp;Grayscale" group="1" tooltip="Value and Alpha&#10;(16 bits per pixel)" />
</box>
View
@@ -189,7 +189,7 @@ void App::initialize(const AppOptions& options)
// Copy db32.gpl as the default palette file.
base::UniquePtr<Palette> pal(load_palette(rf.filename().c_str()));
if (pal)
save_palette(palFile.c_str(), pal.get());
save_palette(palFile.c_str(), pal.get(), 0);
}
}
}
@@ -26,18 +26,29 @@ SetPalette::SetPalette(Sprite* sprite, frame_t frame, const Palette* newPalette)
{
const Palette* curPalette = sprite->palette(frame);
m_oldNColors = curPalette->size();
m_newNColors = newPalette->size();
// Check differences between current sprite palette and the new one
m_from = m_to = -1;
curPalette->countDiff(newPalette, &m_from, &m_to);
int diffs = curPalette->countDiff(newPalette, &m_from, &m_to);
ASSERT(diffs > 0);
if (m_from >= 0 && m_to >= m_from) {
size_t ncolors = m_to-m_from+1;
m_oldColors.resize(ncolors);
m_newColors.resize(ncolors);
int oldColors = MIN(m_to+1, m_oldNColors)-m_from;
if (oldColors > 0)
m_oldColors.resize(oldColors);
int newColors = MIN(m_to+1, m_newNColors)-m_from;
if (newColors > 0)
m_newColors.resize(newColors);
for (size_t i=0; i<size_t(m_to-m_from+1); ++i) {
if (i < m_oldColors.size())
m_oldColors[i] = curPalette->getEntry(m_from+i);
for (size_t i=0; i<ncolors; ++i) {
m_oldColors[i] = curPalette->getEntry(m_from+i);
m_newColors[i] = newPalette->getEntry(m_from+i);
if (i < m_newColors.size())
m_newColors[i] = newPalette->getEntry(m_from+i);
}
}
}
@@ -46,6 +57,7 @@ void SetPalette::onExecute()
{
Sprite* sprite = this->sprite();
Palette* palette = sprite->palette(m_frame);
palette->resize(m_newNColors);
for (size_t i=0; i<m_newColors.size(); ++i)
palette->setEntry(m_from+i, m_newColors[i]);
@@ -57,6 +69,7 @@ void SetPalette::onUndo()
{
Sprite* sprite = this->sprite();
Palette* palette = sprite->palette(m_frame);
palette->resize(m_oldNColors);
for (size_t i=0; i<m_oldColors.size(); ++i)
palette->setEntry(m_from+i, m_oldColors[i]);
@@ -42,6 +42,8 @@ namespace cmd {
private:
frame_t m_frame;
int m_from, m_to;
int m_oldNColors;
int m_newNColors;
std::vector<color_t> m_oldColors;
std::vector<color_t> m_newColors;
};
View
@@ -65,7 +65,7 @@ Color Color::fromGray(int g)
// static
Color Color::fromIndex(int index)
{
assert(index >= 0 && index < Palette::MaxColors);
assert(index >= 0);
Color color(Color::IndexType);
color.m_value.index = index;
@@ -374,9 +374,10 @@ int Color::getRed() const
case Color::IndexType: {
int i = m_value.index;
ASSERT(i >= 0 && i < get_current_palette()->size());
return rgba_getr(get_current_palette()->getEntry(i));
if (i >= 0 && i < get_current_palette()->size())
return rgba_getr(get_current_palette()->getEntry(i));
else
return 0;
}
}
@@ -405,9 +406,10 @@ int Color::getGreen() const
case Color::IndexType: {
int i = m_value.index;
ASSERT(i >= 0 && i < get_current_palette()->size());
return rgba_getg(get_current_palette()->getEntry(i));
if (i >= 0 && i < get_current_palette()->size())
return rgba_getg(get_current_palette()->getEntry(i));
else
return 0;
}
}
@@ -436,9 +438,10 @@ int Color::getBlue() const
case Color::IndexType: {
int i = m_value.index;
ASSERT(i >= 0 && i < get_current_palette()->size());
return rgba_getb(get_current_palette()->getEntry(i));
if (i >= 0 && i < get_current_palette()->size())
return rgba_getb(get_current_palette()->getEntry(i));
else
return 0;
}
}
@@ -467,13 +470,14 @@ int Color::getHue() const
case Color::IndexType: {
int i = m_value.index;
ASSERT(i >= 0 && i < get_current_palette()->size());
uint32_t c = get_current_palette()->getEntry(i);
return Hsv(Rgb(rgba_getr(c),
rgba_getg(c),
rgba_getb(c))).hueInt();
if (i >= 0 && i < get_current_palette()->size()) {
uint32_t c = get_current_palette()->getEntry(i);
return Hsv(Rgb(rgba_getr(c),
rgba_getg(c),
rgba_getb(c))).hueInt();
}
else
return 0;
}
}
@@ -502,13 +506,14 @@ int Color::getSaturation() const
case Color::IndexType: {
int i = m_value.index;
ASSERT(i >= 0 && i < get_current_palette()->size());
uint32_t c = get_current_palette()->getEntry(i);
return Hsv(Rgb(rgba_getr(c),
rgba_getg(c),
rgba_getb(c))).saturationInt();
if (i >= 0 && i < get_current_palette()->size()) {
uint32_t c = get_current_palette()->getEntry(i);
return Hsv(Rgb(rgba_getr(c),
rgba_getg(c),
rgba_getb(c))).saturationInt();
}
else
return 0;
}
}
@@ -537,13 +542,14 @@ int Color::getValue() const
case Color::IndexType: {
int i = m_value.index;
ASSERT(i >= 0 && i < get_current_palette()->size());
uint32_t c = get_current_palette()->getEntry(i);
return Hsv(Rgb(rgba_getr(c),
rgba_getg(c),
rgba_getb(c))).valueInt();
if (i >= 0 && i < get_current_palette()->size()) {
uint32_t c = get_current_palette()->getEntry(i);
return Hsv(Rgb(rgba_getr(c),
rgba_getg(c),
rgba_getb(c))).valueInt();
}
else
return 0;
}
}
@@ -572,13 +578,14 @@ int Color::getGray() const
case Color::IndexType: {
int i = m_value.index;
ASSERT(i >= 0 && i < get_current_palette()->size());
uint32_t c = get_current_palette()->getEntry(i);
return 255 * Hsv(Rgb(rgba_getr(c),
rgba_getg(c),
rgba_getb(c))).valueInt() / 100;
if (i >= 0 && i < get_current_palette()->size()) {
uint32_t c = get_current_palette()->getEntry(i);
return 255 * Hsv(Rgb(rgba_getr(c),
rgba_getg(c),
rgba_getb(c))).valueInt() / 100;
}
else
return 0;
}
}
@@ -62,7 +62,7 @@ NewFileCommand::NewFileCommand()
void NewFileCommand::onExecute(Context* context)
{
PixelFormat format;
int w, h, bg, ncolors = doc::Palette::MaxColors;
int w, h, bg, ncolors = get_default_palette()->size();
char buf[1024];
app::Color bg_table[] = {
app::Color::fromMask(),
@@ -83,7 +83,6 @@ void NewFileCommand::onExecute(Context* context)
w = get_config_int("NewSprite", "Width", 320);
h = get_config_int("NewSprite", "Height", 240);
bg = get_config_int("NewSprite", "Background", 1); // Default = Black
// ncolors = get_config_int("NewSprite", "Colors", 256);
if (bg == 4) // Convert old default (Background color) to new default (Black)
bg = 1;
@@ -99,7 +98,6 @@ void NewFileCommand::onExecute(Context* context)
window.width()->setTextf("%d", MAX(1, w));
window.height()->setTextf("%d", MAX(1, h));
// colors->setTextf("%d", MID(2, ncolors, 256));
// Select image-type
switch (format) {
@@ -127,12 +125,10 @@ void NewFileCommand::onExecute(Context* context)
w = window.width()->getTextInt();
h = window.height()->getTextInt();
// ncolors = colors->getTextInt();
bg = window.bgBox()->getSelectedIndex();
w = MID(1, w, 65535);
h = MID(1, h, 65535);
ncolors = MID(2, ncolors, 256);
// Select the color
app::Color color = app::Color::fromMask();
@@ -153,8 +149,7 @@ void NewFileCommand::onExecute(Context* context)
ASSERT(format == IMAGE_RGB || format == IMAGE_GRAYSCALE || format == IMAGE_INDEXED);
ASSERT(w > 0 && h > 0);
base::UniquePtr<Sprite> sprite(Sprite::createBasicSprite(format, w, h,
(format == IMAGE_INDEXED ? ncolors: 256)));
base::UniquePtr<Sprite> sprite(Sprite::createBasicSprite(format, w, h, ncolors));
if (sprite->pixelFormat() != IMAGE_GRAYSCALE)
get_default_palette()->copyColorsTo(sprite->palette(frame_t(0)));
@@ -250,7 +250,7 @@ PaletteEntryEditor::PaletteEntryEditor()
, m_redrawAll(false)
, m_implantChange(false)
, m_selfPalChange(false)
, m_fromPalette(0, Palette::MaxColors)
, m_fromPalette(0, 0)
{
m_colorType.addItem("RGB");
m_colorType.addItem("HSB");
@@ -66,7 +66,7 @@ void SavePaletteCommand::onExecute(Context* context)
return;
}
if (!save_palette(filename.c_str(), palette))
if (!save_palette(filename.c_str(), palette, 16)) // TODO 16 should be configurable
Alert::show("Error<<Saving palette file||&Close");
if (m_preset == get_default_palette_preset_name()) {
@@ -74,10 +74,6 @@ Palette* load_palette(const char *filename)
fop->document->sprite()->palette(frame_t(0))) {
pal = new Palette(
*fop->document->sprite()->palette(frame_t(0)));
// TODO remove this line when support for palettes with less
// than 256 colors is added.
pal->resize(Palette::MaxColors);
}
delete fop->document;
@@ -92,7 +88,7 @@ Palette* load_palette(const char *filename)
return pal;
}
bool save_palette(const char *filename, const Palette* pal)
bool save_palette(const char *filename, const Palette* pal, int columns)
{
std::string ext = base::string_to_lower(base::get_file_extension(filename));
bool success = false;
@@ -109,10 +105,13 @@ bool save_palette(const char *filename, const Palette* pal)
else {
FileFormat* ff = FileFormatsManager::instance()->getFileFormatByExtension(ext.c_str());
if (ff && ff->support(FILE_SUPPORT_SAVE)) {
int w = (columns > 0 ? columns: pal->size());
int h = (pal->size() / w) + (pal->size() % w > 0 ? 1: 0);
app::Context tmpContext;
doc::Document* doc = tmpContext.documents().add(
16, 16, doc::ColorMode::INDEXED,
Palette::MaxColors);
w, h, (pal->size() <= 256 ? doc::ColorMode::INDEXED:
doc::ColorMode::RGB), pal->size());
Sprite* sprite = doc->sprite();
doc->sprite()->setPalette(pal, false);
@@ -121,9 +120,14 @@ bool save_palette(const char *filename, const Palette* pal)
Image* image = layer->cel(frame_t(0))->image();
int x, y, c;
for (y=c=0; y<16; y++)
for (x=0; x<16; x++)
image->putPixel(x, y, c++);
for (y=c=0; y<h; ++y) {
for (x=0; x<w; ++x, ++c) {
if (doc->colorMode() == doc::ColorMode::INDEXED)
image->putPixel(x, y, c);
else
image->putPixel(x, y, pal->entry(c));
}
}
doc->setFilename(filename);
success = (save_document(&tmpContext, doc) == 0);
@@ -19,7 +19,8 @@ namespace app {
std::string get_writable_palette_extensions();
doc::Palette* load_palette(const char *filename);
bool save_palette(const char *filename, const doc::Palette* pal);
bool save_palette(const char *filename, const doc::Palette* pal,
int columns);
} // namespace app
@@ -19,10 +19,10 @@ namespace tools {
ShadeTable8::ShadeTable8(const app::ColorSwatches& colorSwatches, ShadingMode mode)
{
m_left.resize(Palette::MaxColors);
m_right.resize(Palette::MaxColors);
m_left.resize(256); // TODO could we have more than 256 indexes?
m_right.resize(256);
for (size_t i=0; i<Palette::MaxColors; ++i) {
for (size_t i=0; i<256; ++i) {
m_left[i] = i;
m_right[i] = i;
}
View
@@ -602,7 +602,7 @@ void ColorBar::onReverseColors()
++i;
}
Remap remap(Palette::MaxColors);
Remap remap(get_current_palette()->size());
i = 0;
j = n;
for (bool state : entries) {
@@ -645,7 +645,7 @@ void ColorBar::onSortBy(SortPaletteBy channel)
// Create a bigger new remap for the original palette (with all
// entries, selected and deselected).
Remap remapOrig(Palette::MaxColors);
Remap remapOrig(palette.size());
i = j = 0;
for (bool state : entries) {
if (state)
Oops, something went wrong.

0 comments on commit 89e0392

Please sign in to comment.