gegl-buffer-access.c 90.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* This file is part of GEGL.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
16
 * Copyright 2006-2012,2014-2017  Øyvind Kolås <pippin@gimp.org>
Daniel Sabo's avatar
Daniel Sabo committed
17
 *           2013 Daniel Sabo
18 19 20 21
 */

#include "config.h"
#include <string.h>
22
#include <stdlib.h>
23 24 25 26
#include <math.h>

#include <glib-object.h>
#include <glib/gprintf.h>
27
#include <gio/gio.h>
28

29
#include "gegl.h"
30
#include "gegl-debug.h"
31
#include "gegl-types-internal.h"
Daniel Sabo's avatar
Daniel Sabo committed
32
#include "gegl-algorithms.h"
33 34 35 36
#include "gegl-buffer-types.h"
#include "gegl-buffer.h"
#include "gegl-buffer-private.h"
#include "gegl-tile-storage.h"
Daniel Sabo's avatar
Daniel Sabo committed
37
#include "gegl-sampler.h"
38
#include "gegl-tile-backend.h"
39
#include "gegl-buffer-iterator.h"
40
#include "gegl-buffer-cl-cache.h"
41
#include "gegl-config.h"
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
static void gegl_buffer_iterate_read_fringed (GeglBuffer          *buffer,
                                              const GeglRectangle *roi,
                                              const GeglRectangle *abyss,
                                              guchar              *buf,
                                              gint                 buf_stride,
                                              const Babl          *format,
                                              gint                 level,
                                              GeglAbyssPolicy      repeat_mode);


static void gegl_buffer_iterate_read_dispatch (GeglBuffer          *buffer,
                                               const GeglRectangle *roi,
                                               guchar              *buf,
                                               gint                 rowstride,
                                               const Babl          *format,
                                               gint                 level,
                                               GeglAbyssPolicy      repeat_mode);

61
static void inline
62 63 64 65 66 67
gegl_buffer_get_pixel (GeglBuffer     *buffer,
                       gint            x,
                       gint            y,
                       const Babl     *format,
                       gpointer        data,
                       GeglAbyssPolicy repeat_mode)
68
{
69 70
  const GeglRectangle *abyss = &buffer->abyss;
  guchar              *buf   = data;
Daniel Sabo's avatar
Daniel Sabo committed
71

72 73
  

Daniel Sabo's avatar
Daniel Sabo committed
74 75
  if (y <  abyss->y ||
      x <  abyss->x ||
76
      y >= abyss->y + abyss->height ||
Daniel Sabo's avatar
Daniel Sabo committed
77 78
      x >= abyss->x + abyss->width)
    {
79 80 81
      switch (repeat_mode)
      {
        case GEGL_ABYSS_CLAMP:
82
          x = CLAMP (x, abyss->x, abyss->x+abyss->width-1);
83
          y = CLAMP (y, abyss->y, abyss->y+abyss->height-1);
84 85 86
          break;

        case GEGL_ABYSS_LOOP:
87 88
          x = abyss->x + GEGL_REMAINDER (x - abyss->x, abyss->width);
          y = abyss->y + GEGL_REMAINDER (y - abyss->y, abyss->height);
89 90 91 92 93
          break;

        case GEGL_ABYSS_BLACK:
          {
            gfloat color[4] = {0.0, 0.0, 0.0, 1.0};
94
            babl_process (babl_fish (gegl_babl_rgba_linear_float (), format),
95 96 97 98 99
                          color,
                          buf,
                          1);
            return;
          }
100

101 102 103
        case GEGL_ABYSS_WHITE:
          {
            gfloat color[4] = {1.0, 1.0, 1.0, 1.0};
104
            babl_process (babl_fish (gegl_babl_rgba_linear_float (),
105 106 107 108 109 110 111 112 113
                                     format),
                          color,
                          buf,
                          1);
            return;
          }

        default:
        case GEGL_ABYSS_NONE:
114
          memset (buf, 0x00, babl_format_get_bytes_per_pixel (format));
115 116
          return;
      }
117 118
    }

119

120
  {
Daniel Sabo's avatar
Daniel Sabo committed
121 122
    gint tile_width  = buffer->tile_width;
    gint tile_height = buffer->tile_height;
123 124
    gint tiledy      = y + buffer->shift_y;
    gint tiledx      = x + buffer->shift_x;
125 126
    gint indice_x    = gegl_tile_indice (tiledx, tile_width);
    gint indice_y    = gegl_tile_indice (tiledy, tile_height);
127

128
    GeglTile *tile = buffer->tile_storage->hot_tile;
Daniel Sabo's avatar
Daniel Sabo committed
129
    const Babl *fish = NULL;
130

Daniel Sabo's avatar
Daniel Sabo committed
131 132 133
    if (!(tile &&
          tile->x == indice_x &&
          tile->y == indice_y))
134
      {
135 136 137
        if (gegl_config_threads()>1)
         g_rec_mutex_lock (&buffer->tile_storage->mutex);

138
        _gegl_buffer_drop_hot_tile (buffer);
139 140 141
        tile = gegl_tile_source_get_tile ((GeglTileSource *) (buffer),
                                          indice_x, indice_y,
                                          0);
142
        buffer->tile_storage->hot_tile = tile;
143 144 145

        if (gegl_config_threads()>1)
          g_rec_mutex_unlock (&buffer->tile_storage->mutex);
146
      }
147

148

149 150
    if (tile)
      {
Daniel Sabo's avatar
Daniel Sabo committed
151 152 153 154
        gint tile_origin_x = indice_x * tile_width;
        gint tile_origin_y = indice_y * tile_height;
        gint       offsetx = tiledx - tile_origin_x;
        gint       offsety = tiledy - tile_origin_y;
155
        guchar    *tp;
Daniel Sabo's avatar
Daniel Sabo committed
156

157
       if (format != buffer->soft_format)
158 159 160 161 162 163
          {
            gint px_size = babl_format_get_bytes_per_pixel (buffer->soft_format);
            fish    = babl_fish (buffer->soft_format, format);
            tp = gegl_tile_get_data (tile) + (offsety * tile_width + offsetx) * px_size;
            babl_process (fish, tp, buf, 1);
          }
164
        else
165 166 167 168 169
          {
            gint px_size = babl_format_get_bytes_per_pixel (format);
            tp = gegl_tile_get_data (tile) + (offsety * tile_width + offsetx) * px_size;
            memcpy (buf, tp, px_size);
          }
170 171
      }
  }
172

173 174
}

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
static inline void
__gegl_buffer_set_pixel (GeglBuffer     *buffer,
                       gint            x,
                       gint            y,
                       const Babl     *format,
                       gconstpointer   data)
{
  const GeglRectangle *abyss = &buffer->abyss;
  const guchar        *buf   = data;

  if (y <  abyss->y ||
      x <  abyss->x ||
      y >= abyss->y + abyss->height ||
      x >= abyss->x + abyss->width)
    return;

191

192 193 194 195 196
  {
    gint tile_width  = buffer->tile_width;
    gint tile_height = buffer->tile_height;
    gint tiledy      = y + buffer->shift_y;
    gint tiledx      = x + buffer->shift_x;
197 198
    gint indice_x    = gegl_tile_indice (tiledx, tile_width);
    gint indice_y    = gegl_tile_indice (tiledy, tile_height);
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

    GeglTile *tile = buffer->tile_storage->hot_tile;
    const Babl *fish = NULL;
    gint px_size;

    if (format != buffer->soft_format)
      {
        fish    = babl_fish (format, buffer->soft_format);
        px_size = babl_format_get_bytes_per_pixel (buffer->soft_format);
      }
    else
      {
        px_size = babl_format_get_bytes_per_pixel (buffer->soft_format);
      }

    if (!(tile &&
          tile->x == indice_x &&
          tile->y == indice_y))
      {
218 219 220
        if (gegl_config_threads()>1)
          g_rec_mutex_lock (&buffer->tile_storage->mutex);

221 222 223 224 225
        _gegl_buffer_drop_hot_tile (buffer);
        tile = gegl_tile_source_get_tile ((GeglTileSource *) (buffer),
                                          indice_x, indice_y,
                                          0);
        buffer->tile_storage->hot_tile = tile;
226 227 228

        if (gegl_config_threads()>1)
          g_rec_mutex_unlock (&buffer->tile_storage->mutex);
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
      }

    if (tile)
      {
        gint tile_origin_x = indice_x * tile_width;
        gint tile_origin_y = indice_y * tile_height;
        gint       offsetx = tiledx - tile_origin_x;
        gint       offsety = tiledy - tile_origin_y;

        guchar *tp;
        gegl_tile_lock (tile);
        tp = gegl_tile_get_data (tile) + (offsety * tile_width + offsetx) * px_size;

        if (fish)
          babl_process (fish, buf, tp, 1);
        else
          memcpy (tp, buf, px_size);

        gegl_tile_unlock (tile);
      }
  }
250

251 252 253 254 255
}

