Codebase list dillo / upstream/latest dw / fltkimgbuf.cc
upstream/latest

Tree @upstream/latest (Download .tar.gz)

fltkimgbuf.cc @upstream/latestraw · history · blame

  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
/*
 * Dillo Widget
 *
 * Copyright 2005-2007, 2012-2013 Sebastian Geerken <sgeerken@dillo.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "fltkcore.hh"
#include "../lout/msg.h"
#include "../lout/misc.hh"

#include <FL/fl_draw.H>
#include <math.h>

#define IMAGE_MAX_AREA (6000 * 6000)

#define MAX_WIDTH 0x8000
#define MAX_HEIGHT 0x8000

namespace dw {
namespace fltk {

using namespace lout::container::typed;

const enum ScaleMode { SIMPLE, BEAUTIFUL, BEAUTIFUL_GAMMA }
   scaleMode = BEAUTIFUL_GAMMA;

Vector <FltkImgbuf::GammaCorrectionTable> *FltkImgbuf::gammaCorrectionTables
   = new Vector <FltkImgbuf::GammaCorrectionTable> (true, 2);

uchar *FltkImgbuf::findGammaCorrectionTable (double gamma)
{
   // Since the number of possible keys is low, a linear search is
   // sufficiently fast.

   for (int i = 0; i < gammaCorrectionTables->size(); i++) {
      GammaCorrectionTable *gct = gammaCorrectionTables->get(i);
      if (gct->gamma == gamma)
         return gct->map;
   }

   _MSG("Creating new table for gamma = %g\n", gamma);

   GammaCorrectionTable *gct = new GammaCorrectionTable();
   gct->gamma = gamma;

   for (int i = 0; i < 256; i++)
      gct->map[i] = 255 * pow((double)i / 255, gamma);

   gammaCorrectionTables->put (gct);
   return gct->map;
}

bool FltkImgbuf::excessiveImageDimensions (int width, int height)
{
   return width <= 0 || height <= 0 ||
      width > IMAGE_MAX_AREA / height;
}

void FltkImgbuf::freeall ()
{
   _MSG("Deleting gammaCorrectionTables\n");
   delete gammaCorrectionTables;
   gammaCorrectionTables = NULL;
}

FltkImgbuf::FltkImgbuf (Type type, int width, int height, double gamma)
{
   DBG_OBJ_CREATE ("dw::fltk::FltkImgbuf");

   _MSG ("FltkImgbuf::FltkImgbuf: new root %p\n", this);
   init (type, width, height, gamma, NULL);
}

FltkImgbuf::FltkImgbuf (Type type, int width, int height, double gamma,
                        FltkImgbuf *root)
{
   DBG_OBJ_CREATE ("dw::fltk::FltkImgbuf");

   _MSG ("FltkImgbuf::FltkImgbuf: new scaled %p, root is %p\n", this, root);
   init (type, width, height, gamma, root);
}

void FltkImgbuf::init (Type type, int width, int height, double gamma,
                       FltkImgbuf *root)
{
   if (excessiveImageDimensions (width, height)) {
      // Excessive image sizes which would cause crashes due to too
      // big allocations for the image buffer (for root buffers, when
      // the image was specially prepared). In this case we use a 1 x
      // 1 size.
      MSG("FltkImgbuf::init: suspicious image size request %d x %d\n",
          width, height);
      init (type, 1, 1, gamma, root);
   } else if (width > MAX_WIDTH) {
      // Too large dimensions cause dangerous overflow errors, so we
      // limit dimensions to harmless values.
      //
      // Example: 65535 * 65536 / 65536 (see scaling below) results in
      // the negative value -1.

      MSG("FltkImgbuf::init: cannot handle large width %d\n", width);
      init (type, MAX_WIDTH, height, gamma, root);
   } else if (height > MAX_HEIGHT) {
      MSG("FltkImgbuf::init: cannot handle large height %d\n", height);
      init (type, width, MAX_HEIGHT, gamma, root);
   } else if (gamma <= 0) {
      MSG("FltkImgbuf::init: non-positive gamma %g\n", gamma);
      init (type, width, height, 1, root);
   } else {
      this->root = root;
      this->type = type;
      this->width = width;
      this->height = height;
      this->gamma = gamma;

      DBG_OBJ_SET_NUM ("width", width);
      DBG_OBJ_SET_NUM ("height", height);

      // TODO: Maybe this is only for root buffers
      switch (type) {
      case RGBA: bpp = 4; break;
      case RGB:  bpp = 3; break;
      default:   bpp = 1; break;
      }
      _MSG("FltkImgbuf::init this=%p width=%d height=%d bpp=%d gamma=%g\n",
           this, width, height, bpp, gamma);
      rawdata = new uchar[bpp * width * height];
      // Set light-gray as interim background color.
      memset(rawdata, 222, width*height*bpp);

      refCount = 1;
      deleteOnUnref = true;
      copiedRows = new lout::misc::BitSet (height);

      // The list is only used for root buffers.
      if (isRoot())
         scaledBuffers = new lout::container::typed::List <FltkImgbuf> (true);
      else
         scaledBuffers = NULL;

      if (!isRoot()) {
         // Scaling
         for (int row = 0; row < root->height; row++) {
            if (root->copiedRows->get (row))
               scaleRow (row, root->rawdata + row*root->width*root->bpp);
         }
      }
   }
}

FltkImgbuf::~FltkImgbuf ()
{
   _MSG ("FltkImgbuf::~FltkImgbuf\n");

   if (!isRoot())
      root->detachScaledBuf (this);

   delete[] rawdata;
   delete copiedRows;

   if (scaledBuffers)
      delete scaledBuffers;

   DBG_OBJ_DELETE ();
}

/**
 * \brief This method is called for the root buffer, when a scaled buffer
 *    removed.
 */
