/* ASE - Allegro Sprite Editor * Copyright (C) 2001-2010 David Capello * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "config.h" #include #include #include #include "gfx/hsv.h" #include "gfx/rgb.h" #include "raster/blend.h" #include "raster/color_histogram.h" #include "raster/image.h" #include "raster/images_collector.h" #include "raster/palette.h" #include "raster/quantization.h" #include "raster/rgbmap.h" #include "raster/sprite.h" using namespace gfx; // Converts a RGB image to indexed with ordered dithering method. static Image* ordered_dithering(const Image* src_image, int offsetx, int offsety, const RgbMap* rgbmap, const Palette* palette); static void create_palette_from_bitmaps(const std::vector& images, Palette* palette, bool has_background_layer); Palette* quantization::create_palette_from_rgb(const Sprite* sprite) { bool has_background_layer = (sprite->getBackgroundLayer() != NULL); Palette* palette = new Palette(0, 256); Image* flat_image; ImagesCollector images(sprite, true, // all layers true, // all frames, false); // forWrite=false, read only // Add a flat image with the current sprite's frame rendered flat_image = image_new(sprite->getImgType(), sprite->getWidth(), sprite->getHeight()); image_clear(flat_image, 0); sprite->render(flat_image, 0, 0); // Create an array of images size_t nimage = images.size() + 1; // +1 for flat_image std::vector image_array(nimage); size_t c = 0; for (ImagesCollector::ItemsIterator it=images.begin(); it!=images.end(); ++it) image_array[c++] = it->image(); image_array[c++] = flat_image; // The 'flat_image' // Generate an optimized palette for all images create_palette_from_bitmaps(image_array, palette, has_background_layer); delete flat_image; return palette; } Image* quantization::convert_imgtype(const Image* image, int imgtype, DitheringMethod ditheringMethod, const RgbMap* rgbmap, const Palette* palette) { ase_uint32* rgb_address; ase_uint16* gray_address; ase_uint8* idx_address; ase_uint32 c; int i, r, g, b, size; Image *new_image; // no convertion if (image->imgtype == imgtype) return NULL; // RGB -> Indexed with ordered dithering else if (image->imgtype == IMAGE_RGB && imgtype == IMAGE_INDEXED && ditheringMethod == DITHERING_ORDERED) { return ordered_dithering(image, 0, 0, rgbmap, palette); } new_image = image_new(imgtype, image->w, image->h); if (!new_image) return NULL; size = image->w*image->h; switch (image->imgtype) { case IMAGE_RGB: rgb_address = (ase_uint32*)image->dat; switch (new_image->imgtype) { // RGB -> Grayscale case IMAGE_GRAYSCALE: gray_address = (ase_uint16*)new_image->dat; for (i=0; i Indexed case IMAGE_INDEXED: idx_address = new_image->dat; for (i=0; imapColor(r, g, b); rgb_address++; idx_address++; } break; } break; case IMAGE_GRAYSCALE: gray_address = (ase_uint16*)image->dat; switch (new_image->imgtype) { // Grayscale -> RGB case IMAGE_RGB: rgb_address = (ase_uint32*)new_image->dat; for (i=0; i Indexed case IMAGE_INDEXED: idx_address = new_image->dat; for (i=0; idat; switch (new_image->imgtype) { // Indexed -> RGB case IMAGE_RGB: rgb_address = (ase_uint32*)new_image->dat; for (i=0; igetEntry(c)), _rgba_getg(palette->getEntry(c)), _rgba_getb(palette->getEntry(c)), 255); idx_address++; rgb_address++; } break; // Indexed -> Grayscale case IMAGE_GRAYSCALE: gray_address = (ase_uint16*)new_image->dat; for (i=0; igetEntry(c)); g = _rgba_getg(palette->getEntry(c)); b = _rgba_getb(palette->getEntry(c)); g = 255 * Hsv(Rgb(r, g, b)).valueInt() / 100; *gray_address = _graya(g, 255); } idx_address++; gray_address++; } break; } break; } return new_image; } /* Based on Gary Oberbrunner: */ /*---------------------------------------------------------------------- * Color image quantizer, from Paul Heckbert's paper in * Computer Graphics, vol.16 #3, July 1982 (Siggraph proceedings), * pp. 297-304. * By Gary Oberbrunner, copyright c. 1988. *---------------------------------------------------------------------- */ /* Bayer-method ordered dither. The array line[] contains the * intensity values for the line being processed. As you can see, the * ordered dither is much simpler than the error dispersion dither. * It is also many times faster, but it is not as accurate and * produces cross-hatch * patterns on the output. */ static int pattern[8][8] = { { 0, 32, 8, 40, 2, 34, 10, 42 }, /* 8x8 Bayer ordered dithering */ { 48, 16, 56, 24, 50, 18, 58, 26 }, /* pattern. Each input pixel */ { 12, 44, 4, 36, 14, 46, 6, 38 }, /* is scaled to the 0..63 range */ { 60, 28, 52, 20, 62, 30, 54, 22 }, /* before looking in this table */ { 3, 35, 11, 43, 1, 33, 9, 41 }, /* to determine the action. */ { 51, 19, 59, 27, 49, 17, 57, 25 }, { 15, 47, 7, 39, 13, 45, 5, 37 }, { 63, 31, 55, 23, 61, 29, 53, 21 } }; #define DIST(r1,g1,b1,r2,g2,b2) (3 * ((r1)-(r2)) * ((r1)-(r2)) + \ 4 * ((g1)-(g2)) * ((g1)-(g2)) + \ 2 * ((b1)-(b2)) * ((b1)-(b2))) static Image* ordered_dithering(const Image* src_image, int offsetx, int offsety, const RgbMap* rgbmap, const Palette* palette) { int oppr, oppg, oppb, oppnrcm; Image *dst_image; int dither_const; int nr, ng, nb; int r, g, b, a; int nearestcm; int c, x, y; dst_image = image_new(IMAGE_INDEXED, src_image->w, src_image->h); if (!dst_image) return NULL; for (y=0; yh; y++) { for (x=0; xw; x++) { c = image_getpixel_fast(src_image, x, y); r = _rgba_getr(c); g = _rgba_getg(c); b = _rgba_getb(c); a = _rgba_geta(c); if (a != 0) { nearestcm = rgbmap->mapColor(r, g, b); /* rgb values for nearest color */ nr = _rgba_getr(palette->getEntry(nearestcm)); ng = _rgba_getg(palette->getEntry(nearestcm)); nb = _rgba_getb(palette->getEntry(nearestcm)); /* Color as far from rgb as nrngnb but in the other direction */ oppr = MID(0, 2*r - nr, 255); oppg = MID(0, 2*g - ng, 255); oppb = MID(0, 2*b - nb, 255); /* Nearest match for opposite color: */ 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. Since the most nearly opposite color is not necessarily on the line from the nearest through the true color, some triangulation error can be introduced. In the worst case the r-nr distance can actually be less than the nr-oppr distance. */ if (oppnrcm != nearestcm) { oppr = _rgba_getr(palette->getEntry(oppnrcm)); oppg = _rgba_getg(palette->getEntry(oppnrcm)); oppb = _rgba_getb(palette->getEntry(oppnrcm)); dither_const = DIST(nr, ng, nb, oppr, oppg, oppb); if (dither_const != 0) { dither_const = 64 * DIST(r, g, b, nr, ng, nb) / dither_const; dither_const = MIN(63, dither_const); if (pattern[(x+offsetx) & 7][(y+offsety) & 7] < dither_const) nearestcm = oppnrcm; } } } else nearestcm = 0; image_putpixel_fast(dst_image, x, y, nearestcm); } } return dst_image; } ////////////////////////////////////////////////////////////////////// // Creation of optimized palette for RGB images // by David Capello static void create_palette_from_bitmaps(const std::vector& images, Palette* palette, bool has_background_layer) { quantization::ColorHistogram<5, 6, 5> histogram; ase_uint32 color; RgbTraits::address_t address; // If the sprite has a background layer, the first entry can be // used, in other case the 0 indexed will be the mask color, so it // will not be used later in the color conversion (from RGB to // Indexed). int first_usable_entry = (has_background_layer ? 0: 1); for (int i=0; i<(int)images.size(); ++i) { const Image* image = images[i]; for (int y=0; yh; ++y) { address = image_address_fast(image, 0, y); for (int x=0; xw; ++x) { color = *address; if (_rgba_geta(color) > 0) { color |= _rgba(0, 0, 0, 255); histogram.addSamples(color, 1); } ++address; } } } int used_colors = histogram.createOptimizedPalette(palette, first_usable_entry, 255); //palette->resize(first_usable_entry+used_colors); // TODO }