enum _GeglBufferSetFlag {
  GEGL_BUFFER_SET_FLAG_FAST   = 0,
  GEGL_BUFFER_SET_FLAG_LOCK   = 1<<0,
Michael Henning's avatar
Michael Henning committed
256
  GEGL_BUFFER_SET_FLAG_NOTIFY = 1<<1,
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
  GEGL_BUFFER_SET_FLAG_FULL   = GEGL_BUFFER_SET_FLAG_LOCK |
                                GEGL_BUFFER_SET_FLAG_NOTIFY
};

typedef enum _GeglBufferSetFlag GeglBufferSetFlag;

void
gegl_buffer_set_with_flags (GeglBuffer          *buffer,
                            const GeglRectangle *rect,
                            gint                 level,
                            const Babl          *format,
                            const void          *src,
                            gint                 rowstride,
                            GeglBufferSetFlag    set_flags);


static inline void
_gegl_buffer_set_pixel (GeglBuffer       *buffer,
                        gint              x,
                        gint              y,
                        const Babl       *format,
                        gconstpointer     data,
                        GeglBufferSetFlag flags)
{
  GeglRectangle rect = {x,y,1,1};
  switch (flags)
  {
    case GEGL_BUFFER_SET_FLAG_FAST:
      __gegl_buffer_set_pixel (buffer, x, y, format, data);
    break;
    case GEGL_BUFFER_SET_FLAG_LOCK:
      gegl_buffer_lock (buffer);
      __gegl_buffer_set_pixel (buffer, x, y, format, data);
      gegl_buffer_unlock (buffer);
      break;
    case GEGL_BUFFER_SET_FLAG_NOTIFY:
      __gegl_buffer_set_pixel (buffer, x, y, format, data);
Michael Henning's avatar
Michael Henning committed
294
      gegl_buffer_emit_changed_signal (buffer, &rect);
295 296 297 298 299 300
      break;
    case GEGL_BUFFER_SET_FLAG_LOCK|GEGL_BUFFER_SET_FLAG_NOTIFY:
    default:
      gegl_buffer_lock (buffer);
      __gegl_buffer_set_pixel (buffer, x, y, format, data);
      gegl_buffer_unlock (buffer);
Michael Henning's avatar
Michael Henning committed
301
      gegl_buffer_emit_changed_signal (buffer, &rect);
302 303 304 305
      break;
  }
}

306 307 308 309 310 311
/* flush any unwritten data (flushes the hot-cache of a single
 * tile used by gegl_buffer_set for 1x1 pixel sized rectangles
 */
void
gegl_buffer_flush (GeglBuffer *buffer)
{
312 313
  GeglTileBackend *backend;

314
  g_return_if_fail (GEGL_IS_BUFFER (buffer));
315
  backend = gegl_buffer_backend (buffer);
316

317 318 319
  if (gegl_config_threads()>1)
    g_rec_mutex_lock (&buffer->tile_storage->mutex);

320
  _gegl_buffer_drop_hot_tile (buffer);
321

322 323
  if (backend)
    gegl_tile_backend_set_extent (backend, &buffer->extent);
324

325 326
  gegl_tile_source_command (GEGL_TILE_SOURCE (buffer),
                            GEGL_TILE_FLUSH, 0,0,0,NULL);
327 328 329

  if (gegl_config_threads()>1)
    g_rec_mutex_unlock (&buffer->tile_storage->mutex);
330 331
}

332
static inline void
333
gegl_buffer_iterate_write (GeglBuffer          *buffer,
334
                           const GeglRectangle *roi,
335 336 337 338
                           guchar              *buf,
                           gint                 rowstride,
                           const Babl          *format,
                           gint                 level)
