diff options
-rw-r--r-- | src/FbTk/ImageControl.cc | 184 |
1 files changed, 83 insertions, 101 deletions
diff --git a/src/FbTk/ImageControl.cc b/src/FbTk/ImageControl.cc index 2ceb106..783f7a7 100644 --- a/src/FbTk/ImageControl.cc +++ b/src/FbTk/ImageControl.cc | |||
@@ -22,7 +22,7 @@ | |||
22 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 22 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
23 | // DEALINGS IN THE SOFTWARE. | 23 | // DEALINGS IN THE SOFTWARE. |
24 | 24 | ||
25 | // $Id: ImageControl.cc,v 1.6 2003/10/09 16:48:09 rathnor Exp $ | 25 | // $Id: ImageControl.cc,v 1.7 2004/01/02 13:28:00 fluxgen Exp $ |
26 | 26 | ||
27 | #include "ImageControl.hh" | 27 | #include "ImageControl.hh" |
28 | 28 | ||
@@ -85,7 +85,7 @@ inline unsigned long bsqrt(unsigned long x) { | |||
85 | }; // end anonymous namespace | 85 | }; // end anonymous namespace |
86 | 86 | ||
87 | ImageControl::ImageControl(int screen_num, bool dither, | 87 | ImageControl::ImageControl(int screen_num, bool dither, |
88 | int cpc, unsigned long cache_timeout, unsigned long cmax): | 88 | int cpc, unsigned long cache_timeout, unsigned long cmax): |
89 | m_dither(dither), | 89 | m_dither(dither), |
90 | m_colors(0), | 90 | m_colors(0), |
91 | m_num_colors(0), | 91 | m_num_colors(0), |
@@ -150,8 +150,9 @@ ImageControl::~ImageControl() { | |||
150 | 150 | ||
151 | 151 | ||
152 | Pixmap ImageControl::searchCache(unsigned int width, unsigned int height, | 152 | Pixmap ImageControl::searchCache(unsigned int width, unsigned int height, |
153 | unsigned long texture_type, | 153 | unsigned long texture_type, |
154 | const FbTk::Color &color, const FbTk::Color &color_to) const { | 154 | const FbTk::Color &color, const FbTk::Color &color_to) const { |
155 | |||
155 | Cache tmp; | 156 | Cache tmp; |
156 | tmp.width = width; | 157 | tmp.width = width; |
157 | tmp.height = height; | 158 | tmp.height = height; |
@@ -165,38 +166,19 @@ Pixmap ImageControl::searchCache(unsigned int width, unsigned int height, | |||
165 | (*it)->count++; | 166 | (*it)->count++; |
166 | return (*it)->pixmap; | 167 | return (*it)->pixmap; |
167 | } | 168 | } |
168 | 169 | ||
169 | /* | ||
170 | CacheList::iterator it = cache.begin(); | ||
171 | CacheList::iterator it_end = cache.end(); | ||
172 | for (; it != it_end; ++it) { | ||
173 | if (((*it)->width == width) && | ||
174 | ((*it)->height == height) && | ||
175 | ((*it)->texture == texture_type) && | ||
176 | ((*it)->pixel1 == color.pixel())) { | ||
177 | if (texture_type & FbTk::Texture::GRADIENT) { | ||
178 | if ((*it)->pixel2 == color_to.pixel()) { | ||
179 | (*it)->count++; | ||
180 | return (*it)->pixmap; | ||
181 | } | ||
182 | } else { | ||
183 | (*it)->count++; | ||
184 | return (*it)->pixmap; | ||
185 | } | ||
186 | } | ||
187 | } | ||
188 | return None; | 170 | return None; |
189 | */ | 171 | |
190 | } | 172 | } |
191 | 173 | ||
192 | 174 | ||
193 | Pixmap ImageControl::renderImage(unsigned int width, unsigned int height, | 175 | Pixmap ImageControl::renderImage(unsigned int width, unsigned int height, |
194 | const FbTk::Texture &texture) { | 176 | const FbTk::Texture &texture) { |
195 | 177 | ||
196 | if (texture.type() & FbTk::Texture::PARENTRELATIVE) | 178 | if (texture.type() & FbTk::Texture::PARENTRELATIVE) |
197 | return ParentRelative; | 179 | return ParentRelative; |
198 | 180 | ||
199 | // search cache first | 181 | // search cache first |
200 | Pixmap pixmap = searchCache(width, height, texture.type(), | 182 | Pixmap pixmap = searchCache(width, height, texture.type(), |
201 | texture.color(), texture.colorTo()); | 183 | texture.color(), texture.colorTo()); |
202 | if (pixmap) | 184 | if (pixmap) |
@@ -260,9 +242,9 @@ void ImageControl::removeImage(Pixmap pixmap) { | |||
260 | 242 | ||
261 | 243 | ||
262 | void ImageControl::colorTables(const unsigned char **rmt, const unsigned char **gmt, | 244 | void ImageControl::colorTables(const unsigned char **rmt, const unsigned char **gmt, |
263 | const unsigned char **bmt, | 245 | const unsigned char **bmt, |
264 | int *roff, int *goff, int *boff, | 246 | int *roff, int *goff, int *boff, |
265 | int *rbit, int *gbit, int *bbit) const { | 247 | int *rbit, int *gbit, int *bbit) const { |
266 | 248 | ||
267 | if (rmt) *rmt = red_color_table; | 249 | if (rmt) *rmt = red_color_table; |
268 | if (gmt) *gmt = green_color_table; | 250 | if (gmt) *gmt = green_color_table; |
@@ -285,9 +267,9 @@ void ImageControl::getXColorTable(XColor **c, int *n) { | |||
285 | 267 | ||
286 | 268 | ||
287 | void ImageControl::getGradientBuffers(unsigned int w, | 269 | void ImageControl::getGradientBuffers(unsigned int w, |
288 | unsigned int h, | 270 | unsigned int h, |
289 | unsigned int **xbuf, | 271 | unsigned int **xbuf, |
290 | unsigned int **ybuf) { | 272 | unsigned int **ybuf) { |
291 | 273 | ||
292 | if (w > grad_buffer_width) { | 274 | if (w > grad_buffer_width) { |
293 | if (grad_xbuffer) { | 275 | if (grad_xbuffer) { |
@@ -434,7 +416,7 @@ void ImageControl::createColorTable() { | |||
434 | } | 416 | } |
435 | } | 417 | } |
436 | 418 | ||
437 | break; | 419 | break; |
438 | 420 | ||
439 | case PseudoColor: | 421 | case PseudoColor: |
440 | case StaticColor: { | 422 | case StaticColor: { |
@@ -448,7 +430,7 @@ void ImageControl::createColorTable() { | |||
448 | 430 | ||
449 | if (m_colors_per_channel < 2 || m_num_colors > static_cast<unsigned int>(1 << m_screen_depth)) { | 431 | if (m_colors_per_channel < 2 || m_num_colors > static_cast<unsigned int>(1 << m_screen_depth)) { |
450 | fprintf(stderr, "ImageControl::ImageControl: invalid colormap size %d " | 432 | fprintf(stderr, "ImageControl::ImageControl: invalid colormap size %d " |
451 | "(%d/%d/%d) - reducing", | 433 | "(%d/%d/%d) - reducing", |
452 | m_num_colors, m_colors_per_channel, m_colors_per_channel, | 434 | m_num_colors, m_colors_per_channel, m_colors_per_channel, |
453 | m_colors_per_channel); | 435 | m_colors_per_channel); |
454 | 436 | ||
@@ -533,97 +515,97 @@ void ImageControl::createColorTable() { | |||
533 | 515 | ||
534 | case GrayScale: | 516 | case GrayScale: |
535 | case StaticGray: | 517 | case StaticGray: |
536 | { | 518 | { |
537 | |||
538 | if (visual()->c_class == StaticGray) { | ||
539 | m_num_colors = 1 << m_screen_depth; | ||
540 | } else { | ||
541 | m_num_colors = m_colors_per_channel * m_colors_per_channel * m_colors_per_channel; | ||
542 | 519 | ||
543 | if (m_num_colors > static_cast<unsigned int>(1 << m_screen_depth)) { | 520 | if (visual()->c_class == StaticGray) { |
544 | m_colors_per_channel = (1 << m_screen_depth) / 3; | 521 | m_num_colors = 1 << m_screen_depth; |
522 | } else { | ||
545 | m_num_colors = m_colors_per_channel * m_colors_per_channel * m_colors_per_channel; | 523 | m_num_colors = m_colors_per_channel * m_colors_per_channel * m_colors_per_channel; |
524 | |||
525 | if (m_num_colors > static_cast<unsigned int>(1 << m_screen_depth)) { | ||
526 | m_colors_per_channel = (1 << m_screen_depth) / 3; | ||
527 | m_num_colors = m_colors_per_channel * m_colors_per_channel * m_colors_per_channel; | ||
528 | } | ||
546 | } | 529 | } |
547 | } | ||
548 | 530 | ||
549 | if (m_colors_per_channel < 2 || m_num_colors > static_cast<unsigned int>(1 << m_screen_depth)) { | 531 | if (m_colors_per_channel < 2 || m_num_colors > static_cast<unsigned int>(1 << m_screen_depth)) { |
550 | fprintf(stderr,"FbTk::ImageControl: invalid colormap size %d " | 532 | fprintf(stderr,"FbTk::ImageControl: invalid colormap size %d " |
551 | "(%d/%d/%d) - reducing", | 533 | "(%d/%d/%d) - reducing", |
552 | m_num_colors, m_colors_per_channel, m_colors_per_channel, | 534 | m_num_colors, m_colors_per_channel, m_colors_per_channel, |
553 | m_colors_per_channel); | 535 | m_colors_per_channel); |
554 | 536 | ||
555 | m_colors_per_channel = (1 << m_screen_depth) / 3; | 537 | m_colors_per_channel = (1 << m_screen_depth) / 3; |
556 | } | 538 | } |
557 | 539 | ||
558 | m_colors = new XColor[m_num_colors]; | 540 | m_colors = new XColor[m_num_colors]; |
559 | 541 | ||
560 | int p, bits = 255 / (m_colors_per_channel - 1); | 542 | int p, bits = 255 / (m_colors_per_channel - 1); |
561 | red_bits = green_bits = blue_bits = bits; | 543 | red_bits = green_bits = blue_bits = bits; |
562 | 544 | ||
563 | for (unsigned int i = 0; i < 256; i++) | 545 | for (unsigned int i = 0; i < 256; i++) |
564 | red_color_table[i] = green_color_table[i] = blue_color_table[i] = | 546 | red_color_table[i] = green_color_table[i] = blue_color_table[i] = |
565 | i / bits; | 547 | i / bits; |
566 | 548 | ||
567 | for (unsigned int i = 0; i < m_num_colors; i++) { | 549 | for (unsigned int i = 0; i < m_num_colors; i++) { |
568 | m_colors[i].red = (i * 0xffff) / (m_colors_per_channel - 1); | 550 | m_colors[i].red = (i * 0xffff) / (m_colors_per_channel - 1); |
569 | m_colors[i].green = (i * 0xffff) / (m_colors_per_channel - 1); | 551 | m_colors[i].green = (i * 0xffff) / (m_colors_per_channel - 1); |
570 | m_colors[i].blue = (i * 0xffff) / (m_colors_per_channel - 1);; | 552 | m_colors[i].blue = (i * 0xffff) / (m_colors_per_channel - 1);; |
571 | m_colors[i].flags = DoRed|DoGreen|DoBlue; | ||
572 | |||
573 | if (! XAllocColor(disp, m_colormap, | ||
574 | &m_colors[i])) { | ||
575 | fprintf(stderr, "Couldn't alloc color %i %i %i\n", | ||
576 | m_colors[i].red, m_colors[i].green, m_colors[i].blue); | ||
577 | m_colors[i].flags = 0; | ||
578 | } else | ||
579 | m_colors[i].flags = DoRed|DoGreen|DoBlue; | 553 | m_colors[i].flags = DoRed|DoGreen|DoBlue; |
580 | } | ||
581 | 554 | ||
555 | if (! XAllocColor(disp, m_colormap, | ||
556 | &m_colors[i])) { | ||
557 | fprintf(stderr, "Couldn't alloc color %i %i %i\n", | ||
558 | m_colors[i].red, m_colors[i].green, m_colors[i].blue); | ||
559 | m_colors[i].flags = 0; | ||
560 | } else | ||
561 | m_colors[i].flags = DoRed|DoGreen|DoBlue; | ||
562 | } | ||
582 | 563 | ||
583 | XColor icolors[256]; | ||
584 | unsigned int incolors = (((1 << m_screen_depth) > 256) ? 256 : | ||
585 | (1 << m_screen_depth)); | ||
586 | 564 | ||
587 | for (unsigned int i = 0; i < incolors; i++) | 565 | XColor icolors[256]; |
588 | icolors[i].pixel = i; | 566 | unsigned int incolors = (((1 << m_screen_depth) > 256) ? 256 : |
567 | (1 << m_screen_depth)); | ||
589 | 568 | ||
590 | XQueryColors(disp, m_colormap, icolors, incolors); | 569 | for (unsigned int i = 0; i < incolors; i++) |
591 | for (unsigned int i = 0; i < m_num_colors; i++) { | 570 | icolors[i].pixel = i; |
592 | if (! m_colors[i].flags) { | ||
593 | unsigned long chk = 0xffffffff, pixel, close = 0; | ||
594 | 571 | ||
595 | p = 2; | 572 | XQueryColors(disp, m_colormap, icolors, incolors); |
596 | while (p--) { | 573 | for (unsigned int i = 0; i < m_num_colors; i++) { |
597 | for (unsigned int ii = 0; ii < incolors; ii++) { | 574 | if (! m_colors[i].flags) { |
598 | int r = (m_colors[i].red - icolors[i].red) >> 8; | 575 | unsigned long chk = 0xffffffff, pixel, close = 0; |
599 | int g = (m_colors[i].green - icolors[i].green) >> 8; | ||
600 | int b = (m_colors[i].blue - icolors[i].blue) >> 8; | ||
601 | pixel = (r * r) + (g * g) + (b * b); | ||
602 | 576 | ||
603 | if (pixel < chk) { | 577 | p = 2; |
604 | chk = pixel; | 578 | while (p--) { |
605 | close = ii; | 579 | for (unsigned int ii = 0; ii < incolors; ii++) { |
606 | } | 580 | int r = (m_colors[i].red - icolors[i].red) >> 8; |
581 | int g = (m_colors[i].green - icolors[i].green) >> 8; | ||
582 | int b = (m_colors[i].blue - icolors[i].blue) >> 8; | ||
583 | pixel = (r * r) + (g * g) + (b * b); | ||
607 | 584 | ||
608 | m_colors[i].red = icolors[close].red; | 585 | if (pixel < chk) { |
609 | m_colors[i].green = icolors[close].green; | 586 | chk = pixel; |
610 | m_colors[i].blue = icolors[close].blue; | 587 | close = ii; |
588 | } | ||
611 | 589 | ||
612 | if (XAllocColor(disp, m_colormap, &m_colors[i])) { | 590 | m_colors[i].red = icolors[close].red; |
613 | m_colors[i].flags = DoRed|DoGreen|DoBlue; | 591 | m_colors[i].green = icolors[close].green; |
614 | break; | 592 | m_colors[i].blue = icolors[close].blue; |
593 | |||
594 | if (XAllocColor(disp, m_colormap, &m_colors[i])) { | ||
595 | m_colors[i].flags = DoRed|DoGreen|DoBlue; | ||
596 | break; | ||
597 | } | ||
615 | } | 598 | } |
616 | } | 599 | } |
617 | } | 600 | } |
618 | } | 601 | } |
619 | } | ||
620 | 602 | ||
621 | break; | 603 | break; |
622 | } | 604 | } |
623 | 605 | ||
624 | default: | 606 | default: |
625 | cerr<<"FbTk::ImageControl: Unsupported visual"<<endl; | 607 | cerr<<"FbTk::ImageControl: Unsupported visual"<<endl; |
626 | break; | 608 | break; |
627 | } | 609 | } |
628 | } | 610 | } |
629 | 611 | ||