diff --git a/src/commands/cmd_change_image_type.cpp b/src/commands/cmd_change_image_type.cpp index 185f38c02..242a7798d 100644 --- a/src/commands/cmd_change_image_type.cpp +++ b/src/commands/cmd_change_image_type.cpp @@ -102,7 +102,6 @@ void ChangeImageTypeCommand::execute(Context* context) { CurrentSpriteWriter sprite(context); { - CurrentSpriteRgbMap rgbmap; Undoable undoable(sprite, "Color Mode Change"); undoable.set_imgtype(m_imgtype, m_dithering); diff --git a/src/commands/cmd_configure_tools.cpp b/src/commands/cmd_configure_tools.cpp index ef7367e16..003d8ec8b 100644 --- a/src/commands/cmd_configure_tools.cpp +++ b/src/commands/cmd_configure_tools.cpp @@ -301,7 +301,7 @@ static bool brush_preview_msg_proc(JWidget widget, JMessage msg) clear_to_color(bmp, makecol(0, 0, 0)); image_to_allegro(pen->get_image(), bmp, bmp->w/2 - pen->get_size()/2, - bmp->h/2 - pen->get_size()/2); + bmp->h/2 - pen->get_size()/2, NULL); blit(bmp, ji_screen, 0, 0, widget->rc->x1, widget->rc->y1, bmp->w, bmp->h); destroy_bitmap(bmp); diff --git a/src/commands/cmd_preview.cpp b/src/commands/cmd_preview.cpp index d860afc8d..40479a64a 100644 --- a/src/commands/cmd_preview.cpp +++ b/src/commands/cmd_preview.cpp @@ -120,7 +120,7 @@ void PreviewCommand::preview_sprite(Context* context, int flags) image = RenderEngine::renderSprite(sprite, 0, 0, sprite->getWidth(), sprite->getHeight(), sprite->getCurrentFrame(), 0); if (image) { - image_to_allegro(image, bmp, 0, 0); + image_to_allegro(image, bmp, 0, 0, sprite->getCurrentPalette()); image_free(image); } @@ -262,7 +262,7 @@ void PreviewCommand::preview_sprite(Context* context, int flags) image = RenderEngine::renderSprite(sprite, 0, 0, sprite->getWidth(), sprite->getHeight(), sprite->getCurrentFrame(), 0); if (image) { - image_to_allegro(image, bmp, 0, 0); + image_to_allegro(image, bmp, 0, 0, sprite->getCurrentPalette()); image_free(image); } } diff --git a/src/commands/cmd_sprite_size.cpp b/src/commands/cmd_sprite_size.cpp index 5d2d9fe19..716d8bebe 100644 --- a/src/commands/cmd_sprite_size.cpp +++ b/src/commands/cmd_sprite_size.cpp @@ -68,25 +68,24 @@ protected: { Undoable undoable(m_sprite, "Sprite Size"); - // get all sprite cels + // Get all sprite cels CelList cels; m_sprite->getCels(cels); - // for each cel... - for (CelIterator it = cels.begin(); it != cels.end(); ++it) { + // For each cel... + int progress = 0; + for (CelIterator it = cels.begin(); it != cels.end(); ++it, ++progress) { Cel* cel = *it; - // change it location + // Change its location undoable.set_cel_position(cel, scale_x(cel->x), scale_y(cel->y)); - } - // for each stock's image - for (int i=0; igetStock()->nimage; ++i) { - Image* image = stock_get_image(m_sprite->getStock(), i); + // Get cel's image + Image* image = stock_get_image(m_sprite->getStock(), cel->image); if (!image) continue; - // resize the image + // Resize the image int w = scale_x(image->w); int h = scale_y(image->h); Image* new_image = image_new(image->imgtype, MAX(1, w), MAX(1, h)); @@ -94,12 +93,12 @@ protected: image_fixup_transparent_colors(image); image_resize(image, new_image, m_resize_method, - get_current_palette(), - orig_rgb_map); + m_sprite->getPalette(cel->frame), + m_sprite->getRgbMap(cel->frame)); - undoable.replace_stock_image(i, new_image); + undoable.replace_stock_image(cel->image, new_image); - job_progress((float)i / m_sprite->getStock()->nimage); + job_progress((float)progress / cels.size()); // cancel all the operation? if (is_canceled()) @@ -120,8 +119,8 @@ protected: scale_y(m_sprite->getMask()->y-1), MAX(1, w), MAX(1, h)); image_resize(old_bitmap, new_mask->bitmap, m_resize_method, - get_current_palette(), - orig_rgb_map); + m_sprite->getCurrentPalette(), // Ignored + m_sprite->getRgbMap()); // Ignored image_free(old_bitmap); // reshrink diff --git a/src/core/color.cpp b/src/core/color.cpp index f99ac3eb9..c1ac7d9dc 100644 --- a/src/core/color.cpp +++ b/src/core/color.cpp @@ -546,7 +546,7 @@ int get_color_for_image(int imgtype, color_t color) break; } case IMAGE_INDEXED: - c = orig_rgb_map->data[r >> 3][g >> 3][b >> 3]; + c = get_current_palette()->findBestfit(r, g, b); break; } break; @@ -571,7 +571,7 @@ int get_color_for_image(int imgtype, color_t color) } case IMAGE_INDEXED: hsv_to_rgb_int(&h, &s, &v); - c = orig_rgb_map->data[h >> 3][s >> 3][v >> 3]; + c = get_current_palette()->findBestfit(h, s, v); break; } break; @@ -589,7 +589,7 @@ int get_color_for_image(int imgtype, color_t color) break; case IMAGE_INDEXED: c = data; - c = orig_rgb_map->data[c >> 3][c >> 3][c >> 3]; + c = get_current_palette()->findBestfit(c, c, c); break; } break; diff --git a/src/dialogs/drawtext.cpp b/src/dialogs/drawtext.cpp index 3805b5d8e..c28a6e107 100644 --- a/src/dialogs/drawtext.cpp +++ b/src/dialogs/drawtext.cpp @@ -165,7 +165,7 @@ Image *RenderText(Sprite* sprite, const char *fontname, int size, int color, con return render; } -static Image *render_text(Sprite* sprite, FONT *f, const char *text, int color) +static Image* render_text(Sprite* sprite, FONT *f, const char *text, int color) { /* TODO warning this uses Image->dat and not Image->line */ #define DO(type, colfunc) \ @@ -222,11 +222,9 @@ static Image *render_text(Sprite* sprite, FONT *f, const char *text, int color) DO(ase_uint16, _graya(_graya_getv(color), getg32(c))); break; - case IMAGE_INDEXED: { - CurrentSpriteRgbMap rgbmap; + case IMAGE_INDEXED: DO(ase_uint8, c == makecol32(255, 0, 255) ? 0: color); break; - } } release_bitmap(bmp); diff --git a/src/effect/colcurve.cpp b/src/effect/colcurve.cpp index bdc1c6f65..0936e831c 100644 --- a/src/effect/colcurve.cpp +++ b/src/effect/colcurve.cpp @@ -25,6 +25,7 @@ #include "modules/palettes.h" #include "raster/image.h" #include "raster/palette.h" +#include "raster/rgbmap.h" static struct { Curve *curve; @@ -332,12 +333,13 @@ void apply_color_curve2 (Effect *effect) void apply_color_curve1(Effect *effect) { Palette *pal = get_current_palette(); - ase_uint8 *src_address; - ase_uint8 *dst_address; + RgbMap* rgbmap = effect->sprite->getRgbMap(); + ase_uint8* src_address; + ase_uint8* dst_address; int x, c, r, g, b; - src_address = ((ase_uint8 **)effect->src->line)[effect->row+effect->y]+effect->x; - dst_address = ((ase_uint8 **)effect->dst->line)[effect->row+effect->y]+effect->x; + src_address = ((ase_uint8**)effect->src->line)[effect->row+effect->y]+effect->x; + dst_address = ((ase_uint8**)effect->dst->line)[effect->row+effect->y]+effect->x; for (x=0; xw; x++) { if (effect->mask_address) { @@ -364,7 +366,7 @@ void apply_color_curve1(Effect *effect) if (effect->target & TARGET_GREEN_CHANNEL) g = data.cmap[g]; if (effect->target & TARGET_BLUE_CHANNEL) b = data.cmap[b]; - c = orig_rgb_map->data[r>>3][g>>3][b>>3]; + c = rgbmap->mapColor(r, g, b); } *(dst_address++) = MID(0, c, 255); diff --git a/src/effect/convmatr.cpp b/src/effect/convmatr.cpp index 3dcb31fb9..140cc0b2e 100644 --- a/src/effect/convmatr.cpp +++ b/src/effect/convmatr.cpp @@ -31,6 +31,7 @@ #include "modules/palettes.h" #include "raster/image.h" #include "raster/palette.h" +#include "raster/rgbmap.h" #include "util/filetoks.h" /* TODO warning: this number could be dangerous for big filters */ @@ -470,8 +471,9 @@ void apply_convolution_matrix2(Effect *effect) void apply_convolution_matrix1(Effect *effect) { - Palette* pal = get_current_palette(); - ConvMatr* matrix = data.convmatr; + const Palette* pal = get_current_palette(); + const RgbMap* rgbmap = effect->sprite->getRgbMap(); + const ConvMatr* matrix = data.convmatr; const Image* src = effect->src; Image* dst = effect->dst; ase_uint8* src_address; @@ -537,7 +539,7 @@ void apply_convolution_matrix1(Effect *effect) else b = _rgba_getb(pal->getEntry(color)); - *(dst_address++) = orig_rgb_map->data[r>>3][g>>3][b>>3]; + *(dst_address++) = rgbmap->mapColor(r, g, b); } } } diff --git a/src/effect/invrtcol.cpp b/src/effect/invrtcol.cpp index 8f73da1dd..e6e9408b1 100644 --- a/src/effect/invrtcol.cpp +++ b/src/effect/invrtcol.cpp @@ -22,6 +22,7 @@ #include "modules/palettes.h" #include "raster/image.h" #include "raster/palette.h" +#include "raster/rgbmap.h" void apply_invert_color4(Effect *effect) { @@ -96,6 +97,7 @@ void apply_invert_color2(Effect *effect) void apply_invert_color1(Effect *effect) { Palette *pal = get_current_palette(); + RgbMap* rgbmap = effect->sprite->getRgbMap(); ase_uint8 *src_address; ase_uint8 *dst_address; int x, c, r, g, b; @@ -120,15 +122,15 @@ void apply_invert_color1(Effect *effect) if (effect->target & TARGET_INDEX_CHANNEL) c ^= 0xff; else { - r = _rgba_getr(pal->getEntry(c))>>3; - g = _rgba_getg(pal->getEntry(c))>>3; - b = _rgba_getb(pal->getEntry(c))>>3; + r = _rgba_getr(pal->getEntry(c)); + g = _rgba_getg(pal->getEntry(c)); + b = _rgba_getb(pal->getEntry(c)); - if (effect->target & TARGET_RED_CHANNEL) r ^= 0x1f; - if (effect->target & TARGET_GREEN_CHANNEL) g ^= 0x1f; - if (effect->target & TARGET_BLUE_CHANNEL) b ^= 0x1f; + if (effect->target & TARGET_RED_CHANNEL ) r ^= 0xff; + if (effect->target & TARGET_GREEN_CHANNEL) g ^= 0xff; + if (effect->target & TARGET_BLUE_CHANNEL ) b ^= 0xff; - c = orig_rgb_map->data[r][g][b]; + c = rgbmap->mapColor(r, g, b); } *(dst_address++) = c; diff --git a/src/effect/median.cpp b/src/effect/median.cpp index 59ec1eb87..f06d6d4d6 100644 --- a/src/effect/median.cpp +++ b/src/effect/median.cpp @@ -24,6 +24,7 @@ #include "modules/palettes.h" #include "raster/image.h" #include "raster/palette.h" +#include "raster/rgbmap.h" #include "tiled_mode.h" static struct { @@ -57,9 +58,9 @@ static int cmp_channel(const void *p1, const void *p2) void apply_median4(Effect *effect) { - Image *src = effect->src; + const Image *src = effect->src; Image *dst = effect->dst; - ase_uint32 *src_address; + const ase_uint32 *src_address; ase_uint32 *dst_address; int x, y, dx, dy, color; int c, w, r, g, b, a; @@ -125,9 +126,9 @@ void apply_median4(Effect *effect) void apply_median2(Effect *effect) { - Image *src = effect->src; + const Image *src = effect->src; Image *dst = effect->dst; - ase_uint16 *src_address; + const ase_uint16 *src_address; ase_uint16 *dst_address; int x, y, dx, dy, color; int c, w, k, a; @@ -181,11 +182,12 @@ void apply_median2(Effect *effect) void apply_median1(Effect *effect) { - Palette *pal = get_current_palette(); - Image *src = effect->src; - Image *dst = effect->dst; - ase_uint8 *src_address; - ase_uint8 *dst_address; + const Palette* pal = get_current_palette(); + const RgbMap* rgbmap = effect->sprite->getRgbMap(); + const Image* src = effect->src; + Image* dst = effect->dst; + const ase_uint8* src_address; + ase_uint8* dst_address; int x, y, dx, dy, color; int c, w, r, g, b; int getx, gety, addx, addy; @@ -251,7 +253,7 @@ void apply_median1(Effect *effect) else b = _rgba_getb(pal->getEntry(color)); - *(dst_address++) = orig_rgb_map->data[r>>3][g>>3][b>>3]; + *(dst_address++) = rgbmap->mapColor(r, g, b); } } } diff --git a/src/modules/gfx.cpp b/src/modules/gfx.cpp index 30a65a975..4e730aa92 100644 --- a/src/modules/gfx.cpp +++ b/src/modules/gfx.cpp @@ -433,7 +433,6 @@ void draw_color(BITMAP* bmp, const Rect& rc, int imgtype, color_t color) data = color_get_index(color); rectfill(bmp, rc.x, rc.y, rc.x+rc.w-1, rc.y+rc.h-1, get_color_for_allegro(bitmap_color_depth(bmp), color)); - // palette_color[data]); return; } @@ -442,8 +441,6 @@ void draw_color(BITMAP* bmp, const Rect& rc, int imgtype, color_t color) case IMAGE_INDEXED: rectfill(bmp, rc.x, rc.y, rc.x+rc.w-1, rc.y+rc.h-1, get_color_for_allegro(imgtype, color_index(get_color_for_image(imgtype, color)))); - // get_color_for_allegro(bitmap_color_depth(bmp), color)); - // palette_color[get_color_for_image(imgtype, color)]); break; case IMAGE_RGB: @@ -459,10 +456,7 @@ void draw_color(BITMAP* bmp, const Rect& rc, int imgtype, color_t color) rectfill(graph, 0, 0, rc.w-1, rc.h-1, get_color_for_allegro(32, color2)); } - { - CurrentSpriteRgbMap rgbmap; - blit(graph, bmp, 0, 0, rc.x, rc.y, rc.w, rc.h); - } + blit(graph, bmp, 0, 0, rc.x, rc.y, rc.w, rc.h); destroy_bitmap(graph); break; @@ -478,10 +472,7 @@ void draw_color(BITMAP* bmp, const Rect& rc, int imgtype, color_t color) rectfill(graph, 0, 0, rc.w-1, rc.h-1, get_color_for_allegro(32, color2)); } - { - CurrentSpriteRgbMap rgbmap; - blit(graph, bmp, 0, 0, rc.x, rc.y, rc.w, rc.h); - } + blit(graph, bmp, 0, 0, rc.x, rc.y, rc.w, rc.h); destroy_bitmap(graph); break; diff --git a/src/modules/palettes.cpp b/src/modules/palettes.cpp index 57486cd94..342016fa7 100644 --- a/src/modules/palettes.cpp +++ b/src/modules/palettes.cpp @@ -31,34 +31,19 @@ #include "raster/palette.h" #include "raster/sprite.h" -RGB_MAP *orig_rgb_map = NULL; /* color map for the original palette - (not for the mapped-palette) */ -COLOR_MAP *orig_trans_map = NULL; - /** * The default color palette. */ -static Palette *ase_default_palette = NULL; +static Palette* ase_default_palette = NULL; /** * Current original palette (you can use _current_palette from Allegro * to refer to the current system "mapped-palette"). */ -static Palette *ase_current_palette = NULL; - -/* current rgb map */ -static RGB_MAP *my_rgb_map = NULL; -static int regen_my_rgb_map = false; +static Palette* ase_current_palette = NULL; int init_module_palette() { - orig_rgb_map = jnew(RGB_MAP, 1); - orig_trans_map = jnew(COLOR_MAP, 1); - my_rgb_map = jnew(RGB_MAP, 1); - - rgb_map = my_rgb_map; - color_map = NULL; - ase_default_palette = new Palette(0, 256); ase_default_palette->fromAllegro(default_palette); @@ -70,25 +55,16 @@ int init_module_palette() void exit_module_palette() { - rgb_map = NULL; - - if (ase_default_palette != NULL) - delete ase_default_palette; - - if (ase_current_palette != NULL) - delete ase_current_palette; - - jfree(my_rgb_map); - jfree(orig_trans_map); - jfree(orig_rgb_map); + delete ase_default_palette; + delete ase_current_palette; } -Palette *get_current_palette() +Palette* get_current_palette() { return ase_current_palette; } -Palette *get_default_palette() +Palette* get_default_palette() { return ase_default_palette; } @@ -106,29 +82,19 @@ void set_default_palette(Palette* palette) */ bool set_current_palette(const Palette *_palette, bool forced) { - const Palette *palette = _palette ? _palette: ase_default_palette; + const Palette* palette = _palette ? _palette: ase_default_palette; bool ret = false; - /* have changes */ + // Have changes if (forced || palette->countDiff(ase_current_palette, NULL, NULL) > 0) { - PALETTE rgbpal; - - /* copy current palette */ + // Copy current palette palette->copyColorsTo(ase_current_palette); - /* create a RGB map for the original palette */ - palette->toAllegro(rgbpal); - create_rgb_table(orig_rgb_map, rgbpal, NULL); - - /* create a transparency-map with the original palette */ - rgb_map = orig_rgb_map; - create_trans_table(orig_trans_map, rgbpal, 128, 128, 128, NULL); - rgb_map = my_rgb_map; - - // Create a map for the mapped-palette - create_rgb_table(my_rgb_map, rgbpal, NULL); - set_palette(rgbpal); // Change system color palette + // Change system color palette + PALETTE allegPal; + palette->toAllegro(allegPal); + set_palette(allegPal); // Call slots in signals App::instance()->PaletteChange(); @@ -146,7 +112,7 @@ void set_black_palette() delete p; } -/* changes a color of the current system palette */ +// Changes a color of the current system palette void set_current_color(int index, int r, int g, int b) { register int c; @@ -172,20 +138,3 @@ void set_current_color(int index, int r, int g, int b) set_color(index, &rgb); } } - -////////////////////////////////////////////////////////////////////// - -CurrentSpriteRgbMap::CurrentSpriteRgbMap() -{ - rgb_map = orig_rgb_map; -} - -CurrentSpriteRgbMap::~CurrentSpriteRgbMap() -{ - rgb_map = my_rgb_map; - - if (regen_my_rgb_map) { - regen_my_rgb_map = false; - create_rgb_table(my_rgb_map, _current_palette, NULL); - } -} diff --git a/src/modules/palettes.h b/src/modules/palettes.h index 5b5552f30..b68cb19a3 100644 --- a/src/modules/palettes.h +++ b/src/modules/palettes.h @@ -19,14 +19,7 @@ #ifndef MODULES_PALETTES_H_INCLUDED #define MODULES_PALETTES_H_INCLUDED -#include "jinete/jbase.h" -#include - class Palette; -class Sprite; - -extern RGB_MAP* orig_rgb_map; -extern COLOR_MAP* orig_trans_map; int init_module_palette(); void exit_module_palette(); @@ -39,12 +32,5 @@ bool set_current_palette(const Palette* palette, bool forced); void set_black_palette(); void set_current_color(int index, int r, int g, int b); -class CurrentSpriteRgbMap -{ -public: - CurrentSpriteRgbMap(); - ~CurrentSpriteRgbMap(); -}; - #endif diff --git a/src/raster/image.cpp b/src/raster/image.cpp index 1d2d7b8cb..b4ccb5396 100644 --- a/src/raster/image.cpp +++ b/src/raster/image.cpp @@ -29,6 +29,7 @@ #include "raster/image.h" #include "raster/image_impl.h" #include "raster/palette.h" +#include "raster/rgbmap.h" ////////////////////////////////////////////////////////////////////// @@ -317,9 +318,9 @@ void image_ellipsefill(Image* image, int x1, int y1, int x2, int y2, int color) algo_ellipsefill(x1, y1, x2, y2, &data, (AlgoHLine)hline_for_image); } -void image_to_allegro(const Image* image, BITMAP *bmp, int x, int y) +void image_to_allegro(const Image* image, BITMAP *bmp, int x, int y, const Palette* palette) { - image->to_allegro(bmp, x, y); + image->to_allegro(bmp, x, y, palette); } /** @@ -417,7 +418,7 @@ void image_fixup_transparent_colors(Image* image) * recommended to use @ref image_fixup_transparent_colors function * over the source image @a src before using this routine. */ -void image_resize(const Image* src, Image* dst, ResizeMethod method, Palette* pal, RGB_MAP* rgb_map) +void image_resize(const Image* src, Image* dst, ResizeMethod method, const Palette* pal, const RgbMap* rgbmap) { switch (method) { @@ -519,7 +520,7 @@ void image_resize(const Image* src, Image* dst, ResizeMethod method, Palette* pa (_rgba_getb(pal->getEntry(color[2]))*u2 + _rgba_getb(pal->getEntry(color[3]))*u1)*v1); int a = (((color[0] == 0 ? 0: 255)*u2 + (color[1] == 0 ? 0: 255)*u1)*v2 + ((color[2] == 0 ? 0: 255)*u2 + (color[3] == 0 ? 0: 255)*u1)*v1); - dst_color = a > 127 ? rgb_map->data[r>>3][g>>3][b>>3]: 0; + dst_color = a > 127 ? rgbmap->mapColor(r, g, b): 0; break; } case IMAGE_BITMAP: { diff --git a/src/raster/image.h b/src/raster/image.h index 7101bc4e9..b355975cc 100644 --- a/src/raster/image.h +++ b/src/raster/image.h @@ -25,6 +25,7 @@ class Palette; class Pen; +class RgbMap; // Image Types enum { @@ -60,7 +61,7 @@ public: virtual void merge(const Image* src, int x, int y, int opacity, int blend_mode) = 0; virtual void hline(int x1, int y, int x2, int color) = 0; virtual void rectfill(int x1, int y1, int x2, int y2, int color) = 0; - virtual void to_allegro(BITMAP* bmp, int x, int y) const = 0; + virtual void to_allegro(BITMAP* bmp, int x, int y, const Palette* palette) const = 0; }; Image* image_new(int imgtype, int w, int h); @@ -90,10 +91,10 @@ void image_line(Image* image, int x1, int y1, int x2, int y2, int color); void image_ellipse(Image* image, int x1, int y1, int x2, int y2, int color); void image_ellipsefill(Image* image, int x1, int y1, int x2, int y2, int color); -void image_to_allegro(const Image* image, BITMAP* bmp, int x, int y); +void image_to_allegro(const Image* image, BITMAP* bmp, int x, int y, const Palette* palette); void image_fixup_transparent_colors(Image* image); -void image_resize(const Image* src, Image* dst, ResizeMethod method, Palette* palette, RGB_MAP* rgb_map); +void image_resize(const Image* src, Image* dst, ResizeMethod method, const Palette* palette, const RgbMap* rgbmap); int image_count_diff(const Image* i1, const Image* i2); bool image_shrink_rect(Image *image, int *x1, int *y1, int *x2, int *y2, int refpixel); diff --git a/src/raster/image_impl.h b/src/raster/image_impl.h index 6cef733cc..0f6242718 100644 --- a/src/raster/image_impl.h +++ b/src/raster/image_impl.h @@ -22,6 +22,7 @@ #include #include "raster/image.h" +#include "raster/palette.h" template class ImageImpl : public Image @@ -220,7 +221,7 @@ public: } } - virtual void to_allegro(BITMAP* bmp, int x, int y) const; + virtual void to_allegro(BITMAP* bmp, int x, int y, const Palette* palette) const; }; @@ -492,7 +493,7 @@ void ImageImpl::merge(const Image* src, int x, int y, int opacity, } template<> -void ImageImpl::to_allegro(BITMAP *bmp, int _x, int _y) const +void ImageImpl::to_allegro(BITMAP *bmp, int _x, int _y, const Palette* palette) const { const_address_t addr = raw_pixels(); unsigned long bmp_address; @@ -624,7 +625,7 @@ void ImageImpl::to_allegro(BITMAP *bmp, int _x, int _y) const } template<> -void ImageImpl::to_allegro(BITMAP *bmp, int _x, int _y) const +void ImageImpl::to_allegro(BITMAP *bmp, int _x, int _y, const Palette* palette) const { const_address_t addr = raw_pixels(); unsigned long bmp_address; @@ -749,17 +750,13 @@ void ImageImpl::to_allegro(BITMAP *bmp, int _x, int _y) const } template<> -void ImageImpl::to_allegro(BITMAP *bmp, int _x, int _y) const +void ImageImpl::to_allegro(BITMAP *bmp, int _x, int _y, const Palette* palette) const { -#define RGB_TRIPLET \ - _rgb_scale_6[_current_palette[(*addr)].r], \ - _rgb_scale_6[_current_palette[(*addr)].g], \ - _rgb_scale_6[_current_palette[(*addr)].b] - const_address_t addr = raw_pixels(); unsigned long bmp_address; int depth = bitmap_color_depth(bmp); int x, y; + ase_uint32 c; bmp_select(bmp); @@ -805,7 +802,8 @@ void ImageImpl::to_allegro(BITMAP *bmp, int _x, int _y) const bmp_address = bmp_write_line(bmp, _y)+_x; for (x=0; xgetEntry(*addr); + bmp_write15(bmp_address, makecol15(_rgba_getr(c), _rgba_getg(c), _rgba_getb(c))); addr++; bmp_address += 2; } @@ -821,7 +819,8 @@ void ImageImpl::to_allegro(BITMAP *bmp, int _x, int _y) const bmp_address = bmp_write_line(bmp, _y)+_x; for (x=0; xgetEntry(*addr); + bmp_write16(bmp_address, makecol16(_rgba_getr(c), _rgba_getg(c), _rgba_getb(c))); addr++; bmp_address += 2; } @@ -837,7 +836,8 @@ void ImageImpl::to_allegro(BITMAP *bmp, int _x, int _y) const bmp_address = bmp_write_line(bmp, _y)+_x; for (x=0; xgetEntry(*addr); + bmp_write24(bmp_address, makecol24(_rgba_getr(c), _rgba_getg(c), _rgba_getb(c))); addr++; bmp_address += 3; } @@ -853,7 +853,8 @@ void ImageImpl::to_allegro(BITMAP *bmp, int _x, int _y) const bmp_address = bmp_write_line(bmp, _y)+_x; for (x=0; xgetEntry(*addr); + bmp_write32(bmp_address, makeacol32(_rgba_getr(c), _rgba_getg(c), _rgba_getb(c), 255)); addr++; bmp_address += 4; } @@ -867,7 +868,7 @@ void ImageImpl::to_allegro(BITMAP *bmp, int _x, int _y) const } template<> -void ImageImpl::to_allegro(BITMAP *bmp, int _x, int _y) const +void ImageImpl::to_allegro(BITMAP *bmp, int _x, int _y, const Palette* palette) const { const_address_t addr; unsigned long bmp_address; diff --git a/src/raster/quant.cpp b/src/raster/quant.cpp index 6e59d30d6..474419e91 100644 --- a/src/raster/quant.cpp +++ b/src/raster/quant.cpp @@ -22,11 +22,12 @@ #include "raster/image.h" #include "raster/quant.h" #include "raster/palette.h" +#include "raster/rgbmap.h" -Image *image_set_imgtype(Image *image, int imgtype, +Image *image_set_imgtype(const Image* image, int imgtype, int dithering_method, - RGB_MAP *rgb_map, - Palette *palette) + const RgbMap* rgbmap, + const Palette* palette) { ase_uint32* rgb_address; ase_uint16* gray_address; @@ -42,7 +43,7 @@ Image *image_set_imgtype(Image *image, int imgtype, else if (image->imgtype == IMAGE_RGB && imgtype == IMAGE_INDEXED && dithering_method == DITHERING_ORDERED) { - return image_rgb_to_indexed(image, 0, 0, rgb_map, palette); + return image_rgb_to_indexed(image, 0, 0, rgbmap, palette); } new_image = image_new(imgtype, image->w, image->h); @@ -82,7 +83,7 @@ Image *image_set_imgtype(Image *image, int imgtype, if (_rgba_geta(c) == 0) *idx_address = 0; else - *idx_address = rgb_map->data[r>>3][g>>3][b>>3]; + *idx_address = rgbmap->mapColor(r, g, b); rgb_address++; idx_address++; } @@ -196,10 +197,10 @@ static int pattern[8][8] = { 4 * ((g1)-(g2)) * ((g1)-(g2)) + \ 2 * ((b1)-(b2)) * ((b1)-(b2))) -Image *image_rgb_to_indexed(Image *src_image, +Image* image_rgb_to_indexed(const Image* src_image, int offsetx, int offsety, - RGB_MAP *rgb_map, - Palette *palette) + const RgbMap* rgbmap, + const Palette* palette) { int oppr, oppg, oppb, oppnrcm; Image *dst_image; @@ -223,7 +224,7 @@ Image *image_rgb_to_indexed(Image *src_image, a = _rgba_geta(c); if (a != 0) { - nearestcm = rgb_map->data[r>>3][g>>3][b>>3]; + nearestcm = rgbmap->mapColor(r, g, b); /* rgb values for nearest color */ nr = _rgba_getr(palette->getEntry(nearestcm)); ng = _rgba_getg(palette->getEntry(nearestcm)); @@ -233,7 +234,7 @@ Image *image_rgb_to_indexed(Image *src_image, oppg = MID(0, 2*g - ng, 255); oppb = MID(0, 2*b - nb, 255); /* Nearest match for opposite color: */ - oppnrcm = rgb_map->data[oppr>>3][oppg>>3][oppb>>3]; + oppnrcm = rgbmap->mapColor(oppr, oppg, oppb); /* If they're not the same, dither between them. */ /* Dither constant is measured by where the true color lies between the two nearest approximations. diff --git a/src/raster/quant.h b/src/raster/quant.h index 708f5086f..f3084e773 100644 --- a/src/raster/quant.h +++ b/src/raster/quant.h @@ -21,7 +21,7 @@ #include -/* dithering methods */ +// Dithering methods enum { DITHERING_NONE, DITHERING_ORDERED, @@ -29,15 +29,16 @@ enum { class Image; class Palette; +class RgbMap; -Image* image_set_imgtype(Image* image, int imgtype, +Image* image_set_imgtype(const Image* image, int imgtype, int dithering_method, - RGB_MAP* rgb_map, - Palette* palette); + const RgbMap* rgbmap, + const Palette* palette); -Image* image_rgb_to_indexed(Image* src_image, +Image* image_rgb_to_indexed(const Image* src_image, int offsetx, int offsety, - RGB_MAP* rgb_map, - Palette* palette); + const RgbMap* rgbmap, + const Palette* palette); #endif diff --git a/src/raster/sprite.cpp b/src/raster/sprite.cpp index c8458526f..b233a8cf1 100644 --- a/src/raster/sprite.cpp +++ b/src/raster/sprite.cpp @@ -177,6 +177,17 @@ public: return getPalette(getCurrentFrame()); } + RgbMap* getRgbMap(int frame) { + if (m_rgbMap == NULL) { + m_rgbMap = new RgbMap(); + m_rgbMap->regenerate(getPalette(frame)); + } + else if (!m_rgbMap->match(getPalette(frame))) { + m_rgbMap->regenerate(getPalette(frame)); + } + return m_rgbMap; + } + int getTotalFrames() const { return m_frames; } @@ -406,6 +417,9 @@ private: // Data to save the file in the same format that it was loaded FormatOptions* m_format_options; + + // Current rgb map + RgbMap* m_rgbMap; }; SpriteImpl::SpriteImpl(Sprite* sprite, int imgtype, int width, int height, int ncolors) @@ -471,6 +485,9 @@ SpriteImpl::SpriteImpl(Sprite* sprite, int imgtype, int width, int height, int n // File format options m_format_options = NULL; + // Initial RGB map + m_rgbMap = NULL; + setPalette(&pal, true); } @@ -618,6 +635,8 @@ SpriteImpl::~SpriteImpl() // Destroy file format options if (m_format_options) format_options_free(m_format_options); + + delete m_rgbMap; } /** @@ -1228,6 +1247,16 @@ Palette* Sprite::getCurrentPalette() return m_impl->getCurrentPalette(); } +RgbMap* Sprite::getRgbMap() +{ + return m_impl->getRgbMap(getCurrentFrame()); +} + +RgbMap* Sprite::getRgbMap(int frame) +{ + return m_impl->getRgbMap(frame); +} + ////////////////////////////////////////////////////////////////////// // Frames diff --git a/src/raster/sprite.h b/src/raster/sprite.h index 418c1c853..a27dcade5 100644 --- a/src/raster/sprite.h +++ b/src/raster/sprite.h @@ -33,6 +33,7 @@ class Path; class Stock; class Undo; class Sprite; +class RgbMap; struct _BoundSeg; struct PreferredEditorSettings @@ -124,6 +125,9 @@ public: const Palette* getCurrentPalette() const; Palette* getCurrentPalette(); + RgbMap* getRgbMap(); + RgbMap* getRgbMap(int frame); + //////////////////////////////////////// // Frames diff --git a/src/tools/ink_processing.h b/src/tools/ink_processing.h index 0c3ba7731..47abd49e7 100644 --- a/src/tools/ink_processing.h +++ b/src/tools/ink_processing.h @@ -19,6 +19,8 @@ #include "effect/effect.h" #include "modules/palettes.h" #include "raster/palette.h" +#include "raster/rgbmap.h" +#include "raster/sprite.h" ////////////////////////////////////////////////////////////////////// // Ink Processing @@ -137,6 +139,7 @@ static void ink_hline16_transparent(int x1, int y, int x2, IToolLoop* loop) static void ink_hline8_transparent(int x1, int y, int x2, IToolLoop* loop) { Palette* pal = get_current_palette(); + RgbMap* rgbmap = loop->getSprite()->getRgbMap(); ase_uint32 c; ase_uint32 tc = pal->getEntry(loop->getPrimaryColor()); int opacity = loop->getOpacity(); @@ -145,10 +148,9 @@ static void ink_hline8_transparent(int x1, int y, int x2, IToolLoop* loop) (IndexedTraits, { c = _rgba_blend_normal(pal->getEntry(*src_address), tc, opacity); - *dst_address = orig_rgb_map->data - [_rgba_getr(c)>>3] - [_rgba_getg(c)>>3] - [_rgba_getb(c)>>3]; + *dst_address = rgbmap->mapColor(_rgba_getr(c), + _rgba_getg(c), + _rgba_getb(c)); }); } @@ -253,6 +255,7 @@ static void ink_hline16_blur(int x1, int y, int x2, IToolLoop* loop) static void ink_hline8_blur(int x1, int y, int x2, IToolLoop* loop) { Palette *pal = get_current_palette(); + RgbMap* rgbmap = loop->getSprite()->getRgbMap(); int c, r, g, b, a; int opacity = loop->getOpacity(); TiledMode tiled = loop->getTiledMode(); @@ -292,7 +295,7 @@ static void ink_hline8_blur(int x1, int y, int x2, IToolLoop* loop) g = _rgba_getg(c) + (g-_rgba_getg(c)) * opacity / 255; b = _rgba_getb(c) + (b-_rgba_getb(c)) * opacity / 255; - *dst_address = orig_rgb_map->data[r>>3][g>>3][b>>3]; + *dst_address = rgbmap->mapColor(r, g, b); } else { *dst_address = *src_address; @@ -334,6 +337,7 @@ static void ink_hline8_replace(int x1, int y, int x2, IToolLoop* loop) { int color1 = loop->getPrimaryColor(); Palette *pal = get_current_palette(); + RgbMap* rgbmap = loop->getSprite()->getRgbMap(); ase_uint32 c; ase_uint32 tc = pal->getEntry(loop->getSecondaryColor()); int opacity = loop->getOpacity(); @@ -342,10 +346,9 @@ static void ink_hline8_replace(int x1, int y, int x2, IToolLoop* loop) (IndexedTraits, if (*src_address == color1) { c = _rgba_blend_normal(pal->getEntry(*src_address), tc, opacity); - *dst_address = orig_rgb_map->data - [_rgba_getr(c)>>3] - [_rgba_getg(c)>>3] - [_rgba_getb(c)>>3]; + *dst_address = rgbmap->mapColor(_rgba_getr(c), + _rgba_getg(c), + _rgba_getb(c)); }); } @@ -412,7 +415,8 @@ static void ink_hline16_jumble(int x1, int y, int x2, IToolLoop* loop) static void ink_hline8_jumble(int x1, int y, int x2, IToolLoop* loop) { - Palette *pal = get_current_palette(); + const Palette *pal = get_current_palette(); + const RgbMap* rgbmap = loop->getSprite()->getRgbMap(); ase_uint32 c, tc; int opacity = loop->getOpacity(); Point speed(loop->getSpeed() / 4); @@ -429,10 +433,9 @@ static void ink_hline8_jumble(int x1, int y, int x2, IToolLoop* loop) tc, opacity); if (_rgba_geta(c) >= 128) - *dst_address = orig_rgb_map->data - [_rgba_getr(c)>>3] - [_rgba_getg(c)>>3] - [_rgba_getb(c)>>3]; + *dst_address = rgbmap->mapColor(_rgba_getr(c), + _rgba_getg(c), + _rgba_getb(c)); else *dst_address = 0; } diff --git a/src/undoable.cpp b/src/undoable.cpp index 1e5b7349a..3395b4605 100644 --- a/src/undoable.cpp +++ b/src/undoable.cpp @@ -188,9 +188,6 @@ void Undoable::autocrop_sprite(int bgcolor) crop_sprite(x1, y1, x2-x1+1, y2-y1+1, bgcolor); } -/** - * @warning: it uses the current Allegro "rgb_map" - */ void Undoable::set_imgtype(int new_imgtype, int dithering_method) { Image *old_image; @@ -206,13 +203,15 @@ void Undoable::set_imgtype(int new_imgtype, int dithering_method) m_sprite->getStock()->imgtype = new_imgtype; + // Use the rgbmap for the specified sprite + const RgbMap* rgbmap = m_sprite->getRgbMap(); + for (c=0; cgetStock()->nimage; c++) { old_image = stock_get_image(m_sprite->getStock(), c); if (!old_image) continue; - new_image = image_set_imgtype(old_image, new_imgtype, dithering_method, - rgb_map, + new_image = image_set_imgtype(old_image, new_imgtype, dithering_method, rgbmap, // TODO check this out m_sprite->getCurrentPalette()); if (!new_image) diff --git a/src/util/clipboard.cpp b/src/util/clipboard.cpp index 7e39fb6db..225a9983a 100644 --- a/src/util/clipboard.cpp +++ b/src/util/clipboard.cpp @@ -233,9 +233,9 @@ void clipboard::paste(SpriteWriter& sprite) if (clipboard_image->imgtype == sprite->getImgType()) src_image = clipboard_image; else { - CurrentSpriteRgbMap rgbmap; + RgbMap* rgbmap = sprite->getRgbMap(); src_image = image_set_imgtype(clipboard_image, sprite->getImgType(), DITHERING_NONE, - rgb_map, sprite->getPalette(sprite->getCurrentFrame())); + rgbmap, sprite->getPalette(sprite->getCurrentFrame())); } // do the interactive-transform loop (where the user can move the floating image) @@ -350,7 +350,7 @@ static bool interactive_transform(Editor* editor, /* generate the preview bitmap (for fast-blitting) */ preview = create_bitmap(image->w, image->h); mask_color = bitmap_mask_color(preview); - image_to_allegro(image, preview, 0, 0); + image_to_allegro(image, preview, 0, 0, get_current_palette()); switch (image->imgtype) { diff --git a/src/util/render.cpp b/src/util/render.cpp index 504b89f9f..5cb59f847 100644 --- a/src/util/render.cpp +++ b/src/util/render.cpp @@ -375,14 +375,14 @@ Image* RenderEngine::renderSprite(Sprite* sprite, frame, zoom, zoomed_func, true, false); // Draw transparent layers of the previous frame with opacity=128 - color_map = orig_trans_map; + //color_map = orig_trans_map; global_opacity = 128; renderLayer(sprite, sprite->getFolder(), image, source_x, source_y, frame-1, zoom, zoomed_func, false, true); // Draw transparent layers of the current frame with opacity=255 - color_map = NULL; + //color_map = NULL; global_opacity = 255; renderLayer(sprite, sprite->getFolder(), image, source_x, source_y, diff --git a/src/widgets/editor/editor.cpp b/src/widgets/editor/editor.cpp index 002fc6c91..a8b1b9299 100644 --- a/src/widgets/editor/editor.cpp +++ b/src/widgets/editor/editor.cpp @@ -313,7 +313,7 @@ void Editor::editor_draw_sprite(int x1, int y1, int x2, int y2) BITMAP *bmp = create_bitmap(width, height); use_current_sprite_rgb_map(); - image_to_allegro(rendered, bmp, 0, 0); + image_to_allegro(rendered, bmp, 0, 0, m_sprite->getCurrentPalette()); blit(bmp, ji_screen, 0, 0, dest_x, dest_y, width, height); restore_rgb_map(); @@ -321,21 +321,7 @@ void Editor::editor_draw_sprite(int x1, int y1, int x2, int y2) destroy_bitmap(bmp); #else acquire_bitmap(ji_screen); - - CurrentSpriteRgbMap rgbmap; - if (bitmap_color_depth(screen) == 8) { - image_to_allegro(rendered, ji_screen, dest_x, dest_y); - } - else { - PALETTE rgbpal; - Palette *pal = m_sprite->getPalette(m_sprite->getCurrentFrame()); - pal->toAllegro(rgbpal); - - select_palette(rgbpal); - image_to_allegro(rendered, ji_screen, dest_x, dest_y); - unselect_palette(); - } - + image_to_allegro(rendered, ji_screen, dest_x, dest_y, m_sprite->getCurrentPalette()); release_bitmap(ji_screen); image_free(rendered); diff --git a/src/widgets/fileview.cpp b/src/widgets/fileview.cpp index d917305e7..cfb1e0866 100644 --- a/src/widgets/fileview.cpp +++ b/src/widgets/fileview.cpp @@ -70,7 +70,7 @@ typedef struct ThumbnailData JWidget fileview; Image* thumbnail; JThread thread; - PALETTE rgbpal; + Palette* palette; } ThumbnailData; static FileView* fileview_data(JWidget widget); @@ -781,16 +781,16 @@ static void openfile_bg(void *_data) if (fop_is_stop(fop)) delete fop->sprite; else { - /* the palette to convert the Image to a BITMAP */ - sprite->getPalette(0)->toAllegro(data->rgbpal); + // The palette to convert the Image to a BITMAP + data->palette = new Palette(*sprite->getPalette(0)); - /* render the 'sprite' in one plain 'image' */ + // Render the 'sprite' in one plain 'image' image = image_new(sprite->getImgType(), sprite->getWidth(), sprite->getHeight()); image_clear(image, 0); sprite->render(image, 0, 0); delete sprite; - /* calculate the thumbnail size */ + // Calculate the thumbnail size thumb_w = MAX_THUMBNAIL_SIZE * image->w / MAX(image->w, image->h); thumb_h = MAX_THUMBNAIL_SIZE * image->h / MAX(image->w, image->h); if (MAX(thumb_w, thumb_h) > MAX(image->w, image->h)) { @@ -800,7 +800,7 @@ static void openfile_bg(void *_data) thumb_w = MID(1, thumb_w, MAX_THUMBNAIL_SIZE); thumb_h = MID(1, thumb_h, MAX_THUMBNAIL_SIZE); - /* stretch the 'image' */ + // Stretch the 'image' data->thumbnail = image_new(image->imgtype, thumb_w, thumb_h); image_clear(data->thumbnail, 0); image_scale(data->thumbnail, image, 0, 0, thumb_w, thumb_h); @@ -830,12 +830,12 @@ static void monitor_thumbnail_generation(void *_data) data->thumbnail->w, data->thumbnail->h); - select_palette(data->rgbpal); - image_to_allegro(data->thumbnail, bmp, 0, 0); - unselect_palette(); + image_to_allegro(data->thumbnail, bmp, 0, 0, data->palette); - image_free(data->thumbnail); + delete data->thumbnail; // image + delete data->palette; data->thumbnail = NULL; + data->palette = NULL; fileitem_set_thumbnail(data->fileitem, bmp); @@ -848,8 +848,9 @@ static void monitor_thumbnail_generation(void *_data) remove_gui_monitor(data->monitor); } - else + else { jwidget_dirty(data->fileview); + } } /**