339
{
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
  gint tile_width  = buffer->tile_storage->tile_width;
  gint tile_height = buffer->tile_storage->tile_height;
  gint px_size     = babl_format_get_bytes_per_pixel (buffer->soft_format);
  gint bpx_size    = babl_format_get_bytes_per_pixel (format);
  gint tile_stride = px_size * tile_width;
  gint buf_stride;
  gint bufy        = 0;

  gint buffer_shift_x = buffer->shift_x;
  gint buffer_shift_y = buffer->shift_y;

  gint width    = buffer->extent.width;
  gint height   = buffer->extent.height;
  gint buffer_x = buffer->extent.x + buffer_shift_x;
  gint buffer_y = buffer->extent.y + buffer_shift_y;

  gint buffer_abyss_x = buffer->abyss.x + buffer_shift_x;
  gint buffer_abyss_y = buffer->abyss.y + buffer_shift_y;
  gint abyss_x_total  = buffer_abyss_x + buffer->abyss.width;
  gint abyss_y_total  = buffer_abyss_y + buffer->abyss.height;
  gint factor         = 1<<level;
361
  const Babl *fish;
362 363 364 365 366 367 368 369 370 371
  GeglRectangle scaled_rect;
  if (level && roi)
  {
    scaled_rect = *roi;
    scaled_rect.x <<= level;
    scaled_rect.y <<= level;
    scaled_rect.width <<= level;
    scaled_rect.height <<= level;
    roi = &scaled_rect;
  }
372

373 374 375 376 377 378 379 380
  /* roi specified, override buffers extent */
  if (roi)
    {
      width  = roi->width;
      height = roi->height;
      buffer_x = roi->x + buffer_shift_x;
      buffer_y = roi->y + buffer_shift_y;
    }
381

382 383
  buffer_shift_x /= factor;
  buffer_shift_y /= factor;
384 385 386 387 388 389 390 391 392
  buffer_abyss_x /= factor;
  buffer_abyss_y /= factor;
  abyss_x_total  /= factor;
  abyss_y_total  /= factor;
  buffer_x       /= factor;
  buffer_y       /= factor;
  width          /= factor;
  height         /= factor;

393

394 395 396 397
  buf_stride = width * bpx_size;
  if (rowstride != GEGL_AUTO_ROWSTRIDE)
    buf_stride = rowstride;

398
  if (format == buffer->soft_format)
399 400 401 402
    {
      fish = NULL;
    }
  else
403 404 405 406
      fish = babl_fish ((gpointer) format,
                        (gpointer) buffer->soft_format);

  while (bufy < height)
407
    {
408 409 410 411 412
      gint tiledy  = buffer_y + bufy;
      gint offsety = gegl_tile_offset (tiledy, tile_height);
      gint bufx    = 0;

      while (bufx < width)
413
        {
414 415 416
          gint      tiledx  = buffer_x + bufx;
          gint      offsetx = gegl_tile_offset (tiledx, tile_width);
          gint      y       = bufy;
417 418
          gint      index_x;
          gint      index_y;
419 420 421 422 423 424 425 426 427 428 429
          gint      lskip, rskip, pixels, row;
          guchar   *bp, *tile_base, *tp;
          GeglTile *tile;

          bp = buf + bufy * buf_stride + bufx * bpx_size;

          if (width + offsetx - bufx < tile_width)
            pixels = (width + offsetx - bufx) - offsetx;
          else
            pixels = tile_width - offsetx;

430 431
          index_x = gegl_tile_indice (tiledx, tile_width);
          index_y = gegl_tile_indice (tiledy, tile_height);
432

433
          tile = gegl_buffer_get_tile (buffer, index_x, index_y, level);
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

          lskip = (buffer_abyss_x) - (buffer_x + bufx);
          /* gap between left side of tile, and abyss */
          rskip = (buffer_x + bufx + pixels) - abyss_x_total;
          /* gap between right side of tile, and abyss */

          if (lskip < 0)
            lskip = 0;
          if (lskip > pixels)
            lskip = pixels;
          if (rskip < 0)
            rskip = 0;
          if (rskip > pixels)
            rskip = pixels;

          if (!tile)
            {
              g_warning ("didn't get tile, trying to continue");
              bufx += (tile_width - offsetx);
              continue;
            }

          gegl_tile_lock (tile);

          tile_base = gegl_tile_get_data (tile);
          tp        = ((guchar *) tile_base) + (offsety * tile_width + offsetx) * px_size;

461 462 463
          pixels -= lskip;
          pixels -= rskip;

464 465 466 467 468 469 470 471 472 473 474 475 476
          if (fish)
            {
              for (row = offsety;
                   row < tile_height &&
                     y < height &&
                     buffer_y + y < abyss_y_total;
                   row++, y++)
                {

                  if (buffer_y + y >= buffer_abyss_y &&
                      buffer_y + y < abyss_y_total)
                    {
                      babl_process (fish, bp + lskip * bpx_size, tp + lskip * px_size,
477
                                    pixels);
478 479 480 481 482 483 484 485
                    }

                  tp += tile_stride;
                  bp += buf_stride;
                }
            }
          else
            {
486
              int lskip_offset = lskip * px_size;
487
              switch (pixels * px_size)
488
                {
489 490 491 492
                  case 1:
                    for (row = offsety;
                         row < tile_height && y < height;
                         row++, y++)
493
                    {
494 495 496
                      if (buffer_y + y >= buffer_abyss_y &&
                          buffer_y + y < abyss_y_total)
                        {
497
                          tp[lskip_offset] = bp[lskip_offset];
498 499 500 501 502 503 504 505 506 507 508 509 510
                        }
                      tp += tile_stride;
                      bp += buf_stride;
                    }
                    break;
                  case 2:
                    for (row = offsety;
                         row < tile_height && y < height;
                         row++, y++)
                    {
                      if (buffer_y + y >= buffer_abyss_y &&
                          buffer_y + y < abyss_y_total)
                        {
511 512
                          ((uint16_t*)(&tp[lskip_offset]))[0] =
                          ((uint16_t*)(&bp[lskip_offset]))[0];
513 514 515 516 517 518 519 520 521 522 523 524 525
                        }
                      tp += tile_stride;
                      bp += buf_stride;
                    }
                    break;
                  case 3:
                    for (row = offsety;
                         row < tile_height && y < height;
                         row++, y++)
                    {
                      if (buffer_y + y >= buffer_abyss_y &&
                          buffer_y + y < abyss_y_total)
                        {
526 527 528
                          tp[lskip_offset] = bp[lskip_offset];
                          tp[lskip_offset+1] = bp[lskip_offset+1];
                          tp[lskip_offset+2] = bp[lskip_offset+2];
529 530 531 532 533 534 535 536 537 538 539 540 541
                        }
                      tp += tile_stride;
                      bp += buf_stride;
                    }
                    break;
                  case 4:
                    for (row = offsety;
                         row < tile_height && y < height;
                         row++, y++)
                    {
                      if (buffer_y + y >= buffer_abyss_y &&
                          buffer_y + y < abyss_y_total)
                        {
542 543
                          ((uint32_t*)(&tp[lskip_offset]))[0] =
                          ((uint32_t*)(&bp[lskip_offset]))[0];
544 545 546 547 548 549 550 551 552 553 554 555 556
                        }
                      tp += tile_stride;
                      bp += buf_stride;
                    }
                    break;
                  case 8:
                    for (row = offsety;
                         row < tile_height && y < height;
                         row++, y++)
                    {
                      if (buffer_y + y >= buffer_abyss_y &&
                          buffer_y + y < abyss_y_total)
                        {
557 558
                          ((uint64_t*)(&tp[lskip_offset]))[0] =
                          ((uint64_t*)(&bp[lskip_offset]))[0];
559 560 561 562 563 564 565 566 567 568 569 570 571
                        }
                      tp += tile_stride;
                      bp += buf_stride;
                    }
                    break;
                  case 12:
                    for (row = offsety;
                         row < tile_height && y < height;
                         row++, y++)
                    {
                      if (buffer_y + y >= buffer_abyss_y &&
                          buffer_y + y < abyss_y_total)
                        {
572 573 574 575 576 577
                          ((uint32_t*)(&tp[lskip_offset]))[0] =
                          ((uint32_t*)(&bp[lskip_offset]))[0];
                          ((uint32_t*)(&tp[lskip_offset]))[1] =
                          ((uint32_t*)(&bp[lskip_offset]))[1];
                          ((uint32_t*)(&tp[lskip_offset]))[2] =
                          ((uint32_t*)(&bp[lskip_offset]))[2];
578 579 580 581 582 583 584 585 586 587 588 589 590
                        }
                      tp += tile_stride;
                      bp += buf_stride;
                    }
                    break;
                  case 16:
                    for (row = offsety;
                         row < tile_height && y < height;
                         row++, y++)
                    {
                      if (buffer_y + y >= buffer_abyss_y &&
                          buffer_y + y < abyss_y_total)
                        {
591 592 593 594
                          ((uint64_t*)(&tp[lskip_offset]))[0] =
                          ((uint64_t*)(&bp[lskip_offset]))[0];
                          ((uint64_t*)(&tp[lskip_offset]))[1] =
                          ((uint64_t*)(&bp[lskip_offset]))[1];
595 596 597 598 599 600 601 602 603 604 605 606 607
                        }
                      tp += tile_stride;
                      bp += buf_stride;
                    }
                    break;
                  case 24:
                    for (row = offsety;
                         row < tile_height && y < height;
                         row++, y++)
                    {
                      if (buffer_y + y >= buffer_abyss_y &&
                          buffer_y + y < abyss_y_total)
                        {
608 609 610 611 612 613
                          ((uint64_t*)(&tp[lskip_offset]))[0] =
                          ((uint64_t*)(&bp[lskip_offset]))[0];
                          ((uint64_t*)(&tp[lskip_offset]))[1] =
                          ((uint64_t*)(&bp[lskip_offset]))[1];
                          ((uint64_t*)(&tp[lskip_offset]))[2] =
                          ((uint64_t*)(&bp[lskip_offset]))[2];
614 615 616 617 618 619 620 621 622 623 624 625 626
                        }
                      tp += tile_stride;
                      bp += buf_stride;
                    }
                    break;
                  case 32:
                    for (row = offsety;
                         row < tile_height && y < height;
                         row++, y++)
                    {
                      if (buffer_y + y >= buffer_abyss_y &&
                          buffer_y + y < abyss_y_total)
                        {
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 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
                          ((uint64_t*)(&tp[lskip_offset]))[0] =
                          ((uint64_t*)(&bp[lskip_offset]))[0];
                          ((uint64_t*)(&tp[lskip_offset]))[1] =
                          ((uint64_t*)(&bp[lskip_offset]))[1];
                          ((uint64_t*)(&tp[lskip_offset]))[2] =
                          ((uint64_t*)(&bp[lskip_offset]))[2];
                          ((uint64_t*)(&tp[lskip_offset]))[3] =
                          ((uint64_t*)(&bp[lskip_offset]))[3];
                        }
                      tp += tile_stride;
                      bp += buf_stride;
                    }
                    break;
                  case 40:
                    for (row = offsety;
                         row < tile_height && y < height;
                         row++, y++)
                    {
                      if (buffer_y + y >= buffer_abyss_y &&
                          buffer_y + y < abyss_y_total)
                        {
                          ((uint64_t*)(&tp[lskip_offset]))[0] =
                          ((uint64_t*)(&bp[lskip_offset]))[0];
                          ((uint64_t*)(&tp[lskip_offset]))[1] =
                          ((uint64_t*)(&bp[lskip_offset]))[1];
                          ((uint64_t*)(&tp[lskip_offset]))[2] =
                          ((uint64_t*)(&bp[lskip_offset]))[2];
                          ((uint64_t*)(&tp[lskip_offset]))[3] =
                          ((uint64_t*)(&bp[lskip_offset]))[3];
                          ((uint64_t*)(&tp[lskip_offset]))[4] =
                          ((uint64_t*)(&bp[lskip_offset]))[4];
                        }
                      tp += tile_stride;
                      bp += buf_stride;
                    }
                    break;
                  case 48:
                    for (row = offsety;
                         row < tile_height && y < height;
                         row++, y++)
                    {
                      if (buffer_y + y >= buffer_abyss_y &&
                          buffer_y + y < abyss_y_total)
                        {
                          ((uint64_t*)(&tp[lskip_offset]))[0] =
                          ((uint64_t*)(&bp[lskip_offset]))[0];
                          ((uint64_t*)(&tp[lskip_offset]))[1] =
                          ((uint64_t*)(&bp[lskip_offset]))[1];
                          ((uint64_t*)(&tp[lskip_offset]))[2] =
                          ((uint64_t*)(&bp[lskip_offset]))[2];
                          ((uint64_t*)(&tp[lskip_offset]))[3] =
                          ((uint64_t*)(&bp[lskip_offset]))[3];
                          ((uint64_t*)(&tp[lskip_offset]))[4] =
                          ((uint64_t*)(&bp[lskip_offset]))[4];
                          ((uint64_t*)(&tp[lskip_offset]))[5] =
                          ((uint64_t*)(&bp[lskip_offset]))[5];
                        }
                      tp += tile_stride;
                      bp += buf_stride;
                    }
                    break;
                  case 56:
                    for (row = offsety;
                         row < tile_height && y < height;
                         row++, y++)
                    {
                      if (buffer_y + y >= buffer_abyss_y &&
                          buffer_y + y < abyss_y_total)
                        {
                          ((uint64_t*)(&tp[lskip_offset]))[0] =
                          ((uint64_t*)(&bp[lskip_offset]))[0];
                          ((uint64_t*)(&tp[lskip_offset]))[1] =
                          ((uint64_t*)(&bp[lskip_offset]))[1];
                          ((uint64_t*)(&tp[lskip_offset]))[2] =
                          ((uint64_t*)(&bp[lskip_offset]))[2];
                          ((uint64_t*)(&tp[lskip_offset]))[3] =
                          ((uint64_t*)(&bp[lskip_offset]))[3];
                          ((uint64_t*)(&tp[lskip_offset]))[4] =
                          ((uint64_t*)(&bp[lskip_offset]))[4];
                          ((uint64_t*)(&tp[lskip_offset]))[5] =
                          ((uint64_t*)(&bp[lskip_offset]))[5];
                          ((uint64_t*)(&tp[lskip_offset]))[6] =
                          ((uint64_t*)(&bp[lskip_offset]))[6];
                        }
                      tp += tile_stride;
                      bp += buf_stride;
                    }
                    break;
                  case 64:
                    for (row = offsety;
                         row < tile_height && y < height;
                         row++, y++)
                    {
                      if (buffer_y + y >= buffer_abyss_y &&
                          buffer_y + y < abyss_y_total)
                        {
                          ((uint64_t*)(&tp[lskip_offset]))[0] =
                          ((uint64_t*)(&bp[lskip_offset]))[0];
                          ((uint64_t*)(&tp[lskip_offset]))[1] =
                          ((uint64_t*)(&bp[lskip_offset]))[1];
                          ((uint64_t*)(&tp[lskip_offset]))[2] =
                          ((uint64_t*)(&bp[lskip_offset]))[2];
                          ((uint64_t*)(&tp[lskip_offset]))[3] =
                          ((uint64_t*)(&bp[lskip_offset]))[3];
                          ((uint64_t*)(&tp[lskip_offset]))[4] =
                          ((uint64_t*)(&bp[lskip_offset]))[4];
                          ((uint64_t*)(&tp[lskip_offset]))[5] =
                          ((uint64_t*)(&bp[lskip_offset]))[5];
                          ((uint64_t*)(&tp[lskip_offset]))[6] =
                          ((uint64_t*)(&bp[lskip_offset]))[6];
                          ((uint64_t*)(&tp[lskip_offset]))[7] =
                          ((uint64_t*)(&bp[lskip_offset]))[7];
739 740 741 742 743 744 745 746 747 748 749 750 751
                        }
                      tp += tile_stride;
                      bp += buf_stride;
                    }
                    break;
                  default:
                    for (row = offsety;
                         row < tile_height && y < height;
                         row++, y++)
                    {
                      if (buffer_y + y >= buffer_abyss_y &&
                          buffer_y + y < abyss_y_total)
                        {
752 753
                          memcpy (tp + lskip_offset,
                                  bp + lskip_offset,
754 755 756 757
                                  pixels * px_size);
                        }
                      tp += tile_stride;
                      bp += buf_stride;
758 759 760 761 762 763 764
                    }
                }
            }

          gegl_tile_unlock (tile);
          gegl_tile_unref (tile);
          bufx += (tile_width - offsetx);
765
        }
766 767 768 769
      bufy += (tile_height - offsety);
    }
}