void FltkImgbuf::detachScaledBuf (FltkImgbuf *scaledBuf)
{
   scaledBuffers->detachRef (scaledBuf);

   _MSG("FltkImgbuf[root %p]: scaled buffer %p is detached, %d left\n",
        this, scaledBuf, scaledBuffers->size ());

   if (refCount == 0 && scaledBuffers->isEmpty () && deleteOnUnref)
      // If the root buffer is not used anymore, but this is the last scaled
      // buffer.
      // See also: FltkImgbuf::unref().
      delete this;
}

void FltkImgbuf::setCMap (int *colors, int num_colors)
{
}

inline void FltkImgbuf::scaleRow (int row, const core::byte *data)
{
   if (row < root->height) {
      if (scaleMode == SIMPLE)
         scaleRowSimple (row, data);
      else
         scaleRowBeautiful (row, data);
   }
}

inline void FltkImgbuf::scaleRowSimple (int row, const core::byte *data)
{
   int sr1 = scaledY (row);
   int sr2 = scaledY (row + 1);

   for (int sr = sr1; sr < sr2; sr++) {
      // Avoid multiple passes.
      if (copiedRows->get(sr)) continue;

      copiedRows->set (sr, true);
      if (sr == sr1) {
         for (int px = 0; px < root->width; px++) {
            int px1 = px * width / root->width;
            int px2 = (px+1) * width / root->width;
            for (int sp = px1; sp < px2; sp++) {
               memcpy(rawdata + (sr*width + sp)*bpp, data + px*bpp, bpp);
            }
         }
      } else {
         memcpy(rawdata + sr*width*bpp, rawdata + sr1*width*bpp, width*bpp);
      }
   }
}

inline void FltkImgbuf::scaleRowBeautiful (int row, const core::byte *data)
{
   int sr1 = scaledY (row);
   int sr2 = scaledY (row + 1);
   bool allRootRows = false;

   // Don't rescale rows!
   if (copiedRows->get(sr1)) return;

   if (height > root->height) {
      scaleBuffer (data, root->width, 1,
                   rawdata + sr1 * width * bpp, width, sr2 - sr1,
                   bpp, gamma);
      // Mark scaled rows done
      for (int sr = sr1; sr < sr2 || sr == sr1; sr++)
         copiedRows->set (sr, true);
   } else {
      assert (sr1 == sr2 || sr1 + 1 == sr2);
      int row1 = backscaledY(sr1), row2 = backscaledY(sr1 + 1);

      // Check all the necessary root lines already arrived,
      // a larger area than a single row may be accessed here.
      for (int r=row1; (allRootRows=root->copiedRows->get(r)) && ++r < row2; );
      if (allRootRows) {
         scaleBuffer (root->rawdata + row1 * root->width * bpp,
                      root->width, row2 - row1,
                      rawdata + sr1 * width * bpp, width, 1,
                      bpp, gamma);
         // Mark scaled row done
         copiedRows->set (sr1, true);
      }
   }
}

/**
 * General method to scale an image buffer. Used to scale single lines
 * in scaleRowBeautiful.
 *
 * The algorithm is rather simple. If the scaled buffer is smaller
 * (both width and height) than the original buffer, each pixel in the
 * scaled buffer is assigned a rectangle of pixels in the original
 * buffer; the resulting pixel value (red, green, blue) is simply the
 * average of all pixel values. This is pretty fast and leads to
 * rather good results.
 *
 * Nothing special (like interpolation) is done when scaling up.
 *
 * If scaleMode is set to BEAUTIFUL_GAMMA, gamma correction is
 * considered, see <http://www.4p8.com/eric.brasseur/gamma.html>.
 *
 * TODO Could be optimized as in scaleRowSimple: when the destination
 * image is larger, calculate only one row/column, and copy it to the
 * other rows/columns.
 */
