Dillo v3.1.1-11-g8f0909b7
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 for(int x = 0; x < destWidth; x++)
328 for(int y = 0; y < destHeight; y++) {
329 int xo1 = x * srcWidth / destWidth;
330 int xo2 = lout::misc::max ((x + 1) * srcWidth / destWidth, xo1 + 1);
331 int yo1 = y * srcHeight / destHeight;
332 int yo2 = lout::misc::max ((y + 1) * srcHeight / destHeight, yo1 + 1);
333 int n = (xo2 - xo1) * (yo2 - yo1);
334
335 int v[bpp];
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
354void FltkImgbuf::copyRow (int row, const core::byte *data)
355{
356 assert (isRoot());
357
358 if (row < height) {
359 // Flag the row done and copy its data.
360 copiedRows->set (row, true);
361
365 DBG_OBJ_SET_SYM ("copiedRows", sb.getChars ());
366 }
367
368 memcpy(rawdata + row * width * bpp, data, width * bpp);
369
370 // Update all the scaled buffers of this root image.
371 for (Iterator <FltkImgbuf> it = scaledBuffers->iterator();
372 it.hasNext(); ) {
373 FltkImgbuf *sb = it.getNext ();
374 sb->scaleRow (row, data);
375 }
376 }
377}
378
380{
381 if (isRoot()) {
382 for (Iterator<FltkImgbuf> it = scaledBuffers->iterator(); it.hasNext();){
383 FltkImgbuf *sb = it.getNext ();
384 sb->copiedRows->clear();
385
389 DBG_OBJ_SET_SYM ("copiedRows", sb.getChars ());
390 }
391 }
392 }
393}
394
396{
397 if (!isRoot())
398 return root->getScaledBuf (width, height);
399
400 if (width > MAX_WIDTH) {
401 // Similar to init.
402 MSG("FltkImgbuf::getScaledBuf: cannot handle large width %d\n", width);
403 return getScaledBuf (MAX_WIDTH, height);
404 }
405 if (height > MAX_HEIGHT) {
406 MSG("FltkImgbuf::getScaledBuf: cannot handle large height %d\n", height);
407 return getScaledBuf (width, MAX_HEIGHT);
408 }
409
410 if (width == this->width && height == this->height) {
411 ref ();
412 return this;
413 }
414
415 for (Iterator <FltkImgbuf> it = scaledBuffers->iterator(); it.hasNext(); ) {
416 FltkImgbuf *sb = it.getNext ();
417 if (sb->width == width && sb->height == height) {
418 sb->ref ();
419 return sb;
420 }
421 }
422
423 // Check for excessive image sizes which would cause crashes due to
424 // too big allocations for the image buffer. In this case we return
425 // a pointer to the unscaled image buffer.
427 MSG("FltkImgbuf::getScaledBuf: suspicious image size request %d x %d\n",
428 width, height);
429 ref ();
430 return this;
431 }
432
433 // This size is not yet used, so a new buffer has to be created.
434 FltkImgbuf *sb = new FltkImgbuf (type, width, height, gamma, this);
435 scaledBuffers->append (sb);
437
438 return sb;
439}
440
442{
443 // TODO: May have to be adjusted.
444
445 if (isRoot()) {
446 /* root buffer */
447 area->x = 0;
448 area->y = row;
449 area->width = width;
450 area->height = 1;
451 _MSG("::getRowArea: area x=%d y=%d width=%d height=%d\n",
452 area->x, area->y, area->width, area->height);
453 } else {
454 if (row > root->height)
455 area->x = area->y = area->width = area->height = 0;
456 else {
457 // scaled buffer
458 int sr1 = scaledY (row);
459 int sr2 = scaledY (row + 1);
460
461 area->x = 0;
462 area->y = sr1;
463 area->width = width;
464 area->height = sr2 - sr1;
465 _MSG("::getRowArea: area x=%d y=%d width=%d height=%d\n",
466 area->x, area->y, area->width, area->height);
467 }
468 }
469}
470
472{
473 return root ? root->width : width;
474}
475
477{
478 return root ? root->height : height;
479}
480
482{
483 return new FltkImgbuf (type, width, height, gamma);
484}
485
486void FltkImgbuf::copyTo (Imgbuf *dest, int xDestRoot, int yDestRoot,
487 int xSrc, int ySrc, int widthSrc, int heightSrc)
488{
489 FltkImgbuf *fDest = (FltkImgbuf*)dest;
490 assert (bpp == fDest->bpp);
491
492 int xSrc2 = lout::misc::min (xSrc + widthSrc, fDest->width - xDestRoot);
493 int ySrc2 = lout::misc::min (ySrc + heightSrc, fDest->height - yDestRoot);
494
495 //printf ("copying from (%d, %d), %d x %d to (%d, %d) (root) => "
496 // "xSrc2 = %d, ySrc2 = %d\n",
497 // xSrc, ySrc, widthSrc, heightSrc, xDestRoot, yDestRoot,
498 // xSrc2, ySrc2);
499
500 for (int x = xSrc; x < xSrc2; x++)
501 for (int y = ySrc; y < ySrc2; y++) {
502 int iSrc = x + width * y;
503 int iDest = xDestRoot + x + fDest->width * (yDestRoot + y);
504
505 //printf (" (%d, %d): %d -> %d\n", x, y, iSrc, iDest);
506
507 for (int b = 0; b < bpp; b++)
508 fDest->rawdata[bpp * iDest + b] = rawdata[bpp * iSrc + b];
509 }
510}
511
513{
514 refCount++;
515
516 //if (root)
517 // MSG("FltkImgbuf[scaled %p, root is %p]: ref() => %d\n",
518 // this, root, refCount);
519 //else
520 // MSG("FltkImgbuf[root %p]: ref() => %d\n", this, refCount);
521}
522
524{
525 //if (root)
526 // MSG("FltkImgbuf[scaled %p, root is %p]: ref() => %d\n",
527 // this, root, refCount - 1);
528 //else
529 // MSG("FltkImgbuf[root %p]: ref() => %d\n", this, refCount - 1);
530
531 if (--refCount == 0) {
532 if (isRoot ()) {
533 // Root buffer, it must be ensured that no scaled buffers are left.
534 // See also FltkImgbuf::detachScaledBuf().
535 if (scaledBuffers->isEmpty () && deleteOnUnref) {
536 delete this;
537 } else {
538 _MSG("FltkImgbuf[root %p]: not deleted. numScaled=%d\n",
539 this, scaledBuffers->size ());
540 }
541 } else
542 // Scaled buffer buffer, simply delete it.
543 delete this;
544 }
545}
546
548{
549 return refCount == 1 &&
550 (scaledBuffers == NULL || scaledBuffers->isEmpty ());
551}
552
553void FltkImgbuf::setDeleteOnUnref (bool deleteOnUnref)
554{
555 assert (isRoot ());
556 this->deleteOnUnref = deleteOnUnref;
557}
558
560{
561 return refCount != 0 ||
562 (scaledBuffers != NULL && !scaledBuffers->isEmpty ());
563}
564
565
567{
568 // TODO: May have to be adjusted.
569 assert (root != NULL);
570 return ySrc * height / root->height;
571}
572
574{
575 assert (root != NULL);
576
577 // Notice that rounding errors because of integers do not play a
578 // role. This method cannot be the exact inverse of scaledY, since
579 // scaleY is not bijective, and so not invertible. Instead, both
580 // values always return the smallest value.
581 return yScaled * root->height / height;
582}
583
584void FltkImgbuf::draw (Fl_Widget *target, int xRoot, int yRoot,
585 int x, int y, int width, int height)
586{
587 // TODO: Clarify the question, whether "target" is the current widget
588 // (and so has not to be passed at all).
589
590 _MSG("::draw: xRoot=%d x=%d yRoot=%d y=%d width=%d height=%d\n"
591 " this->width=%d this->height=%d\n",
592 xRoot, x, yRoot, y, width, height, this->width, this->height);
593
594 if (x > this->width || y > this->height) {
595 return;
596 }
597
598 if (x + width > this->width) {
599 width = this->width - x;
600 }
601
602 if (y + height > this->height) {
603 height = this->height - y;
604 }
605
606 fl_draw_image(rawdata+bpp*(y*this->width + x), xRoot + x, yRoot + y, width,
607 height, bpp, this->width * bpp);
608
609}
610
611} // namespace fltk
612} // 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:605
void intoStringBuffer(misc::StringBuffer *sb)
Definition misc.cc:147
void set(int i, bool val)
Definition misc.cc:163
bool get(int i) const
Definition misc.cc:155
A class for fast concatenation of a large number of strings.
Definition misc.hh:566
const char * getChars()
Return a NUL-terminated strings containing all appended strings.
Definition misc.cc:92
#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:19
T max(T a, T b)
Definition misc.hh:20