770 771 772 773 774 775 776 777 778 779 780 781 782
static inline void
gegl_buffer_set_internal (GeglBuffer          *buffer,
                          const GeglRectangle *rect,
                          gint                 level,
                          const Babl          *format,
                          const void          *src,
                          gint                 rowstride)
{
  if (gegl_cl_is_accelerated ())
    {
      gegl_buffer_cl_cache_flush (buffer, rect);
    }

783
  gegl_buffer_iterate_write (buffer, rect, (void *) src, rowstride, format, level);
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839

  if (gegl_buffer_is_shared (buffer))
    {
      gegl_buffer_flush (buffer);
    }
}

static void inline
_gegl_buffer_set_with_flags (GeglBuffer       *buffer,
                            const GeglRectangle *rect,
                            gint                 level,
                            const Babl          *format,
                            const void          *src,
                            gint                 rowstride,
                            GeglBufferSetFlag    flags)
{
  switch (flags)
  {
    case GEGL_BUFFER_SET_FLAG_FAST:
      gegl_buffer_set_internal (buffer, rect, level, format, src, rowstride);
    break;
    case GEGL_BUFFER_SET_FLAG_LOCK:
      gegl_buffer_lock (buffer);
      gegl_buffer_set_internal (buffer, rect, level, format, src, rowstride);
      gegl_buffer_unlock (buffer);
      break;
    case GEGL_BUFFER_SET_FLAG_NOTIFY:
      gegl_buffer_set_internal (buffer, rect, level, format, src, rowstride);
      if (flags & GEGL_BUFFER_SET_FLAG_NOTIFY)
        gegl_buffer_emit_changed_signal(buffer, rect);
      break;
    case GEGL_BUFFER_SET_FLAG_LOCK|GEGL_BUFFER_SET_FLAG_NOTIFY:
    default:
      gegl_buffer_lock (buffer);
      gegl_buffer_set_internal (buffer, rect, level, format, src, rowstride);
      gegl_buffer_unlock (buffer);
      gegl_buffer_emit_changed_signal(buffer, rect);
      break;
  }
}

