src/FbTk/ImageControl.cc (raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 |
// ImageControl.cc for FbTk - Fluxbox Toolkit // Copyright (c) 2001 - 2006 Henrik Kinnunen (fluxbox at users.sourceforge.net) // // Image.cc for Blackbox - an X11 Window manager // Copyright (c) 1997 - 2000 Brad Hughes (bhughes at tcac.net) // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. // $Id$ #include "ImageControl.hh" #include "TextureRender.hh" #include "App.hh" #include "SimpleCommand.hh" #include "I18n.hh" //use GNU extensions #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif // _GNU_SOURCE #ifdef HAVE_CONFIG_H #include "config.h" #endif // HAVE_CONFIG_H #ifdef HAVE_SYS_TYPES_H #include <sys/types.h> #endif // HAVE_SYS_TYPES_H #ifdef HAVE_CSTDLIB #include <cstdlib> #else #include <stdlib.h> #endif #ifdef HAVE_CSTRING #include <cstring> #else #include <string.h> #endif #ifdef HAVE_CSTDIO #include <cstdio> #else #include <stdio.h> #endif #ifdef HAVE_CTYPE_H #include <ctype.h> #endif // HAVE_CTYPE_H #include <iostream> using std::cerr; using std::endl; using std::list; namespace FbTk { // lookup table for texture unsigned long *ImageControl::sqrt_table = 0; #ifdef TIMEDCACHE bool ImageControl::s_timed_cache = true; #else bool ImageControl::s_timed_cache = false; #endif // TIMEDCACHE namespace { // anonymous inline unsigned long bsqrt(unsigned long x) { if (x <= 0) return 0; if (x == 1) return 1; unsigned long r = x >> 1; unsigned long q; while (1) { q = x / r; if (q >= r) return r; r = (r + q) >> 1; } } }; // end anonymous namespace ImageControl::ImageControl(int screen_num, bool dither, int cpc, unsigned long cache_timeout, unsigned long cmax): m_dither(dither), m_colors(0), m_num_colors(0), m_colors_per_channel(cpc) { Display *disp = FbTk::App::instance()->display(); m_screen_depth = DefaultDepth(disp, screen_num); m_screen_num = screen_num; m_root_window = RootWindow(disp, screen_num); m_visual = DefaultVisual(disp, screen_num); m_colormap = DefaultColormap(disp, screen_num); cache_max = cmax; if (cache_timeout && s_timed_cache) { m_timer.setTimeout(cache_timeout); RefCount<Command> clean_cache(new SimpleCommand<ImageControl>(*this, &ImageControl::cleanCache)); m_timer.setCommand(clean_cache); m_timer.start(); } createColorTable(); } ImageControl::~ImageControl() { if (sqrt_table) { delete [] sqrt_table; sqrt_table = 0; } if (grad_xbuffer) { delete [] grad_xbuffer; } if (grad_ybuffer) { delete [] grad_ybuffer; } Display *disp = FbTk::App::instance()->display(); if (m_colors) { unsigned long *pixels = new unsigned long [m_num_colors]; for (unsigned int color = 0; color < m_num_colors; color++) *(pixels + color) = (*(m_colors + color)).pixel; XFreeColors(disp, m_colormap, pixels, m_num_colors, 0); delete [] m_colors; } if (cache.size() > 0) { CacheList::iterator it = cache.begin(); CacheList::iterator it_end = cache.end(); for (; it != it_end; ++it) { XFreePixmap(disp, (*it)->pixmap); delete (*it); } } } Pixmap ImageControl::searchCache(unsigned int width, unsigned int height, const Texture &text, FbTk::Orientation orient) const { if (text.pixmap().drawable() != None) { // do comparsion with width/height and texture_pixmap CacheList::iterator it = cache.begin(); CacheList::iterator it_end = cache.end(); for (; it != it_end; ++it) { if ((*it)->texture_pixmap == text.pixmap().drawable() && (*it)->orient == orient && (*it)->width == width && (*it)->height == height && (*it)->texture == text.type()) { (*it)->count++; return (*it)->pixmap; } } return None; } /* Cache tmp; tmp.texture_pixmap = text.pixmap().drawable(); tmp.width = width; tmp.height = height; tmp.texture = text.type(); tmp.pixel1 = text.color().pixel(); tmp.pixel2 = text.colorTo().pixel(); */ CacheList::iterator it = cache.begin(); CacheList::iterator it_end = cache.end(); for (; it != it_end; ++it) { if (((*it)->width == width) && ((*it)->orient == orient) && ((*it)->height == height) && ((*it)->texture == text.type()) && ((*it)->pixel1 == text.color().pixel())) { if (text.type() & FbTk::Texture::GRADIENT) { if ((*it)->pixel2 == text.colorTo().pixel()) { (*it)->count++; return (*it)->pixmap; } } else { (*it)->count++; return (*it)->pixmap; } } } return None; } Pixmap ImageControl::renderImage(unsigned int width, unsigned int height, const FbTk::Texture &texture, FbTk::Orientation orient) { if (texture.type() & FbTk::Texture::PARENTRELATIVE) return ParentRelative; // search cache first Pixmap pixmap = searchCache(width, height, texture, orient); if (pixmap) { return pixmap; // return cache item } // render new image TextureRender image(*this, width, height, orient, m_colors, m_num_colors); pixmap = image.render(texture); if (pixmap) { // create new cache item and add it to cache list Cache *tmp = new Cache; tmp->pixmap = pixmap; tmp->texture_pixmap = texture.pixmap().drawable(); tmp->orient = orient; tmp->width = width; tmp->height = height; tmp->count = 1; tmp->texture = texture.type(); tmp->pixel1 = texture.color().pixel(); if (texture.type() & FbTk::Texture::GRADIENT) tmp->pixel2 = texture.colorTo().pixel(); else tmp->pixel2 = 0l; cache.push_back(tmp); if (cache.size() > cache_max) cleanCache(); return pixmap; } return None; } void ImageControl::removeImage(Pixmap pixmap) { if (!pixmap) return; CacheList::iterator it = cache.begin(); CacheList::iterator it_end = cache.end(); for (; it != it_end; ++it) { if ((*it)->pixmap == pixmap) { if ((*it)->count) { (*it)->count--; if (s_timed_cache) { cleanCache(); return; } } if ((*it)->count <= 0) cleanCache(); return; } } } void ImageControl::colorTables(const unsigned char **rmt, const unsigned char **gmt, const unsigned char **bmt, int *roff, int *goff, int *boff, int *rbit, int *gbit, int *bbit) const { if (rmt) *rmt = red_color_table; if (gmt) *gmt = green_color_table; if (bmt) *bmt = blue_color_table; if (roff) *roff = red_offset; if (goff) *goff = green_offset; if (boff) *boff = blue_offset; if (rbit) *rbit = red_bits; if (gbit) *gbit = green_bits; if (bbit) *bbit = blue_bits; } #ifdef NOT_USED void ImageControl::getXColorTable(XColor **c, int *n) { if (c) *c = m_colors; if (n) *n = m_num_colors; } #endif void ImageControl::getGradientBuffers(unsigned int w, unsigned int h, unsigned int **xbuf, unsigned int **ybuf) { if (w > grad_buffer_width) { if (grad_xbuffer) { delete [] grad_xbuffer; } grad_buffer_width = w; grad_xbuffer = new unsigned int[grad_buffer_width * 3]; } if (h > grad_buffer_height) { if (grad_ybuffer) { delete [] grad_ybuffer; } grad_buffer_height = h; grad_ybuffer = new unsigned int[grad_buffer_height * 3]; } *xbuf = grad_xbuffer; *ybuf = grad_ybuffer; } void ImageControl::installRootColormap() { Display *disp = FbTk::App::instance()->display(); XGrabServer(disp); bool install = true; int i = 0, ncmap = 0; Colormap *cmaps = XListInstalledColormaps(disp, m_root_window, &ncmap); if (cmaps) { for (i = 0; i < ncmap; i++) { if (*(cmaps + i) == m_colormap) install = false; } if (install) XInstallColormap(disp, m_colormap); XFree(cmaps); } XUngrabServer(disp); } #ifdef NOT_USED void ImageControl::setColorsPerChannel(int cpc) { if (cpc < 2) cpc = 2; if (cpc > 6) cpc = 6; m_colors_per_channel = cpc; } #endif unsigned long ImageControl::getSqrt(unsigned int x) const { if (! sqrt_table) { // build sqrt table for use with elliptic gradient sqrt_table = new unsigned long[256 * 256 * 2 + 1]; int i = 0; for (; i < (256 * 256 * 2); i++) sqrt_table[i] = bsqrt(i); } return sqrt_table[x]; } void ImageControl::cleanCache() { Display *disp = FbTk::App::instance()->display(); list<CacheList::iterator> deadlist; CacheList::iterator it = cache.begin(); CacheList::iterator it_end = cache.end(); for (; it != it_end; ++it) { Cache *tmp = (*it); if (tmp->count <= 0) { XFreePixmap(disp, tmp->pixmap); deadlist.push_back(it); delete tmp; tmp=0; } } list<CacheList::iterator>::iterator dead_it = deadlist.begin(); list<CacheList::iterator>::iterator dead_it_end = deadlist.end(); for (; dead_it != dead_it_end; ++dead_it) { cache.erase(*dead_it); } } void ImageControl::createColorTable() { Display *disp = FbTk::App::instance()->display(); grad_xbuffer = grad_ybuffer = (unsigned int *) 0; grad_buffer_width = grad_buffer_height = 0; int count; XPixmapFormatValues *pmv = XListPixmapFormats(disp, &count); if (pmv) { bits_per_pixel = 0; for (int i = 0; i < count; i++) { if (pmv[i].depth == m_screen_depth) { bits_per_pixel = pmv[i].bits_per_pixel; break; } } XFree(pmv); } if (bits_per_pixel == 0) bits_per_pixel = m_screen_depth; if (bits_per_pixel >= 24) setDither(false); red_offset = green_offset = blue_offset = 0; switch (visual()->c_class) { case TrueColor: { int i; // compute color tables unsigned long red_mask = visual()->red_mask, green_mask = visual()->green_mask, blue_mask = visual()->blue_mask; while (! (red_mask & 1)) { red_offset++; red_mask >>= 1; } while (! (green_mask & 1)) { green_offset++; green_mask >>= 1; } while (! (blue_mask & 1)) { blue_offset++; blue_mask >>= 1; } red_bits = 255 / red_mask; green_bits = 255 / green_mask; blue_bits = 255 / blue_mask; for (i = 0; i < 256; i++) { red_color_table[i] = i / red_bits; green_color_table[i] = i / green_bits; blue_color_table[i] = i / blue_bits; } } break; case PseudoColor: case StaticColor: { m_num_colors = m_colors_per_channel * m_colors_per_channel * m_colors_per_channel; if (m_num_colors > static_cast<unsigned int>(1 << m_screen_depth)) { m_colors_per_channel = (1 << m_screen_depth) / 3; m_num_colors = m_colors_per_channel * m_colors_per_channel * m_colors_per_channel; } if (m_colors_per_channel < 2 || m_num_colors > static_cast<unsigned int>(1 << m_screen_depth)) { fprintf(stderr, "ImageControl::ImageControl: invalid colormap size %d " "(%d/%d/%d) - reducing", m_num_colors, m_colors_per_channel, m_colors_per_channel, m_colors_per_channel); m_colors_per_channel = (1 << m_screen_depth) / 3; } m_colors = new XColor[m_num_colors]; int bits = 256 / m_colors_per_channel; #ifndef ORDEREDPSEUDO bits = 255 / (m_colors_per_channel - 1); #endif // ORDEREDPSEUDO red_bits = green_bits = blue_bits = bits; for (unsigned int i = 0; i < 256; i++) { red_color_table[i] = green_color_table[i] = blue_color_table[i] = i / bits; } for (int r = 0, i = 0; r < m_colors_per_channel; r++) { for (int g = 0; g < m_colors_per_channel; g++) { for (int b = 0; b < m_colors_per_channel; b++, i++) { m_colors[i].red = (r * 0xffff) / (m_colors_per_channel - 1); m_colors[i].green = (g * 0xffff) / (m_colors_per_channel - 1); m_colors[i].blue = (b * 0xffff) / (m_colors_per_channel - 1);; m_colors[i].flags = DoRed|DoGreen|DoBlue; } } } for (unsigned int i = 0; i < m_num_colors; i++) { if (! XAllocColor(disp, m_colormap, &m_colors[i])) { fprintf(stderr, "couldn't alloc color %i %i %i\n", m_colors[i].red, m_colors[i].green, m_colors[i].blue); m_colors[i].flags = 0; } else m_colors[i].flags = DoRed|DoGreen|DoBlue; } XColor icolors[256]; unsigned int incolors = (((1 << m_screen_depth) > 256) ? 256 : (1 << m_screen_depth)); for (unsigned int i = 0; i < incolors; i++) icolors[i].pixel = i; XQueryColors(disp, m_colormap, icolors, incolors); for (unsigned int i = 0; i < m_num_colors; i++) { if (! m_colors[i].flags) { unsigned long chk = 0xffffffff, pixel, close = 0; char p = 2; while (p--) { for (unsigned int ii = 0; ii < incolors; ii++) { int r = (m_colors[i].red - icolors[i].red) >> 8; int g = (m_colors[i].green - icolors[i].green) >> 8; int b = (m_colors[i].blue - icolors[i].blue) >> 8; pixel = (r * r) + (g * g) + (b * b); if (pixel < chk) { chk = pixel; close = ii; } m_colors[i].red = icolors[close].red; m_colors[i].green = icolors[close].green; m_colors[i].blue = icolors[close].blue; if (XAllocColor(disp, m_colormap, &m_colors[i])) { m_colors[i].flags = DoRed|DoGreen|DoBlue; break; } } } } } break; } case GrayScale: case StaticGray: { if (visual()->c_class == StaticGray) { m_num_colors = 1 << m_screen_depth; } else { m_num_colors = m_colors_per_channel * m_colors_per_channel * m_colors_per_channel; if (m_num_colors > static_cast<unsigned int>(1 << m_screen_depth)) { m_colors_per_channel = (1 << m_screen_depth) / 3; m_num_colors = m_colors_per_channel * m_colors_per_channel * m_colors_per_channel; } } if (m_colors_per_channel < 2 || m_num_colors > static_cast<unsigned int>(1 << m_screen_depth)) { fprintf(stderr,"FbTk::ImageControl: invalid colormap size %d " "(%d/%d/%d) - reducing", m_num_colors, m_colors_per_channel, m_colors_per_channel, m_colors_per_channel); m_colors_per_channel = (1 << m_screen_depth) / 3; } m_colors = new XColor[m_num_colors]; int p, bits = 255 / (m_colors_per_channel - 1); red_bits = green_bits = blue_bits = bits; for (unsigned int i = 0; i < 256; i++) red_color_table[i] = green_color_table[i] = blue_color_table[i] = i / bits; for (unsigned int i = 0; i < m_num_colors; i++) { m_colors[i].red = (i * 0xffff) / (m_colors_per_channel - 1); m_colors[i].green = (i * 0xffff) / (m_colors_per_channel - 1); m_colors[i].blue = (i * 0xffff) / (m_colors_per_channel - 1);; m_colors[i].flags = DoRed|DoGreen|DoBlue; if (! XAllocColor(disp, m_colormap, &m_colors[i])) { fprintf(stderr, "Couldn't alloc color %i %i %i\n", m_colors[i].red, m_colors[i].green, m_colors[i].blue); m_colors[i].flags = 0; } else m_colors[i].flags = DoRed|DoGreen|DoBlue; } XColor icolors[256]; unsigned int incolors = (((1 << m_screen_depth) > 256) ? 256 : (1 << m_screen_depth)); for (unsigned int i = 0; i < incolors; i++) icolors[i].pixel = i; XQueryColors(disp, m_colormap, icolors, incolors); for (unsigned int i = 0; i < m_num_colors; i++) { if (! m_colors[i].flags) { unsigned long chk = 0xffffffff, pixel, close = 0; p = 2; while (p--) { for (unsigned int ii = 0; ii < incolors; ii++) { int r = (m_colors[i].red - icolors[i].red) >> 8; int g = (m_colors[i].green - icolors[i].green) >> 8; int b = (m_colors[i].blue - icolors[i].blue) >> 8; pixel = (r * r) + (g * g) + (b * b); if (pixel < chk) { chk = pixel; close = ii; } m_colors[i].red = icolors[close].red; m_colors[i].green = icolors[close].green; m_colors[i].blue = icolors[close].blue; if (XAllocColor(disp, m_colormap, &m_colors[i])) { m_colors[i].flags = DoRed|DoGreen|DoBlue; break; } } } } } break; } default: _FB_USES_NLS; cerr<<"FbTk::ImageControl: "<<_FBTK_CONSOLETEXT(Error, UnsupportedVisual, "Unsupported visual", "A visual is a technical term in X")<<endl; break; } } }; // end namespace FbTk |