inline void FltkImgbuf::scaleBuffer (const core::byte *src, int srcWidth,
                                     int srcHeight, core::byte *dest,
                                     int destWidth, int destHeight, int bpp,
                                     double gamma)
{
   uchar *gammaMap1, *gammaMap2;

   if (scaleMode == BEAUTIFUL_GAMMA) {
      gammaMap1 = findGammaCorrectionTable (gamma);
      gammaMap2 = findGammaCorrectionTable (1 / gamma);
   }

   for(int x = 0; x < destWidth; x++)
      for(int y = 0; y < destHeight; y++) {
         int xo1 = x * srcWidth / destWidth;
         int xo2 = lout::misc::max ((x + 1) * srcWidth / destWidth, xo1 + 1);
         int yo1 = y * srcHeight / destHeight;
         int yo2 = lout::misc::max ((y + 1) * srcHeight / destHeight, yo1 + 1);
         int n = (xo2 - xo1) * (yo2 - yo1);

         int v[bpp];
         for(int i = 0; i < bpp; i++)
            v[i] = 0;

         for(int xo = xo1; xo < xo2; xo++)
            for(int yo = yo1; yo < yo2; yo++) {
               const core::byte *ps = src + bpp * (yo * srcWidth + xo);
               for(int i = 0; i < bpp; i++)
                  v[i] +=
                     (scaleMode == BEAUTIFUL_GAMMA ? gammaMap2[ps[i]] : ps[i]);
            }

         core::byte *pd = dest + bpp * (y * destWidth + x);
         for(int i = 0; i < bpp; i++)
            pd[i] =
               scaleMode == BEAUTIFUL_GAMMA ? gammaMap1[v[i] / n] : v[i] / n;
      }
}

void FltkImgbuf::copyRow (int row, const core::byte *data)
{
   assert (isRoot());

   if (row < height) {
      // Flag the row done and copy its data.
      copiedRows->set (row, true);
      memcpy(rawdata + row * width * bpp, data, width * bpp);

      // Update all the scaled buffers of this root image.
      for (Iterator <FltkImgbuf> it = scaledBuffers->iterator();
           it.hasNext(); ) {
         FltkImgbuf *sb = it.getNext ();
         sb->scaleRow (row, data);
      }
   }
}

void FltkImgbuf::newScan ()
{
   if (isRoot()) {
      for (Iterator<FltkImgbuf> it = scaledBuffers->iterator(); it.hasNext();){
         FltkImgbuf *sb = it.getNext ();
         sb->copiedRows->clear();
      }
   }
}

core::Imgbuf* FltkImgbuf::getScaledBuf (int width, int height)
{
   if (!isRoot())
      return root->getScaledBuf (width, height);

   if (width > MAX_WIDTH) {
      // Similar to init.
      MSG("FltkImgbuf::getScaledBuf: cannot handle large width %d\n", width);
      return getScaledBuf (MAX_WIDTH, height);
   }
   if (height > MAX_HEIGHT) {
      MSG("FltkImgbuf::getScaledBuf: cannot handle large height %d\n", height);
      return getScaledBuf (width, MAX_HEIGHT);
   }

   if (width == this->width && height == this->height) {
      ref ();
      return this;
   }

   for (Iterator <FltkImgbuf> it = scaledBuffers->iterator(); it.hasNext(); ) {
      FltkImgbuf *sb = it.getNext ();
      if (sb->width == width && sb->height == height) {
         sb->ref ();
         return sb;
      }
   }

   // Check for excessive image sizes which would cause crashes due to
   // too big allocations for the image buffer. In this case we return
   // a pointer to the unscaled image buffer.
   if (excessiveImageDimensions (width, height)) {
      MSG("FltkImgbuf::getScaledBuf: suspicious image size request %d x %d\n",
           width, height);
      ref ();
      return this;
   }

   // This size is not yet used, so a new buffer has to be created.
   FltkImgbuf *sb = new FltkImgbuf (type, width, height, gamma, this);
   scaledBuffers->append (sb);
   DBG_OBJ_ASSOC_CHILD (sb);

   return sb;
}

void FltkImgbuf::getRowArea (int row, dw::core::Rectangle *area)
{
   // TODO: May have to be adjusted.

   if (isRoot()) {
      /* root buffer */
      area->x = 0;
      area->y = row;
      area->width = width;
      area->height = 1;
      _MSG("::getRowArea: area x=%d y=%d width=%d height=%d\n",
           area->x, area->y, area->width, area->height);
   } else {
      if (row > root->height)
         area->x = area->y = area->width = area->height = 0;
      else {
         // scaled buffer
         int sr1 = scaledY (row);
         int sr2 = scaledY (row + 1);

         area->x = 0;
         area->y = sr1;
         area->width = width;
         area->height = sr2 - sr1;
         _MSG("::getRowArea: area x=%d y=%d width=%d height=%d\n",
              area->x, area->y, area->width, area->height);
      }
   }
}