void
gegl_buffer_set_with_flags (GeglBuffer       *buffer,
                            const GeglRectangle *rect,
                            gint                 level,
                            const Babl          *format,
                            const void          *src,
                            gint                 rowstride,
                            GeglBufferSetFlag    flags)
{
  g_return_if_fail (GEGL_IS_BUFFER (buffer));
  if (format == NULL)
    format = buffer->soft_format;
  _gegl_buffer_set_with_flags (buffer, rect, level, format, src, rowstride, flags);
}

Daniel Sabo's avatar
Daniel Sabo committed
840
static void
841 842 843 844 845 846 847
gegl_buffer_iterate_read_simple (GeglBuffer          *buffer,
                                 const GeglRectangle *roi,
                                 guchar              *buf,
                                 gint                 buf_stride,
                                 const Babl          *format,
                                 gint                 level)
{
848 849 850 851 852 853 854 855 856 857 858
  gint tile_width  = buffer->tile_storage->tile_width;
  gint tile_height = buffer->tile_storage->tile_height;
  gint px_size     = babl_format_get_bytes_per_pixel (buffer->soft_format);
  gint bpx_size    = babl_format_get_bytes_per_pixel (format);
  gint tile_stride = px_size * tile_width;
  gint bufy        = 0;

  gint width    = roi->width;
  gint height   = roi->height;
  gint buffer_x = roi->x;
  gint buffer_y = roi->y;
859 860 861 862 863 864 865 866 867

  const Babl *fish;

  if (format == buffer->soft_format)
    fish = NULL;
  else
    fish = babl_fish ((gpointer) buffer->soft_format,
                      (gpointer) format);

868

869 870 871 872 873 874 875
  while (bufy < height)
    {
      gint tiledy  = buffer_y + bufy;
      gint offsety = gegl_tile_offset (tiledy, tile_height);
      gint bufx    = 0;

      while (bufx < width)
876
        {
877 878 879 880 881 882 883 884 885
          gint      tiledx  = buffer_x + bufx;
          gint      offsetx = gegl_tile_offset (tiledx, tile_width);
          guchar   *bp, *tile_base, *tp;
          gint      pixels, row, y;
          GeglTile *tile;

          bp = buf + bufy * buf_stride + bufx * bpx_size;

          if (width + offsetx - bufx < tile_width)
886
            pixels = width - bufx;
887 888 889
          else
            pixels = tile_width - offsetx;

890 891 892 893 894 895 896 897 898 899 900 901
          if (gegl_config_threads()>1)
          {
             g_rec_mutex_lock (&buffer->tile_storage->mutex);
             tile = gegl_tile_source_get_tile ((GeglTileSource *) (buffer),
                                            gegl_tile_indice (tiledx, tile_width),
                                            gegl_tile_indice (tiledy, tile_height),
                                            level);
            g_rec_mutex_unlock (&buffer->tile_storage->mutex);
          }
          else
          {
             tile = gegl_tile_source_get_tile ((GeglTileSource *) (buffer),
902 903
                                            gegl_tile_indice (tiledx, tile_width),
                                            gegl_tile_indice (tiledy, tile_height),
904
                                            level);
905
          }
906 907 908 909 910 911 912 913 914 915 916 917

          if (!tile)
            {
              g_warning ("didn't get tile, trying to continue");
              bufx += (tile_width - offsetx);
              continue;
            }

          tile_base = gegl_tile_get_data (tile);
          tp        = ((guchar *) tile_base) + (offsety * tile_width + offsetx) * px_size;

          y = bufy;
918 919 920 921 922 923 924 925 926 927 928 929 930 931

          if (fish)
            {
              for (row = offsety;
                   row < tile_height && y < height;
                   row++, y++)
                {
                  babl_process (fish, tp, bp, pixels);

                  tp += tile_stride;
                  bp += buf_stride;
                }
            }
          else
932
            {
933
              switch (pixels * px_size)
934
                {
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
                  case 1:
                    for (row = offsety; row < tile_height && y < height;
                         row++, y++)
                      {
                         bp[0] = tp[0];
                         tp += tile_stride;
                         bp += buf_stride;
                      }
                    break;
                  case 2:
                    for (row = offsety; row < tile_height && y < height;
                         row++, y++)
                      {
                         ((uint16_t*)bp)[0] = ((uint16_t*)tp)[0];
                         tp += tile_stride;
                         bp += buf_stride;
                      }
                    break;
953 954 955 956 957 958 959 960 961 962 963
                  case 3:
                    for (row = offsety; row < tile_height && y < height;
                         row++, y++)
                      {
                         bp[0] = tp[0];
                         bp[1] = tp[1];
                         bp[2] = tp[2];
                         tp += tile_stride;
                         bp += buf_stride;
                      }
                    break;
964 965 966 967 968 969 970 971 972
                  case 4:
                    for (row = offsety; row < tile_height && y < height;
                         row++, y++)
                      {
                         ((uint32_t*)bp)[0] = ((uint32_t*)tp)[0];
                         tp += tile_stride;
                         bp += buf_stride;
                      }
                    break;
973 974 975 976 977 978 979 980 981 982 983
                  case 6:
                    for (row = offsety; row < tile_height && y < height;
                         row++, y++)
                      {
                         ((uint16_t*)bp)[0] = ((uint16_t*)tp)[0];
                         ((uint16_t*)bp)[1] = ((uint16_t*)tp)[1];
                         ((uint16_t*)bp)[2] = ((uint16_t*)tp)[2];
                         tp += tile_stride;
                         bp += buf_stride;
                      }
                    break;
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
                  case 8:
                    for (row = offsety; row < tile_height && y < height;
                         row++, y++)
                      {
                         ((uint64_t*)bp)[0] = ((uint64_t*)tp)[0];
                         tp += tile_stride;
                         bp += buf_stride;
                      }
                    break;
                  case 12:
                    for (row = offsety; row < tile_height && y < height;
                         row++, y++)
                      {
                         ((uint32_t*)bp)[0] = ((uint32_t*)tp)[0];
                         ((uint32_t*)bp)[1] = ((uint32_t*)tp)[1];
                         ((uint32_t*)bp)[2] = ((uint32_t*)tp)[2];
                         tp += tile_stride;
                         bp += buf_stride;
                      }
                    break;
                  case 16:
                    for (row = offsety; row < tile_height && y < height;
                         row++, y++)
                      {
                         ((uint64_t*)bp)[0] = ((uint64_t*)tp)[0];
                         ((uint64_t*)bp)[1] = ((uint64_t*)tp)[1];
                         tp += tile_stride;
                         bp += buf_stride;
                      }
                    break;
                  case 24:
                    for (row = offsety; row < tile_height && y < height;
                         row++, y++)
                      {
                         ((uint64_t*)bp)[0] = ((uint64_t*)tp)[0];
                         ((uint64_t*)bp)[1] = ((uint64_t*)tp)[1];
                         ((uint64_t*)bp)[2] = ((uint64_t*)tp)[2];
                         tp += tile_stride;
                         bp += buf_stride;
                      }
                    break;
                  case 32:
                    for (row = offsety; row < tile_height && y < height;
                         row++, y++)
                      {
                         ((uint64_t*)bp)[0] = ((uint64_t*)tp)[0];
                         ((uint64_t*)bp)[1] = ((uint64_t*)tp)[1];
                         ((uint64_t*)bp)[2] = ((uint64_t*)tp)[2];
                         ((uint64_t*)bp)[3] = ((uint64_t*)tp)[3];
                         tp += tile_stride;
                         bp += buf_stride;
                      }
                    break;
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
                  case 40:
                    for (row = offsety; row < tile_height && y < height;
                         row++, y++)
                      {
                         ((uint64_t*)bp)[0] = ((uint64_t*)tp)[0];
                         ((uint64_t*)bp)[1] = ((uint64_t*)tp)[1];
                         ((uint64_t*)bp)[2] = ((uint64_t*)tp)[2];
                         ((uint64_t*)bp)[3] = ((uint64_t*)tp)[3];
                         ((uint64_t*)bp)[4] = ((uint64_t*)tp)[4];
                         tp += tile_stride;
                         bp += buf_stride;
                      }
                    break;
                  case 48:
                    for (row = offsety; row < tile_height && y < height;
                         row++, y++)
                      {
                         ((uint64_t*)bp)[0] = ((uint64_t*)tp)[0];
                         ((uint64_t*)bp)[1] = ((uint64_t*)tp)[1];
                         ((uint64_t*)bp)[2] = ((uint64_t*)tp)[2];
                         ((uint64_t*)bp)[3] = ((uint64_t*)tp)[3];
                         ((uint64_t*)bp)[4] = ((uint64_t*)tp)[4];
                         ((uint64_t*)bp)[5] = ((uint64_t*)tp)[5];
                         tp += tile_stride;
                         bp += buf_stride;
                      }
                    break;
                  case 56:
                    for (row = offsety; row < tile_height && y < height;
                         row++, y++)
                      {
                         ((uint64_t*)bp)[0] = ((uint64_t*)tp)[0];
                         ((uint64_t*)bp)[1] = ((uint64_t*)tp)[1];
                         ((uint64_t*)bp)[2] = ((uint64_t*)tp)[2];
                         ((uint64_t*)bp)[3] = ((uint64_t*)tp)[3];
                         ((uint64_t*)bp)[4] = ((uint64_t*)tp)[4];
                         ((uint64_t*)bp)[5] = ((uint64_t*)tp)[5];
                         ((uint64_t*)bp)[6] = ((uint64_t*)tp)[6];
                         tp += tile_stride;
                         bp += buf_stride;
                      }
                    break;
                  case 64:
                    for (row = offsety; row < tile_height && y < height;
                         row++, y++)
                      {
                         ((uint64_t*)bp)[0] = ((uint64_t*)tp)[0];
                         ((uint64_t*)bp)[1] = ((uint64_t*)tp)[1];
                         ((uint64_t*)bp)[2] = ((uint64_t*)tp)[2];
                         ((uint64_t*)bp)[3] = ((uint64_t*)tp)[3];
                         ((uint64_t*)bp)[4] = ((uint64_t*)tp)[4];
                         ((uint64_t*)bp)[5] = ((uint64_t*)tp)[5];
                         ((uint64_t*)bp)[6] = ((uint64_t*)tp)[6];
                         ((uint64_t*)bp)[7] = ((uint64_t*)tp)[7];
                         tp += tile_stride;
                         bp += buf_stride;
                      }
                    break;

1096 1097 1098 1099 1100 1101 1102 1103 1104
                  default:
                    for (row = offsety;
                         row < tile_height && y < height;
                         row++, y++)
                      {
                         memcpy (bp, tp, pixels * px_size);
                         tp += tile_stride;
                         bp += buf_stride;
                      }
1105
                }
1106 1107 1108 1109
            }

          gegl_tile_unref (tile);
          bufx += (tile_width - offsetx);
1110
        }
1111
      bufy += (tile_height - offsety);
1112
    }
1113

1114 1115
}

