Dillo v3.1.1-98-g318d1f14
Loading...
Searching...
No Matches
fltkimgbuf.cc
Go to the documentation of this file.
1/*
2 * Dillo Widget
3 *
4 * Copyright 2005-2007, 2012-2013 Sebastian Geerken <sgeerken@dillo.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "fltkcore.hh"
21#include "../lout/msg.h"
22#include "../lout/misc.hh"
23
24#include <FL/fl_draw.H>
25#include <math.h>
26
27#define IMAGE_MAX_AREA (6000 * 6000)
28
29#define MAX_WIDTH 0x8000
30#define MAX_HEIGHT 0x8000
31
32namespace dw {
33namespace fltk {
34
35using namespace lout::container::typed;
36
39
40Vector <FltkImgbuf::GammaCorrectionTable> *FltkImgbuf::gammaCorrectionTables
41 = new Vector <FltkImgbuf::GammaCorrectionTable> (true, 2);
42
44{
45 // Since the number of possible keys is low, a linear search is
46 // sufficiently fast.
47
48 for (int i = 0; i < gammaCorrectionTables->size(); i++) {
50 if (gct->gamma == gamma)
51 return gct->map;
52 }
53
54 _MSG("Creating new table for gamma = %g\n", gamma);
55
57 gct->gamma = gamma;
58
59 for (int i = 0; i < 256; i++)
60 gct->map[i] = 255 * pow((double)i / 255, gamma);
61
62 gammaCorrectionTables->put (gct);
63 return gct->map;
64}
65
66bool FltkImgbuf::excessiveImageDimensions (int width, int height)
67{
68 return width <= 0 || height <= 0 ||
70}
71
73{
74 _MSG("Deleting gammaCorrectionTables\n");
77}
78
79FltkImgbuf::FltkImgbuf (Type type, int width, int height, double gamma)
80{
81 DBG_OBJ_CREATE ("dw::fltk::FltkImgbuf");
82
83 _MSG ("FltkImgbuf::FltkImgbuf: new root %p\n", this);
84 init (type, width, height, gamma, NULL);
85}
86
87FltkImgbuf::FltkImgbuf (Type type, int width, int height, double gamma,
88 FltkImgbuf *root)
89{
90 DBG_OBJ_CREATE ("dw::fltk::FltkImgbuf");
91
92 _MSG ("FltkImgbuf::FltkImgbuf: new scaled %p, root is %p\n", this, root);
94}
95
96void FltkImgbuf::init (Type type, int width, int height, double gamma,
97 FltkImgbuf *root)
98{
100 // Excessive image sizes which would cause crashes due to too
101 // big allocations for the image buffer (for root buffers, when
102 // the image was specially prepared). In this case we use a 1 x
103 // 1 size.
104 MSG("FltkImgbuf::init: suspicious image size request %d x %d\n",
105 width, height);
106 init (type, 1, 1, gamma, root);
107 } else if (width > MAX_WIDTH) {
108 // Too large dimensions cause dangerous overflow errors, so we
109 // limit dimensions to harmless values.
110 //
111 // Example: 65535 * 65536 / 65536 (see scaling below) results in
112 // the negative value -1.
113
114 MSG("FltkImgbuf::init: cannot handle large width %d\n", width);
116 } else if (height > MAX_HEIGHT) {
117 MSG("FltkImgbuf::init: cannot handle large height %d\n", height);
119 } else if (gamma <= 0) {
120 MSG("FltkImgbuf::init: non-positive gamma %g\n", gamma);
121 init (type, width, height, 1, root);
122 } else {
123 this->root = root;
124 this->type = type;
125 this->width = width;
126 this->height = height;
127 this->gamma = gamma;
128
129 DBG_OBJ_SET_NUM ("width", width);
130 DBG_OBJ_SET_NUM ("height", height);
131
132 // TODO: Maybe this is only for root buffers
133 switch (type) {
134 case RGBA: bpp = 4; break;
135 case RGB: bpp = 3; break;
136 default: bpp = 1; break;
137 }
138 _MSG("FltkImgbuf::init this=%p width=%d height=%d bpp=%d gamma=%g\n",
139 this, width, height, bpp, gamma);
140 rawdata = new uchar[bpp * width * height];
141 // Set light-gray as interim background color.
142 memset(rawdata, 222, width*height*bpp);
143
144 refCount = 1;
145 deleteOnUnref = true;
147
151 DBG_OBJ_SET_SYM ("copiedRows", sb.getChars ());
152 }
153
154 // The list is only used for root buffers.
155 if (isRoot())
156 scaledBuffers = new lout::container::typed::List <FltkImgbuf> (true);
157 else
158 scaledBuffers = NULL;
159
160 if (!isRoot()) {
161 // Scaling
162 for (int row = 0; row < root->height; row++) {
163 if (root->copiedRows->get (row))
164 scaleRow (row, root->rawdata + row*root->width*root->bpp);
165 }
166 }
167 }
168}
169
171{
172 _MSG ("FltkImgbuf::~FltkImgbuf\n");
173
174 if (!isRoot())
175 root->detachScaledBuf (this);
176
177 delete[] rawdata;
178 delete copiedRows;
179
180 if (scaledBuffers)
181 delete scaledBuffers;
182
184}
185
191{
192 scaledBuffers->detachRef (scaledBuf);
193
194 _MSG("FltkImgbuf[root %p]: scaled buffer %p is detached, %d left\n",
195 this, scaledBuf, scaledBuffers->size ());
196
197 if (refCount == 0 && scaledBuffers->isEmpty () && deleteOnUnref)
198 // If the root buffer is not used anymore, but this is the last scaled
199 // buffer.
200 // See also: FltkImgbuf::unref().
201 delete this;
202}
203
204void FltkImgbuf::setCMap (int *colors, int num_colors)
205{
206}
207
208inline void FltkImgbuf::scaleRow (int row, const core::byte *data)
209{
210 if (row < root->height) {
211 if (scaleMode == SIMPLE)
212 scaleRowSimple (row, data);
213 else
214 scaleRowBeautiful (row, data);
215 }
216}
217
218inline void FltkImgbuf::scaleRowSimple (int row, const core::byte *data)
219{
220 int sr1 = scaledY (row);
221 int sr2 = scaledY (row + 1);
222
223 for (int sr = sr1; sr < sr2; sr++) {
224 // Avoid multiple passes.
225 if (copiedRows->get(sr)) continue;
226
227 copiedRows->set (sr, true);
228
232 DBG_OBJ_SET_SYM ("copiedRows", sb.getChars ());
233 }
234
235 if (sr == sr1) {
236 for (int px = 0; px < root->width; px++) {
237 int px1 = px * width / root->width;
238 int px2 = (px+1) * width / root->width;
239 for (int sp = px1; sp < px2; sp++) {
240 memcpy(rawdata + (sr*width + sp)*bpp, data + px*bpp, bpp);
241 }
242 }
243 } else {
244 memcpy(rawdata + sr*width*bpp, rawdata + sr1*width*bpp, width*bpp);
245 }
246 }
247}
248
249inline void FltkImgbuf::scaleRowBeautiful (int row, const core::byte *data)
250{
251 int sr1 = scaledY (row);
252 int sr2 = scaledY (row + 1);
253 bool allRootRows = false;
254
255 // Don't rescale rows!
256 if (copiedRows->get(sr1)) return;
257
258 if (height > root->height) {
259 scaleBuffer (data, root->width, 1,
260 rawdata + sr1 * width * bpp, width, sr2 - sr1,
261 bpp, gamma);
262 // Mark scaled rows done
263 for (int sr = sr1; sr < sr2 || sr == sr1; sr++)
264 copiedRows->set (sr, true);
265
269 DBG_OBJ_SET_SYM ("copiedRows", sb.getChars ());
270 }
271 } else {
272 assert (sr1 == sr2 || sr1 + 1 == sr2);
273 int row1 = backscaledY(sr1), row2 = backscaledY(sr1 + 1);
274
275 // Check all the necessary root lines already arrived,
276 // a larger area than a single row may be accessed here.
277 for (int r=row1; (allRootRows=root->copiedRows->get(r)) && ++r < row2; );
278 if (allRootRows) {
279 scaleBuffer (root->rawdata + row1 * root->width * bpp,
280 root->width, row2 - row1,
281 rawdata + sr1 * width * bpp, width, 1,
282 bpp, gamma);
283 // Mark scaled row done
284 copiedRows->set (sr1, true);
285
289 DBG_OBJ_SET_SYM ("copiedRows", sb.getChars ());
290 }
291 }
292 }
293}
294
315inline void FltkImgbuf::scaleBuffer (const core::byte *src, int srcWidth,
316 int srcHeight, core::byte *dest,
317 int destWidth, int destHeight, int bpp,
318 double gamma)
319{
320 uchar *gammaMap1, *gammaMap2;
321
322 if (scaleMode == BEAUTIFUL_GAMMA) {
323 gammaMap1 = findGammaCorrectionTable (gamma);
324 gammaMap2 = findGammaCorrectionTable (1 / gamma);
325 }
326
327 int *v = new int[bpp];
328 for(int x = 0; x < destWidth; x++) {
329 for(int y = 0; y < destHeight; y++) {
330 int xo1 = x * srcWidth / destWidth;
331 int xo2 = lout::misc::max ((x + 1) * srcWidth / destWidth, xo1 + 1);
332 int yo1 = y * srcHeight / destHeight;
333 int yo2 = lout::misc::max ((y + 1) * srcHeight / destHeight, yo1 + 1);
334 int n = (xo2 - xo1) * (yo2 - yo1);
335
336 for(int i = 0; i < bpp; i++)
337 v[i] = 0;
338
339 for(int xo = xo1; xo < xo2; xo++)
340 for(int yo = yo1; yo < yo2; yo++) {
341 const core::byte *ps = src + bpp * (yo * srcWidth + xo);
342 for(int i = 0; i < bpp; i++)
343 v[i] +=
344 (scaleMode == BEAUTIFUL_GAMMA ? gammaMap2[ps[i]] : ps[i]);
345 }
346
347 core::byte *pd = dest + bpp * (y * destWidth + x);
348 for(int i = 0; i < bpp; i++)
349 pd[i] =
350 scaleMode == BEAUTIFUL_GAMMA ? gammaMap1[v[i] / n] : v[i] / n;
351 }
352 }
353 delete[] v;
354}
355
356void FltkImgbuf::copyRow (int row, const core::byte *data)
357{
358 assert (isRoot());
359
360 if (row < height) {
361 // Flag the row done and copy its data.
362 copiedRows->set (row, true);
363
367 DBG_OBJ_SET_SYM ("copiedRows", sb.getChars ());
368 }
369
370 memcpy(rawdata + row * width * bpp, data, width * bpp);
371
372 // Update all the scaled buffers of this root image.
373 for (Iterator <FltkImgbuf> it = scaledBuffers->iterator();
374 it.hasNext(); ) {
375 FltkImgbuf *sb = it.getNext ();
376 sb->scaleRow (row, data);
377 }
378 }
379}
380
382{
383 if (isRoot()) {
384 for (Iterator<FltkImgbuf> it = scaledBuffers->iterator(); it.hasNext();){
385 FltkImgbuf *sb = it.getNext ();
386 sb->copiedRows->clear();
387
391 DBG_OBJ_SET_SYM ("copiedRows", sb.getChars ());
392 }
393 }
394 }
395}
396
398{
399 if (!isRoot())
400 return root->getScaledBuf (width, height);
401
402 if (width > MAX_WIDTH) {
403 // Similar to init.
404 MSG("FltkImgbuf::getScaledBuf: cannot handle large width %d\n", width);
405 return getScaledBuf (MAX_WIDTH, height);
406 }
407 if (height > MAX_HEIGHT) {
408 MSG("FltkImgbuf::getScaledBuf: cannot handle large height %d\n", height);
409 return getScaledBuf (width, MAX_HEIGHT);
410 }
411
412 if (width == this->width && height == this->height) {
413 ref ();
414 return this;
415 }
416
417 for (Iterator <FltkImgbuf> it = scaledBuffers->iterator(); it.hasNext(); ) {
418 FltkImgbuf *sb = it.getNext ();
419 if (sb->width == width && sb->height == height) {
420 sb->ref ();
421 return sb;
422 }
423 }
424
425 // Check for excessive image sizes which would cause crashes due to
426 // too big allocations for the image buffer. In this case we return
427 // a pointer to the unscaled image buffer.
429 MSG("FltkImgbuf::getScaledBuf: suspicious image size request %d x %d\n",
430 width, height);
431 ref ();
432 return this;
433 }
434
435 // This size is not yet used, so a new buffer has to be created.
436 FltkImgbuf *sb = new FltkImgbuf (type, width, height, gamma, this);
437 scaledBuffers->append (sb);
439
440 return sb;
441}
442
444{
445 // TODO: May have to be adjusted.
446
447 if (isRoot()) {
448 /* root buffer */
449 area->x = 0;
450 area->y = row;
451 area->width = width;
452 area->height = 1;
453 _MSG("::getRowArea: area x=%d y=%d width=%d height=%d\n",
454 area->x, area->y, area->width, area->height);
455 } else {
456 if (row > root->height)
457 area->x = area->y = area->width = area->height = 0;
458 else {
459 // scaled buffer
460 int sr1 = scaledY (row);
461 int sr2 = scaledY (row + 1);
462
463 area->x = 0;
464 area->y = sr1;
465 area->width = width;
466 area->height = sr2 - sr1;
467 _MSG("::getRowArea: area x=%d y=%d width=%d height=%d\n",
468 area->x, area->y, area->width, area->height);
469 }
470 }
471}
472
474{
475 return root ? root->width : width;
476}
477
479{
480 return root ? root->height : height;
481}
482
484{
485 return new FltkImgbuf (type, width, height, gamma);
486}
487
488void FltkImgbuf::copyTo (Imgbuf *dest, int xDestRoot, int yDestRoot,
489 int xSrc, int ySrc, int widthSrc, int heightSrc)
490{
491 FltkImgbuf *fDest = (FltkImgbuf*)dest;
492 assert (bpp == fDest->bpp);
493
494 int xSrc2 = lout::misc::min (xSrc + widthSrc, fDest->width - xDestRoot);
495 int ySrc2 = lout::misc::min (ySrc + heightSrc, fDest->height - yDestRoot);
496
497 //printf ("copying from (%d, %d), %d x %d to (%d, %d) (root) => "
498 // "xSrc2 = %d, ySrc2 = %d\n",
499 // xSrc, ySrc, widthSrc, heightSrc, xDestRoot, yDestRoot,
500 // xSrc2, ySrc2);
501
502 for (int x = xSrc; x < xSrc2; x++)
503 for (int y = ySrc; y < ySrc2; y++) {
504 int iSrc = x + width * y;
505 int iDest = xDestRoot + x + fDest->width * (yDestRoot + y);
506
507 //printf (" (%d, %d): %d -> %d\n", x, y, iSrc, iDest);
508
509 for (int b = 0; b < bpp; b++)
510 fDest->rawdata[bpp * iDest + b] = rawdata[bpp * iSrc + b];
511 }
512}
513
515{
516 refCount++;
517
518 //if (root)
519 // MSG("FltkImgbuf[scaled %p, root is %p]: ref() => %d\n",
520 // this, root, refCount);
521 //else
522 // MSG("FltkImgbuf[root %p]: ref() => %d\n", this, refCount);
523}
524
526{
527 //if (root)
528 // MSG("FltkImgbuf[scaled %p, root is %p]: ref() => %d\n",
529 // this, root, refCount - 1);
530 //else
531 // MSG("FltkImgbuf[root %p]: ref() => %d\n", this, refCount - 1);
532
533 if (--refCount == 0) {
534 if (isRoot ()) {
535 // Root buffer, it must be ensured that no scaled buffers are left.
536 // See also FltkImgbuf::detachScaledBuf().
537 if (scaledBuffers->isEmpty () && deleteOnUnref) {
538 delete this;
539 } else {
540 _MSG("FltkImgbuf[root %p]: not deleted. numScaled=%d\n",
541 this, scaledBuffers->size ());
542 }
543 } else
544 // Scaled buffer buffer, simply delete it.
545 delete this;
546 }
547}
548
550{
551 return refCount == 1 &&
552 (scaledBuffers == NULL || scaledBuffers->isEmpty ());
553}
554
555void FltkImgbuf::setDeleteOnUnref (bool deleteOnUnref)
556{
557 assert (isRoot ());
558 this->deleteOnUnref = deleteOnUnref;
559}
560
562{
563 return refCount != 0 ||
564 (scaledBuffers != NULL && !scaledBuffers->isEmpty ());
565}
566
567
569{
570 // TODO: May have to be adjusted.
571 assert (root != NULL);
572 return ySrc * height / root->height;
573}
574
576{
577 assert (root != NULL);
578
579 // Notice that rounding errors because of integers do not play a
580 // role. This method cannot be the exact inverse of scaledY, since
581 // scaleY is not bijective, and so not invertible. Instead, both
582 // values always return the smallest value.
583 return yScaled * root->height / height;
584}
585
586void FltkImgbuf::draw (Fl_Widget *target, int xRoot, int yRoot,
587 int x, int y, int width, int height)
588{
589 // TODO: Clarify the question, whether "target" is the current widget
590 // (and so has not to be passed at all).
591
592 _MSG("::draw: xRoot=%d x=%d yRoot=%d y=%d width=%d height=%d\n"
593 " this->width=%d this->height=%d\n",
594 xRoot, x, yRoot, y, width, height, this->width, this->height);
595
596 if (x > this->width || y > this->height) {
597 return;
598 }
599
600 if (x + width > this->width) {
601 width = this->width - x;
602 }
603
604 if (y + height > this->height) {
605 height = this->height - y;
606 }
607
608 fl_draw_image(rawdata+bpp*(y*this->width + x), xRoot + x, yRoot + y, width,
609 height, bpp, this->width * bpp);
610
611}
612
613} // namespace fltk
614} // namespace dw
#define _MSG(...)
Definition bookmarks.c:45
#define MSG(...)
Definition bookmarks.c:46
The platform independent interface for image buffers.
Definition imgbuf.hh:162
dw::core::Shape implemtation for simple rectangles.
Definition types.hh:70
void setCMap(int *colors, int num_colors)
void getRowArea(int row, dw::core::Rectangle *area)
FltkImgbuf * root
Definition fltkimgbuf.hh:21
FltkImgbuf(Type type, int width, int height, double gamma, FltkImgbuf *root)
Definition fltkimgbuf.cc:87
void draw(Fl_Widget *target, int xRoot, int yRoot, int x, int y, int width, int height)
void copyTo(Imgbuf *dest, int xDestRoot, int yDestRoot, int xSrc, int ySrc, int widthSrc, int heightSrc)
static uchar * findGammaCorrectionTable(double gamma)
Definition fltkimgbuf.cc:43
static bool excessiveImageDimensions(int width, int height)
Definition fltkimgbuf.cc:66
void copyRow(int row, const core::byte *data)
void setDeleteOnUnref(bool deleteOnUnref)
static void scaleBuffer(const core::byte *src, int srcWidth, int srcHeight, core::byte *dest, int destWidth, int destHeight, int bpp, double gamma)
General method to scale an image buffer.
void scaleRow(int row, const core::byte *data)
core::Imgbuf * createSimilarBuf(int width, int height)
Creates an image buffer with same parameters (type, gamma etc.) except size.
core::Imgbuf * getScaledBuf(int width, int height)
static lout::container::typed::Vector< GammaCorrectionTable > * gammaCorrectionTables
Definition fltkimgbuf.hh:40
void detachScaledBuf(FltkImgbuf *scaledBuf)
This method is called for the root buffer, when a scaled buffer removed.
lout::container::typed::List< FltkImgbuf > * scaledBuffers
Definition fltkimgbuf.hh:24
void scaleRowBeautiful(int row, const core::byte *data)
static void freeall()
Definition fltkimgbuf.cc:72
int backscaledY(int yScaled)
void scaleRowSimple(int row, const core::byte *data)
lout::misc::BitSet * copiedRows
Definition fltkimgbuf.hh:37
int scaledY(int ySrc)
void init(Type type, int width, int height, double gamma, FltkImgbuf *root)
Definition fltkimgbuf.cc:96
Typed version of container::untyped::Iterator.
Definition container.hh:395
A bit set, which automatically reallocates when needed.
Definition misc.hh:609
void intoStringBuffer(misc::StringBuffer *sb)
Definition misc.cc:141
void set(int i, bool val)
Definition misc.cc:157
bool get(int i) const
Definition misc.cc:149
A class for fast concatenation of a large number of strings.
Definition misc.hh:570
const char * getChars()
Return a NUL-terminated strings containing all appended strings.
Definition misc.cc:86
#define DBG_IF_RTFL
Definition debug.hh:73
#define DBG_OBJ_DELETE()
#define DBG_OBJ_CREATE(klass)
#define DBG_OBJ_SET_SYM(var, val)
#define DBG_OBJ_SET_NUM(var, val)
#define DBG_OBJ_ASSOC_CHILD(child)
#define MAX_WIDTH
Definition fltkimgbuf.cc:29
#define MAX_HEIGHT
Definition fltkimgbuf.cc:30
#define IMAGE_MAX_AREA
Definition fltkimgbuf.cc:27
unsigned char byte
Definition core.hh:25
@ BEAUTIFUL_GAMMA
Definition fltkimgbuf.cc:37
enum dw::fltk::ScaleMode scaleMode
Dw is in this namespace, or sub namespaces of this one.
This namespace provides thin wrappers, implemented as C++ templates, to gain type-safety.
Definition container.hh:387
T min(T a, T b)
Definition misc.hh:40
T max(T a, T b)
Definition misc.hh:41