546
546
assert(ncolor > 0);
548
IndexedMap *newmap = 0;
548
550
pool<Ocnode> pool;
552
tree = octreeBuild(&pool, rgbmap, ncolor);
554
catch (std::bad_alloc& ex) {
555
return NULL; //should do smthg else?
558
RGB *rgbpal = new RGB[ncolor];
560
octreeIndex(tree, rgbpal, &indexes);
562
octreeDelete(&pool, tree);
564
// stacking with increasing contrasts
565
qsort((void *)rgbpal, indexes, sizeof(RGB), compRGB);
568
IndexedMap *newmap = IndexedMapCreate(rgbmap->width, rgbmap->height);
569
if (!newmap) { delete rgbpal; return NULL; }
571
// fill in the color lookup table
572
for (int i = 0; i < indexes; i++) newmap->clut[i] = rgbpal[i];
573
newmap->nrColors = indexes;
575
// fill in new map pixels
576
for (int y = 0; y < rgbmap->height; y++)
578
for (int x = 0; x < rgbmap->width; x++)
580
RGB rgb = rgbmap->getPixel(rgbmap, x, y);
581
int index = findRGB(rgbpal, ncolor, rgb);
582
newmap->setPixel(newmap, x, y, index);
554
tree = octreeBuild(&pool, rgbmap, ncolor);
556
catch (std::bad_alloc &ex) {
557
//should do smthg else?
561
RGB *rgbpal = new RGB[ncolor];
563
octreeIndex(tree, rgbpal, &indexes);
565
octreeDelete(&pool, tree);
567
// stacking with increasing contrasts
568
qsort((void *)rgbpal, indexes, sizeof(RGB), compRGB);
571
newmap = IndexedMapCreate(rgbmap->width, rgbmap->height);
573
// fill in the color lookup table
574
for (int i = 0; i < indexes; i++) {
575
newmap->clut[i] = rgbpal[i];
577
newmap->nrColors = indexes;
579
// fill in new map pixels
580
for (int y = 0; y < rgbmap->height; y++) {
581
for (int x = 0; x < rgbmap->width; x++) {
582
RGB rgb = rgbmap->getPixel(rgbmap, x, y);
583
int index = findRGB(rgbpal, ncolor, rgb);
584
newmap->setPixel(newmap, x, y, index);