int FltkImgbuf::getRootWidth ()
{
   return root ? root->width : width;
}

int FltkImgbuf::getRootHeight ()
{
   return root ? root->height : height;
}

core::Imgbuf *FltkImgbuf::createSimilarBuf (int width, int height)
{
   return new FltkImgbuf (type, width, height, gamma);
}

void FltkImgbuf::copyTo (Imgbuf *dest, int xDestRoot, int yDestRoot,
                         int xSrc, int ySrc, int widthSrc, int heightSrc)
{
   FltkImgbuf *fDest = (FltkImgbuf*)dest;
   assert (bpp == fDest->bpp);

   int xSrc2 = lout::misc::min (xSrc + widthSrc, fDest->width - xDestRoot);
   int ySrc2 = lout::misc::min (ySrc + heightSrc, fDest->height - yDestRoot);

   //printf ("copying from (%d, %d), %d x %d to (%d, %d) (root) => "
   //        "xSrc2 = %d, ySrc2 = %d\n",
   //        xSrc, ySrc, widthSrc, heightSrc, xDestRoot, yDestRoot,
   //        xSrc2, ySrc2);

   for (int x = xSrc; x < xSrc2; x++)
      for (int y = ySrc; y < ySrc2; y++) {
         int iSrc = x + width * y;
         int iDest = xDestRoot + x + fDest->width * (yDestRoot + y);

         //printf ("   (%d, %d): %d -> %d\n", x, y, iSrc, iDest);

         for (int b = 0; b < bpp; b++)
            fDest->rawdata[bpp * iDest + b] = rawdata[bpp * iSrc + b];
      }
}

void FltkImgbuf::ref ()
{
   refCount++;

   //if (root)
   //   MSG("FltkImgbuf[scaled %p, root is %p]: ref() => %d\n",
   //        this, root, refCount);
   //else
   //   MSG("FltkImgbuf[root %p]: ref() => %d\n", this, refCount);
}

void FltkImgbuf::unref ()
{
   //if (root)
   //   MSG("FltkImgbuf[scaled %p, root is %p]: ref() => %d\n",
   //       this, root, refCount - 1);
   //else
   //   MSG("FltkImgbuf[root %p]: ref() => %d\n", this, refCount - 1);

   if (--refCount == 0) {
      if (isRoot ()) {
         // Root buffer, it must be ensured that no scaled buffers are left.
         // See also FltkImgbuf::detachScaledBuf().
         if (scaledBuffers->isEmpty () && deleteOnUnref) {
            delete this;
         } else {
            _MSG("FltkImgbuf[root %p]: not deleted. numScaled=%d\n",
                 this, scaledBuffers->size ());
         }
      } else
         // Scaled buffer buffer, simply delete it.
         delete this;
   }
}

bool FltkImgbuf::lastReference ()
{
   return refCount == 1 &&
      (scaledBuffers == NULL || scaledBuffers->isEmpty ());
}

void FltkImgbuf::setDeleteOnUnref (bool deleteOnUnref)
{
   assert (isRoot ());
   this->deleteOnUnref = deleteOnUnref;
}

bool FltkImgbuf::isReferred ()
{
   return refCount != 0 ||
      (scaledBuffers != NULL && !scaledBuffers->isEmpty ());
}


int FltkImgbuf::scaledY(int ySrc)
{
   // TODO: May have to be adjusted.
   assert (root != NULL);
   return ySrc * height / root->height;
}

int FltkImgbuf::backscaledY(int yScaled)
{
   assert (root != NULL);

   // Notice that rounding errors because of integers do not play a
   // role. This method cannot be the exact inverse of scaledY, since
   // scaleY is not bijective, and so not invertible. Instead, both
   // values always return the smallest value.
   return yScaled * root->height / height;
}

void FltkImgbuf::draw (Fl_Widget *target, int xRoot, int yRoot,
                       int x, int y, int width, int height)
{
   // TODO: Clarify the question, whether "target" is the current widget
   //       (and so has not to be passed at all).

   _MSG("::draw: xRoot=%d x=%d yRoot=%d y=%d width=%d height=%d\n"
        "        this->width=%d this->height=%d\n",
        xRoot, x, yRoot, y, width, height, this->width, this->height);

   if (x > this->width || y > this->height) {
      return;
   }

   if (x + width > this->width) {
      width = this->width - x;
   }

   if (y + height > this->height) {
      height = this->height - y;
   }

   fl_draw_image(rawdata+bpp*(y*this->width + x), xRoot + x, yRoot + y, width,
                 height, bpp, this->width * bpp);

}

} // namespace fltk
} // namespace dw