Daniel Sabo's avatar
Daniel Sabo committed
1116 1117
static void
fill_abyss_none (guchar *buf, gint width, gint height, gint buf_stride, gint pixel_size)
1118
{
Daniel Sabo's avatar
Daniel Sabo committed
1119
  const int byte_width = width * pixel_size;
1120

Daniel Sabo's avatar
Daniel Sabo committed
1121 1122 1123 1124
  if (buf_stride == byte_width)
    {
      memset (buf, 0, byte_width * height);
    }
1125
  else
1126
    {
Daniel Sabo's avatar
Daniel Sabo committed
1127 1128 1129 1130
      while (height--)
        {
          memset (buf, 0, byte_width);
          buf += buf_stride;
1131
        }
Daniel Sabo's avatar
Daniel Sabo committed
1132 1133
    }
}
1134

Daniel Sabo's avatar
Daniel Sabo committed
1135 1136 1137
static void
fill_abyss_color (guchar *buf, gint width, gint height, gint buf_stride, guchar *pixel, gint pixel_size)
{
Daniel Sabo's avatar
Daniel Sabo committed
1138
  if (buf_stride == width * pixel_size)
Daniel Sabo's avatar
Daniel Sabo committed
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
    {
      gegl_memset_pattern (buf, pixel, pixel_size, width * height);
    }
  else
    {
      while (height--)
        {
          gegl_memset_pattern (buf, pixel, pixel_size, width);
          buf += buf_stride;
        }
1149 1150 1151
    }
}

Daniel Sabo's avatar
Daniel Sabo committed
1152
static void
1153 1154 1155 1156 1157 1158 1159
gegl_buffer_iterate_read_abyss_color (GeglBuffer          *buffer,
                                      const GeglRectangle *roi,
                                      const GeglRectangle *abyss,
                                      guchar              *buf,
                                      gint                 buf_stride,
                                      const Babl          *format,
                                      gint                 level,
1160 1161
                                      guchar              *color,
                                      GeglAbyssPolicy      repeat_mode)
1162
{
Daniel Sabo's avatar
Daniel Sabo committed
1163 1164
  GeglRectangle current_roi = *roi;
  gint bpp = babl_format_get_bytes_per_pixel (format);
1165

Daniel Sabo's avatar
Daniel Sabo committed
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
  if (current_roi.y < abyss->y)
    {
      /* Abyss above image */
      gint height = abyss->y - current_roi.y;
      if (current_roi.height < height)
        height = current_roi.height;
      if (color)
        fill_abyss_color (buf, current_roi.width, height, buf_stride, color, bpp);
      else
        fill_abyss_none (buf, current_roi.width, height, buf_stride, bpp);
      buf += buf_stride * height;
      current_roi.y += height;
      current_roi.height -= height;
    }
1180

Daniel Sabo's avatar
Daniel Sabo committed
1181 1182 1183 1184
  if (current_roi.height && (current_roi.y < abyss->y + abyss->height))
    {
      GeglRectangle inner_roi = current_roi;
      guchar *inner_buf       = buf;
1185

Daniel Sabo's avatar
Daniel Sabo committed
1186 1187 1188 1189 1190
      if (inner_roi.height + inner_roi.y > abyss->height + abyss->y)
        {
          /* Clamp inner_roi to the in abyss height */
          inner_roi.height -= (inner_roi.height + inner_roi.y) - (abyss->height + abyss->y);
        }
1191

Daniel Sabo's avatar
Daniel Sabo committed
1192 1193 1194 1195 1196 1197
      if (inner_roi.x < abyss->x)
        {
          /* Abyss left of image */
          gint width = abyss->x - inner_roi.x;
          if (width > inner_roi.width)
            width = inner_roi.width;
1198

Daniel Sabo's avatar
Daniel Sabo committed
1199 1200 1201 1202 1203 1204 1205 1206
          if (color)
            fill_abyss_color (inner_buf, width, inner_roi.height, buf_stride, color, bpp);
          else
            fill_abyss_none (inner_buf, width, inner_roi.height, buf_stride, bpp);
          inner_buf += width * bpp;
          inner_roi.x += width;
          inner_roi.width -= width;
        }
1207

Daniel Sabo's avatar
Daniel Sabo committed
1208 1209 1210
      if (inner_roi.width && (inner_roi.x < abyss->x + abyss->width))
        {
          gint full_width = inner_roi.width;
1211

Daniel Sabo's avatar
Daniel Sabo committed
1212
          if (inner_roi.width + inner_roi.x > abyss->width + abyss->x)
1213
            {
Daniel Sabo's avatar
Daniel Sabo committed
1214 1215
              /* Clamp inner_roi to the in abyss width */
              inner_roi.width -= (inner_roi.width + inner_roi.x) - (abyss->width + abyss->x);
1216
            }
Daniel Sabo's avatar
Daniel Sabo committed
1217

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
          if (level)
            gegl_buffer_iterate_read_fringed (buffer,
                                              &inner_roi,
                                              abyss,
                                              inner_buf,
                                              buf_stride,
                                              format,
                                              level,
                                              repeat_mode);
          else
            gegl_buffer_iterate_read_simple (buffer,
                                             &inner_roi,
                                             inner_buf,
                                             buf_stride,
                                             format,
                                             level);
Daniel Sabo's avatar
Daniel Sabo committed
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245

          inner_buf += inner_roi.width * bpp;
          inner_roi.width = full_width - inner_roi.width;
        }

      if (inner_roi.width)
        {
          /* Abyss right of image */
          if (color)
            fill_abyss_color (inner_buf, inner_roi.width, inner_roi.height, buf_stride, color, bpp);
          else
            fill_abyss_none (inner_buf, inner_roi.width, inner_roi.height, buf_stride, bpp);
1246
        }
Daniel Sabo's avatar
Daniel Sabo committed
1247 1248

      buf += inner_roi.height * buf_stride;
Daniel Sabo's avatar
Daniel Sabo committed
1249
      /* current_roi.y += inner_roi.height; */
Daniel Sabo's avatar
Daniel Sabo committed
1250 1251 1252 1253 1254 1255 1256 1257
      current_roi.height -= inner_roi.height;
    }

  if (current_roi.height)
    {
      /* Abyss below image */
      if (color)
        fill_abyss_color (buf, current_roi.width, current_roi.height, buf_stride, color, bpp);
1258
      else
Daniel Sabo's avatar
Daniel Sabo committed
1259
        fill_abyss_none (buf, current_roi.width, current_roi.height, buf_stride, bpp);
1260 1261 1262
    }
}

Daniel Sabo's avatar
Daniel Sabo committed
1263
static void
1264 1265 1266 1267 1268 1269 1270 1271
gegl_buffer_iterate_read_abyss_clamp (GeglBuffer          *buffer,
                                      const GeglRectangle *roi,
                                      const GeglRectangle *abyss,
                                      guchar              *buf,
                                      gint                 buf_stride,
                                      const Babl          *format,
                                      gint                 level)
{
Daniel Sabo's avatar
Daniel Sabo committed
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
  GeglRectangle read_output_rect;
  GeglRectangle read_input_rect;

  gint    bpp           = babl_format_get_bytes_per_pixel (format);
  gint    x_read_offset = 0;
  gint    y_read_offset = 0;
  gint    buf_offset_cols;
  gint    buf_offset_rows;
  gint    top_rows, left_cols, right_cols, bottom_rows;
  guchar *read_buf;

  if (roi->x >= abyss->x + abyss->width) /* Right of */
    x_read_offset = roi->x - (abyss->x + abyss->width) + 1;
  else if (roi->x + roi->width <= abyss->x) /* Left of */
    x_read_offset = (roi->x + roi->width) - abyss->x - 1;

  if (roi->y >= abyss->y + abyss->height) /* Above */
    y_read_offset = roi->y - (abyss->y + abyss->height) + 1;
  else if (roi->y + roi->height <= abyss->y) /* Below of */
    y_read_offset = (roi->y + roi->height) - abyss->y - 1;

  /* Intersect our shifted abyss with the roi */
  gegl_rectangle_intersect (&read_output_rect,
                            roi,
                            GEGL_RECTANGLE (abyss->x + x_read_offset,
                                            abyss->y + y_read_offset,
                                            abyss->width,
                                            abyss->height));

  /* Offset into *buf based on the intersected rect's x & y */
  buf_offset_cols = read_output_rect.x - roi->x;
  buf_offset_rows = read_output_rect.y - roi->y;
  read_buf = buf + (buf_offset_cols * bpp + buf_offset_rows * buf_stride);

  /* Convert the read output to a coresponding input */
  read_input_rect.x = read_output_rect.x - x_read_offset;
  read_input_rect.y = read_output_rect.y - y_read_offset;
  read_input_rect.width = read_output_rect.width;
  read_input_rect.height = read_output_rect.height;
1311
#if 1
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
  if (level)
    gegl_buffer_iterate_read_fringed (buffer,
                                      &read_input_rect,
                                      abyss,
                                      read_buf,
                                      buf_stride,
                                      format,
                                      level,
                                      GEGL_ABYSS_CLAMP);
  else
1322
#endif
1323 1324 1325 1326 1327 1328
    gegl_buffer_iterate_read_simple (buffer,
                                     &read_input_rect,
                                     read_buf,
                                     buf_stride,
                                     format,
                                     level);
Daniel Sabo's avatar
Daniel Sabo committed
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339

  /* All calculations are done relative to read_output_rect because it is guranteed
   * to be inside of the roi rect and none of these calculations can return a value
   * less than 0.
   */
  top_rows = read_output_rect.y - roi->y;
  left_cols = read_output_rect.x - roi->x;
  right_cols = (roi->x + roi->width) - (read_output_rect.x + read_output_rect.width);
  bottom_rows = (roi->y + roi->height) - (read_output_rect.y + read_output_rect.height);

  if (top_rows)
1340
    {
Daniel Sabo's avatar
Daniel Sabo committed
1341 1342 1343
      guchar *fill_buf = buf;
      /* Top left pixel */
      if (left_cols)
1344
        {
Daniel Sabo's avatar
Daniel Sabo committed
1345 1346 1347 1348
          guchar *src_pixel = read_buf;
          fill_abyss_color (fill_buf, left_cols, top_rows, buf_stride, src_pixel, bpp);
          fill_buf += left_cols * bpp;
        }
1349

Daniel Sabo's avatar
Daniel Sabo committed
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
      /* Top rows */
      {
        guchar *src_pixel = read_buf;
        guchar *row_fill_buf = fill_buf;
        gint byte_width = read_output_rect.width * bpp;
        gint i;
        for (i = 0; i < top_rows; ++i)
          {
            memcpy (row_fill_buf, src_pixel, byte_width);
            row_fill_buf += buf_stride;
          }
      }
1362

Daniel Sabo's avatar
Daniel Sabo committed
1363 1364 1365 1366 1367 1368 1369 1370
      fill_buf += (read_input_rect.width) * bpp;
      /* Top right pixel */
      if (right_cols)
        {
          guchar *src_pixel = read_buf + (read_input_rect.width - 1) * bpp;
          fill_abyss_color (fill_buf, right_cols, top_rows, buf_stride, src_pixel, bpp);
        }
    }
1371

Daniel Sabo's avatar
Daniel Sabo committed
1372 1373 1374 1375 1376 1377
  /* Left */
  if (left_cols)
    {
      guchar *row_fill_buf = buf + (top_rows * buf_stride);
      guchar *src_pixel = read_buf;
      gint i;