gegl-buffer-access.c 65.4 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-2008 Øyvind Kolås <pippin@gimp.org>
17 18 19 20 21 22 23 24
 */

#include "config.h"
#include <string.h>
#include <math.h>

#include <glib-object.h>
#include <glib/gprintf.h>
25
#include <gio/gio.h>
26

27
#include "gegl.h"
28
#include "gegl/gegl-debug.h"
29
#include "gegl-types-internal.h"
30 31 32 33 34 35 36 37
#include "gegl-buffer-types.h"
#include "gegl-buffer.h"
#include "gegl-buffer-private.h"
#include "gegl-tile-storage.h"
#include "gegl-utils.h"
#include "gegl-sampler-nearest.h"
#include "gegl-sampler-linear.h"
#include "gegl-sampler-cubic.h"
38
#include "gegl-sampler-nohalo.h"
39
#include "gegl-sampler-lohalo.h"
40 41
#include "gegl-buffer-index.h"
#include "gegl-tile-backend.h"
42
#include "gegl-buffer-iterator.h"
43
#include "gegl-buffer-cl-cache.h"
44

45 46
 #include "gegl-algorithms.h"

47 48
#if 0
static inline void
49 50 51 52 53
gegl_buffer_pixel_set (GeglBuffer *buffer,
                       gint        x,
                       gint        y,
                       const Babl *format,
                       guchar     *buf)
54 55
{
  gint  tile_width  = buffer->tile_storage->tile_width;
56
  gint  tile_height = buffer->tile_storage->tile_width;
57
  gint  px_size     = gegl_buffer_px_size (buffer);
58
  gint  bpx_size    = babl_format_get_bytes_per_pixel (format);
59
  const Babl *fish  = NULL;
60 61 62 63 64 65 66 67

  gint  abyss_x_total  = buffer->abyss.x + buffer->abyss.width;
  gint  abyss_y_total  = buffer->abyss.y + buffer->abyss.height;
  gint  buffer_x       = buffer->extent.x;
  gint  buffer_y       = buffer->extent.y;
  gint  buffer_abyss_x = buffer->abyss.x;
  gint  buffer_abyss_y = buffer->abyss.y;

68
  if (format != buffer->soft_format)
69
    {
70
      fish = babl_fish (buffer->soft_format, format);
71 72
    }

73 74 75 76 77 78 79 80 81 82 83
  if (!(buffer_y + y >= buffer_abyss_y &&
        buffer_y + y < abyss_y_total &&
        buffer_x + x >= buffer_abyss_x &&
        buffer_x + x < abyss_x_total))
    { /* in abyss */
      return;
    }
  else
    {
      gint      tiledy = buffer_y + buffer->shift_y + y;
      gint      tiledx = buffer_x + buffer->shift_x + x;
84

85 86 87 88
      GeglTile *tile = gegl_tile_source_get_tile ((GeglTileSource *) (buffer),
                                                  gegl_tile_indice (tiledx, tile_width),
                                                  gegl_tile_indice (tiledy, tile_height),
                                                  0);
89

90 91 92 93 94 95 96 97 98 99 100 101 102
      if (tile)
        {
          gint    offsetx = gegl_tile_offset (tiledx, tile_width);
          gint    offsety = gegl_tile_offset (tiledy, tile_height);
          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, bpx_size);
103

104 105 106 107
          gegl_tile_unlock (tile);
          gegl_tile_unref (tile);
        }
    }
108 109 110 111
  return;
}
#endif

112
static gboolean
113
gegl_buffer_in_abyss (GeglBuffer *buffer,
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
                      gint        x,
                      gint        y )
{
  gint  buffer_shift_x = buffer->shift_x;
  gint  buffer_shift_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 tiledy = y + buffer_shift_y;
  gint tiledx = x + buffer_shift_x;

  return !(tiledy >= buffer_abyss_y &&
           tiledy <  abyss_y_total  &&
           tiledx >= buffer_abyss_x &&
           tiledx <  abyss_x_total);
}

133
static inline void
134 135 136 137 138
gegl_buffer_set_pixel (GeglBuffer *buffer,
                       gint        x,
                       gint        y,
                       const Babl *format,
                       gpointer    data)
139
{
140 141 142
  if (gegl_buffer_in_abyss( buffer, x, y))
    /* Nothing to set here */
    return;
143 144

  {
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    guchar         *buf = data;
    gint    tile_width  = buffer->tile_storage->tile_width;
    gint    tile_height = buffer->tile_storage->tile_height;
    gint buffer_shift_x = buffer->shift_x;
    gint buffer_shift_y = buffer->shift_y;
    gint         tiledy = y + buffer_shift_y;
    gint         tiledx = x + buffer_shift_x;
    gint       indice_x = gegl_tile_indice (tiledx, tile_width);
    gint       indice_y = gegl_tile_indice (tiledy, tile_height);
    GeglTile      *tile = NULL;


    if (buffer->tile_storage->hot_tile &&
        buffer->tile_storage->hot_tile->x == indice_x &&
        buffer->tile_storage->hot_tile->y == indice_y)
      {
        tile = buffer->tile_storage->hot_tile;
162 163 164
      }
    else
      {
165 166 167 168 169
        _gegl_buffer_drop_hot_tile (buffer);
        tile = gegl_tile_source_get_tile ((GeglTileSource *) (buffer),
                                         indice_x, indice_y,
                                         0);
      }
170

171 172 173 174 175 176 177 178 179 180
    if (tile)
      {
        const Babl *fish  = NULL;
        gint      offsetx = gegl_tile_offset (tiledx, tile_width);
        gint      offsety = gegl_tile_offset (tiledy, tile_height);
        gint     bpx_size = babl_format_get_bytes_per_pixel (format);
        gint      px_size = babl_format_get_bytes_per_pixel (buffer->soft_format);
        guchar *tp;

        if (format != buffer->soft_format)
181
          {
182 183
            fish = babl_fish ((gpointer) format,
                              (gpointer) buffer->soft_format);
184 185
          }

186
        gegl_tile_lock (tile);
187

188 189 190 191 192 193
        tp = gegl_tile_get_data (tile) +
             (offsety * tile_width + offsetx) * px_size;
        if (fish)
          babl_process (fish, buf, tp, 1);
        else
          memcpy (tp, buf, bpx_size);
194

195 196
        gegl_tile_unlock (tile);
        buffer->tile_storage->hot_tile = tile;
197 198 199 200 201
      }
  }
}

static inline void
202 203 204 205 206 207
gegl_buffer_get_pixel (GeglBuffer     *buffer,
                       gint            x,
                       gint            y,
                       const Babl     *format,
                       gpointer        data,
                       GeglAbyssPolicy repeat_mode)
208
{
209 210
  guchar     *buf         = data;
  gint        bpx_size    = babl_format_get_bytes_per_pixel (format);
211

212 213 214 215 216 217 218
  if (gegl_buffer_in_abyss (buffer, x, y))
    { /* in abyss */
      const GeglRectangle *abyss;
      switch (repeat_mode)
      {
        case GEGL_ABYSS_CLAMP:
          abyss = gegl_buffer_get_abyss (buffer);
219
          x = CLAMP (x, abyss->x, abyss->x+abyss->width-1);
220
          y = CLAMP (y, abyss->y, abyss->y+abyss->height-1);
221 222 223 224
          break;

        case GEGL_ABYSS_LOOP:
          abyss = gegl_buffer_get_abyss (buffer);
225 226
          x = abyss->x + GEGL_REMAINDER (x - abyss->x, abyss->width);
          y = abyss->y + GEGL_REMAINDER (y - abyss->y, abyss->height);
227 228 229 230 231 232 233 234 235 236 237
          break;

        case GEGL_ABYSS_BLACK:
          {
            gfloat color[4] = {0.0, 0.0, 0.0, 1.0};
            babl_process (babl_fish (babl_format ("RGBA float"), format),
                          color,
                          buf,
                          1);
            return;
          }
238

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
        case GEGL_ABYSS_WHITE:
          {
            gfloat color[4] = {1.0, 1.0, 1.0, 1.0};
            babl_process (babl_fish (babl_format ("RGBA float"),
                                     format),
                          color,
                          buf,
                          1);
            return;
          }

        default:
        case GEGL_ABYSS_NONE:
          memset (buf, 0x00, bpx_size);
          return;
      }
255 256 257
    }

  {
258 259 260 261 262 263 264
    gint  tile_width     = buffer->tile_storage->tile_width;
    gint  tile_height    = buffer->tile_storage->tile_height;

    gint  buffer_shift_x = buffer->shift_x;
    gint  buffer_shift_y = buffer->shift_y;
    gint  px_size        = babl_format_get_bytes_per_pixel (buffer->soft_format);

265 266 267
    gint tiledy = y + buffer_shift_y;
    gint tiledx = x + buffer_shift_x;

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    gint      indice_x = gegl_tile_indice (tiledx, tile_width);
    gint      indice_y = gegl_tile_indice (tiledy, tile_height);
    GeglTile *tile     = NULL;

    const Babl *fish   = NULL;

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

    if (buffer->tile_storage->hot_tile &&
        buffer->tile_storage->hot_tile->x == indice_x &&
        buffer->tile_storage->hot_tile->y == indice_y)
      {
        tile = buffer->tile_storage->hot_tile;
285 286 287
      }
    else
      {
288 289 290 291 292
        _gegl_buffer_drop_hot_tile (buffer);
        tile = gegl_tile_source_get_tile ((GeglTileSource *) (buffer),
                                          indice_x, indice_y,
                                          0);
      }
293

294 295 296 297 298 299 300 301
    if (tile)
      {
        gint    offsetx = gegl_tile_offset (tiledx, tile_width);
        gint    offsety = gegl_tile_offset (tiledy, tile_height);
        guchar *tp      = gegl_tile_get_data (tile) +
                          (offsety * tile_width + offsetx) * px_size;
        if (fish)
          babl_process (fish, tp, buf, 1);
302
        else
303
          memcpy (buf, tp, px_size);
304

305 306
        /*gegl_tile_unref (tile);*/
        buffer->tile_storage->hot_tile = tile;
307 308 309 310 311 312 313 314 315 316
      }
  }
}

/* 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)
{
317 318
  GeglTileBackend *backend;

319
  g_return_if_fail (GEGL_IS_BUFFER (buffer));
320
  backend = gegl_buffer_backend (buffer);
321

322
  _gegl_buffer_drop_hot_tile (buffer);
323 324

  if ((GeglBufferHeader*)(backend->priv->header))
325
    {
326 327 328 329 330
      GeglBufferHeader* header = backend->priv->header;
      header->x = buffer->extent.x;
      header->y = buffer->extent.y;
      header->width =buffer->extent.width;
      header->height =buffer->extent.height;
331 332
    }

333 334
  gegl_tile_source_command (GEGL_TILE_SOURCE (buffer),
                            GEGL_TILE_FLUSH, 0,0,0,NULL);
335 336 337
}


338

339
static inline void
340
gegl_buffer_iterate_write (GeglBuffer          *buffer,
341
                           const GeglRectangle *roi,
342 343 344 345
                           guchar              *buf,
                           gint                 rowstride,
                           const Babl          *format,
                           gint                 level)
346
{
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
  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;
368
  const Babl *fish;
369

370 371 372 373 374 375 376 377
  /* 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;
    }
378

379 380 381 382 383 384 385 386 387 388 389 390 391
  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;

  buf_stride = width * bpx_size;
  if (rowstride != GEGL_AUTO_ROWSTRIDE)
    buf_stride = rowstride;

392
  if (format == buffer->soft_format)
393 394 395 396
    {
      fish = NULL;
    }
  else
397 398 399 400
      fish = babl_fish ((gpointer) format,
                        (gpointer) buffer->soft_format);

  while (bufy < height)
401
    {
402 403 404 405 406
      gint tiledy  = buffer_y + bufy;
      gint offsety = gegl_tile_offset (tiledy, tile_height);
      gint bufx    = 0;

      while (bufx < width)
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
          gint      tiledx  = buffer_x + bufx;
          gint      offsetx = gegl_tile_offset (tiledx, tile_width);
          gint      y       = bufy;
          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;

          tile = gegl_tile_source_get_tile ((GeglTileSource *) (buffer),
                                            gegl_tile_indice (tiledx, tile_width),
                                            gegl_tile_indice (tiledy, tile_height),
                                            level);

          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;

          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,
                                    pixels - lskip - rskip);
                    }

                  tp += tile_stride;
                  bp += buf_stride;
                }
            }
          else
            {
              for (row = offsety;
                   row < tile_height && y < height;
                   row++, y++)
                {

                  if (buffer_y + y >= buffer_abyss_y &&
                      buffer_y + y < abyss_y_total)
                    {

                      memcpy (tp + lskip * px_size, bp + lskip * px_size,
                              (pixels - lskip - rskip) * px_size);
                    }

                  tp += tile_stride;
                  bp += buf_stride;
                }
            }

          gegl_tile_unlock (tile);
          gegl_tile_unref (tile);
          bufx += (tile_width - offsetx);
496
        }
497 498 499 500 501 502 503 504 505 506 507 508
      bufy += (tile_height - offsety);
    }
}

static inline void
gegl_buffer_iterate_read_simple (GeglBuffer          *buffer,
                                 const GeglRectangle *roi,
                                 guchar              *buf,
                                 gint                 buf_stride,
                                 const Babl          *format,
                                 gint                 level)
{
509 510 511 512 513 514 515 516 517 518 519
  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;
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535

  const Babl *fish;

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

  while (bufy < height)
    {
      gint tiledy  = buffer_y + bufy;
      gint offsety = gegl_tile_offset (tiledy, tile_height);
      gint bufx    = 0;

      while (bufx < width)
536
        {
537 538 539 540 541 542 543 544 545
          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)
546
            pixels = width - bufx;
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
          else
            pixels = tile_width - offsetx;

          tile = gegl_tile_source_get_tile ((GeglTileSource *) (buffer),
                                            gegl_tile_indice (tiledx, tile_width),
                                            gegl_tile_indice (tiledy, tile_height),
                                            level);

          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;
          for (row = offsety;
               row < tile_height && y < height;
               row++, y++)
            {
              if (fish)
                babl_process (fish, tp, bp, pixels);
              else
                memcpy (bp, tp, pixels * px_size);

              tp += tile_stride;
              bp += buf_stride;
            }

          gegl_tile_unref (tile);
          bufx += (tile_width - offsetx);
581
        }
582
      bufy += (tile_height - offsety);
583
    }
584 585 586 587 588 589 590 591 592 593 594
}

static inline void
gegl_buffer_iterate_read_abyss_none (GeglBuffer          *buffer,
                                     const GeglRectangle *roi,
                                     const GeglRectangle *abyss,
                                     guchar              *buf,
                                     gint                 buf_stride,
                                     const Babl          *format,
                                     gint                 level)
{
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
  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;

  gint buffer_abyss_x = abyss->x;
  gint buffer_abyss_y = abyss->y;
  gint abyss_x_total  = buffer_abyss_x + abyss->width;
  gint abyss_y_total  = buffer_abyss_y + abyss->height;
611 612 613 614 615 616 617 618

  const Babl *fish;

  if (format == buffer->soft_format)
    fish = NULL;
  else
    fish = babl_fish ((gpointer) buffer->soft_format,
                      (gpointer) format);
619 620 621 622 623 624 625 626 627 628 629

  while (bufy < height)
    {
      gint tiledy  = buffer_y + bufy;
      gint offsety = gegl_tile_offset (tiledy, tile_height);

      gint bufx    = 0;

      if (!(buffer_y + bufy + (tile_height) >= buffer_abyss_y &&
            buffer_y + bufy < abyss_y_total))
        { /* entire row of tiles is in abyss */
630 631
          gint    row;
          gint    y  = bufy;
632
          guchar *bp = buf + bufy * buf_stride;
633

634 635 636 637
          for (row = offsety;
               row < tile_height && y < height;
               row++, y++)
            {
638
              memset (bp, 0x00, width * bpx_size);
639
              bp += buf_stride;
640 641 642 643 644 645 646 647 648 649 650 651 652 653
            }
        }
      else

        while (bufx < width)
          {
            gint    tiledx  = buffer_x + bufx;
            gint    offsetx = gegl_tile_offset (tiledx, tile_width);
            gint    pixels;
            guchar *bp;

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

            if (width + offsetx - bufx < tile_width)
654
              pixels = width - bufx;
655 656 657 658 659 660
            else
              pixels = tile_width - offsetx;

            if (!(buffer_x + bufx + tile_width >= buffer_abyss_x &&
                  buffer_x + bufx < abyss_x_total))
              { /* entire tile is in abyss */
661 662
                gint row;
                gint y = bufy;
663

664 665 666 667 668 669
                for (row = offsety;
                     row < tile_height && y < height;
                     row++, y++)
                  {
                    memset (bp, 0x00, pixels * bpx_size);
                    bp += buf_stride;
670 671 672 673 674
                  }
              }
            else
              {
                guchar   *tile_base, *tp;
675
                gint      row, y, lskip, rskip;
676
                GeglTile *tile = gegl_tile_source_get_tile ((GeglTileSource *) (buffer),
677 678 679
                                                            gegl_tile_indice (tiledx, tile_width),
                                                            gegl_tile_indice (tiledy, tile_height),
                                                            level);
680

681
                lskip = (buffer_abyss_x) - (buffer_x + bufx);
682
                /* gap between left side of tile, and abyss */
683
                rskip = (buffer_x + bufx + pixels) - abyss_x_total;
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
                /* 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;
                  }

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

705 706 707 708
                y = bufy;
                for (row = offsety;
                     row < tile_height && y < height;
                     row++, y++)
709
                  {
710 711
                    if (buffer_y + y >= buffer_abyss_y &&
                        buffer_y + y < abyss_y_total)
712
                      {
713 714 715 716
                        if (fish)
                          babl_process (fish, tp, bp, pixels);
                        else
                          memcpy (bp, tp, pixels * px_size);
717 718 719
                      }
                    else
                      {
720 721
                        /* entire row in abyss */
                        memset (bp, 0x00, pixels * bpx_size);
722 723
                      }

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
                    /* left hand zeroing of abyss in tile */
                    if (lskip)
                      memset (bp, 0x00, bpx_size * lskip);

                    /* right side zeroing of abyss in tile */
                    if (rskip)
                      memset (bp + (pixels - rskip) * bpx_size, 0x00, bpx_size * rskip);

                    tp += tile_stride;
                    bp += buf_stride;
                  }
                gegl_tile_unref (tile);
              }
            bufx += (tile_width - offsetx);
          }
      bufy += (tile_height - offsety);
    }
}

static inline void
gegl_buffer_iterate_read_abyss_color (GeglBuffer          *buffer,
                                      const GeglRectangle *roi,
                                      const GeglRectangle *abyss,
                                      guchar              *buf,
                                      gint                 buf_stride,
                                      const Babl          *format,
                                      gint                 level,
                                      guchar              *color)
{
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
  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;

  gint buffer_abyss_x = abyss->x;
  gint buffer_abyss_y = abyss->y;
  gint abyss_x_total  = buffer_abyss_x + abyss->width;
  gint abyss_y_total  = buffer_abyss_y + abyss->height;
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788

  const Babl *fish;

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

  while (bufy < height)
    {
      gint tiledy  = buffer_y + bufy;
      gint offsety = gegl_tile_offset (tiledy, tile_height);
      gint bufx    = 0;

      if (!(buffer_y + bufy + (tile_height) >= buffer_abyss_y &&
            buffer_y + bufy < abyss_y_total))
        { /* entire row of tiles is in abyss */
          gint    row;
          gint    y  = bufy;
789
          guchar *bp = buf + bufy * buf_stride;
790 791 792 793 794

          for (row = offsety;
               row < tile_height && y < height;
               row++, y++)
            {
795
              gegl_memset_pattern (bp, color, bpx_size, width);
796 797 798 799 800 801 802 803 804 805 806 807 808 809
              bp += buf_stride;
            }
        }
      else
        while (bufx < width)
          {
            gint    tiledx  = buffer_x + bufx;
            gint    offsetx = gegl_tile_offset (tiledx, tile_width);
            gint    pixels;
            guchar *bp;

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

            if (width + offsetx - bufx < tile_width)
810
              pixels = width - bufx;
811 812 813 814 815 816 817 818 819 820 821 822 823
            else
              pixels = tile_width - offsetx;

            if (!(buffer_x + bufx + tile_width >= buffer_abyss_x &&
                  buffer_x + bufx < abyss_x_total))
              { /* entire tile is in abyss */
                gint row;
                gint y = bufy;

                for (row = offsety;
                     row < tile_height && y < height;
                     row++, y++)
                  {
824
                    gegl_memset_pattern (bp, color, bpx_size, pixels);
825
                    bp += buf_stride;
826
                  }
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
              }
            else
              {
                guchar   *tile_base, *tp;
                gint      row, y, lskip, rskip;
                GeglTile *tile = gegl_tile_source_get_tile ((GeglTileSource *) (buffer),
                                                            gegl_tile_indice (tiledx, tile_width),
                                                            gegl_tile_indice (tiledy, tile_height),
                                                            level);

                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)
852
                  {
853 854 855 856
                    g_warning ("didn't get tile, trying to continue");
                    bufx += (tile_width - offsetx);
                    continue;
                  }
857

858 859 860 861 862 863 864 865 866 867
                tile_base = gegl_tile_get_data (tile);
                tp        = ((guchar *) tile_base) + (offsety * tile_width + offsetx) * px_size;

                y = bufy;
                for (row = offsety;
                     row < tile_height && y < height;
                     row++, y++)
                  {
                    if (buffer_y + y >= buffer_abyss_y &&
                        buffer_y + y < abyss_y_total)
868
                      {
869 870
                        if (fish)
                          babl_process (fish, tp, bp, pixels);
871
                        else
872
                          memcpy (bp, tp, pixels * px_size);
873
                      }
874 875 876
                    else
                      {
                        /* entire row in abyss */
877
                        gegl_memset_pattern (bp, color, bpx_size, pixels);
878 879 880 881 882
                      }

                    /* left hand zeroing of abyss in tile */
                    if (lskip)
                      {
883
                        gegl_memset_pattern (bp, color, bpx_size, lskip);
884 885 886 887 888
                      }

                    /* right side zeroing of abyss in tile */
                    if (rskip)
                      {
889 890
                        guchar *bp_right = bp + (pixels - rskip) * bpx_size;
                        gegl_memset_pattern (bp_right, color, bpx_size, rskip);
891 892 893 894
                      }

                    tp += tile_stride;
                    bp += buf_stride;
895
                  }
896
                gegl_tile_unref (tile);
897 898 899 900 901 902 903
              }
            bufx += (tile_width - offsetx);
          }
      bufy += (tile_height - offsety);
    }
}

904 905 906 907 908 909 910 911 912
static inline void
gegl_buffer_iterate_read_abyss_clamp (GeglBuffer          *buffer,
                                      const GeglRectangle *roi,
                                      const GeglRectangle *abyss,
                                      guchar              *buf,
                                      gint                 buf_stride,
                                      const Babl          *format,
                                      gint                 level)
{
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
  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;

  gint buffer_abyss_x = abyss->x;
  gint buffer_abyss_y = abyss->y;
  gint abyss_x_total  = buffer_abyss_x + abyss->width;
  gint abyss_y_total  = buffer_abyss_y + abyss->height;
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972

  const Babl *fish;

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

  while (bufy < height)
    {
      gint     tiledy       = CLAMP (buffer_y + bufy, buffer_abyss_y, abyss_y_total - 1);
      gint     offsety      = gegl_tile_offset (tiledy, tile_height);
      gint     bufx         = 0;
      gboolean row_in_abyss = !(buffer_y + bufy + (tile_height) >= buffer_abyss_y &&
                                buffer_y + bufy < abyss_y_total);

      while (bufx < width)
        {
          gint      tiledx  = CLAMP (buffer_x + bufx, buffer_abyss_x, abyss_x_total - 1);
          gint      offsetx = gegl_tile_offset (tiledx, tile_width);
          gint      row, y, pixels, lskip, rskip;
          guchar   *bp, *tile_base, *tp;
          GeglTile *tile;

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

          tile = gegl_tile_source_get_tile ((GeglTileSource *) (buffer),
                                            gegl_tile_indice (tiledx, tile_width),
                                            gegl_tile_indice (tiledy, tile_height),
                                            level);

          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;
          if (tiledx != buffer_x + bufx)
973 974
            { /* x was clamped. Copy a single color since x remains clamped in
               this iteration. */
975
              guchar *color = alloca (bpx_size);
976

977
              /* gap between current column and left side of abyss rect */
978 979 980
              lskip = (buffer_abyss_x) - (buffer_x + bufx);
              /* gap between current column and end of roi */
              rskip = width - bufx;
981
              pixels = (lskip > 0 && lskip < width) ? lskip : rskip;
982 983

              if (row_in_abyss)
984
                { /* y remains clamped in this iteration so don't change the color */
985 986 987 988 989 990 991 992 993
                  if (fish)
                    babl_process (fish, tp, color, 1);
                  else
                    memcpy (color, tp, px_size);

                  for (row = 0;
                       row < tile_height && y < height;
                       row++, y++)
                    {
994
                      gegl_memset_pattern (bp, color, bpx_size, pixels);
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
                      bp += buf_stride;
                    }
                }
              else
                {
                  for (row = offsety;
                       row < tile_height && y < height;
                       row++, y++)
                    {
                      if (fish)
                        babl_process (fish, tp, color, 1);
                      else
                        memcpy (color, tp, px_size);

1009
                      gegl_memset_pattern (bp, color, bpx_size, pixels);
1010

1011 1012
                      if (buffer_y + y >= buffer_abyss_y &&
                          buffer_y + y < abyss_y_total - 1)
1013 1014 1015 1016 1017 1018 1019 1020
                        tp += tile_stride;
                      bp += buf_stride;
                    }
                }
            }
          else
            {
              if (width + offsetx - bufx < tile_width)
1021
                pixels = width - bufx;
1022 1023 1024
              else
                pixels = tile_width - offsetx;

1025
              /* gap between current column and right side of abyss rect */
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
              rskip = abyss_x_total - (buffer_x + bufx);
              if (rskip > 0 && rskip < pixels)
                pixels = rskip;

              for (row = (row_in_abyss) ? 0 : offsety;
                   row < tile_height && y < height;
                   row++, y++)
                {
                  if (fish)
                    babl_process (fish, tp, bp, pixels);
                  else
                    memcpy (bp, tp, pixels * px_size);

1039 1040
                  if (buffer_y + y >= buffer_abyss_y &&
                      buffer_y + y < abyss_y_total - 1)
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
                    tp += tile_stride;
                  bp += buf_stride;
                }
            }
          gegl_tile_unref (tile);
          bufx += pixels;
        }
      if (row_in_abyss)
        bufy += tile_height;
      else
        bufy += (tile_height - offsety);
    }
}

static inline void
gegl_buffer_iterate_read_abyss_loop (GeglBuffer          *buffer,
                                     const GeglRectangle *roi,
                                     const GeglRectangle *abyss,
                                     guchar              *buf,
                                     gint                 buf_stride,
                                     const Babl          *format,
                                     gint                 level)
{
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
  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;

  gint buffer_abyss_x = abyss->x;
  gint buffer_abyss_y = abyss->y;
  gint abyss_x_total  = buffer_abyss_x + abyss->width;
  gint abyss_y_total  = buffer_abyss_y + abyss->height;
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090

  const Babl *fish;

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

  while (bufy < height)
    {
1091
      gint tiledy  = buffer_abyss_y +
1092 1093
        GEGL_REMAINDER (buffer_y + bufy - buffer_abyss_y, abyss->height);
      gint offsety = gegl_tile_offset (tiledy, tile_height);
1094
      gint bufx    = 0;
1095 1096
      gint rows, topskip, bottomskip;

1097
      if (height + offsety - bufy < tile_height)
1098 1099 1100 1101
        rows = height - bufy;
      else
        rows = tile_height - offsety;

1102
      /* gap between current row and top of abyss rect */
1103
      topskip = buffer_abyss_y - tiledy;
1104
      /* gap between current row and bottom of abyss rect */
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
      bottomskip = abyss_y_total - tiledy;

      if (topskip > 0 && topskip < rows)
        rows = topskip;
      else if (bottomskip > 0 && bottomskip < rows)
        rows = bottomskip;

      while (bufx < width)
        {
          gint      tiledx  = buffer_abyss_x +
            GEGL_REMAINDER (buffer_x + bufx - buffer_abyss_x, abyss->width);
          gint      offsetx = gegl_tile_offset (tiledx, tile_width);
          guchar   *bp, *tile_base, *tp;
          gint      pixels, row, y, lskip, rskip;
          GeglTile *tile;

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

          if (width + offsetx - bufx < tile_width)
1124
            pixels = width - bufx;
1125 1126 1127 1128 1129 1130 1131 1132
          else
            pixels = tile_width - offsetx;

          tile = gegl_tile_source_get_tile ((GeglTileSource *) (buffer),
                                            gegl_tile_indice (tiledx, tile_width),
                                            gegl_tile_indice (tiledy, tile_height),
                                            level);

1133
          /* gap between current column and left side of abyss rect */
1134
          lskip = buffer_abyss_x - tiledx;
1135
          /* gap between current column and right side of abyss rect */
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
          rskip = abyss_x_total - tiledx;

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

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

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

          y = bufy;
          for (row = 0;
               row < rows && y < height;
               row++, y++)
            {
              if (fish)
                babl_process (fish, tp, bp, pixels);
              else
                memcpy (bp, tp, pixels * px_size);

              tp += tile_stride;
              bp += buf_stride;
            }

          gegl_tile_unref (tile);
          bufx += pixels;
        }
      bufy += rows;
    }
}

static inline void
gegl_buffer_iterate_read_dispatch (GeglBuffer          *buffer,
                                   const GeglRectangle *roi,
                                   guchar              *buf,
                                   gint                 rowstride,
                                   const Babl          *format,
                                   gint                 level,
                                   GeglAbyssPolicy      repeat_mode)
{
1183
  GeglRectangle abyss          = buffer->abyss;
1184 1185
  GeglRectangle abyss_factored = abyss;
  GeglRectangle roi_factored   = *roi;
1186 1187 1188 1189 1190
  const gint    factor         = 1 << level;
  const gint    x1 = buffer->shift_x + abyss.x;
  const gint    y1 = buffer->shift_y + abyss.y;
  const gint    x2 = buffer->shift_x + abyss.x + abyss.width;
  const gint    y2 = buffer->shift_y + abyss.y + abyss.height;
1191

1192 1193 1194 1195
  abyss_factored.x      = (x1 + (x1 < 0 ? 1 - factor : 0)) / factor;
  abyss_factored.y      = (y1 + (y1 < 0 ? 1 - factor : 0)) / factor;
  abyss_factored.width  = (x2 + (x2 < 0 ? 0 : factor - 1)) / factor - abyss_factored.x;
  abyss_factored.height = (y2 + (y2 < 0 ? 0 : factor - 1)) / factor - abyss_factored.y;
1196 1197 1198 1199 1200 1201 1202

  roi_factored.x       = (buffer->shift_x + roi_factored.x) / factor;
  roi_factored.y       = (buffer->shift_y + roi_factored.y) / factor;
  roi_factored.width  /= factor;
  roi_factored.height /= factor;

  if (rowstride == GEGL_AUTO_ROWSTRIDE)
1203
    rowstride = roi_factored.width * babl_format_get_bytes_per_pixel (format);
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213

  if (gegl_rectangle_contains (&abyss, roi))
    {
      gegl_buffer_iterate_read_simple (buffer, &roi_factored, buf, rowstride, format, level);
    }
  else if (repeat_mode == GEGL_ABYSS_NONE)
    {
      gegl_buffer_iterate_read_abyss_none (buffer, &roi_factored, &abyss_factored,
                                           buf, rowstride, format, level);
    }
1214
  else if (repeat_mode == GEGL_ABYSS_WHITE)
1215 1216
    {
      guchar color[128];
1217
      gfloat in_color[] = {1.0f, 1.0f, 1.0f, 1.0f};
1218 1219

      babl_process (babl_fish (babl_format ("RGBA float"), format),
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
                    in_color, color, 1);

      gegl_buffer_iterate_read_abyss_color (buffer, &roi_factored, &abyss_factored,
                                            buf, rowstride, format, level, color);
    }
  else if (repeat_mode == GEGL_ABYSS_BLACK)
    {
      guchar color[128];
      gfloat  in_color[] = {0.0f, 0.0f, 0.0f, 1.0f};

      babl_process (babl_fish (babl_format ("RGBA float"), format),
                    in_color, color, 1);
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247

      gegl_buffer_iterate_read_abyss_color (buffer, &roi_factored, &abyss_factored,
                                            buf, rowstride, format, level, color);
    }
  else if (repeat_mode == GEGL_ABYSS_CLAMP)
    {
      gegl_buffer_iterate_read_abyss_clamp (buffer, &roi_factored, &abyss_factored,
                                            buf, rowstride, format, level);
    }
  else
    {
      gegl_buffer_iterate_read_abyss_loop (buffer, &roi_factored, &abyss_factored,
                                           buf, rowstride, format, level);
    }
}

1248
void
1249 1250 1251
gegl_buffer_set_unlocked (GeglBuffer          *buffer,
                          const GeglRectangle *rect,
                          const Babl          *format,
1252
                          const void          *src,
1253
                          gint                 rowstride)
1254 1255 1256 1257 1258 1259 1260 1261
{
    gegl_buffer_set_unlocked_no_notify(buffer, rect, format, src, rowstride);
    gegl_buffer_emit_changed_signal(buffer, rect);
}


void
gegl_buffer_set_unlocked_no_notify (GeglBuffer          *buffer,
Daniel Sabo's avatar
Daniel Sabo committed
1262 1263 1264 1265
                                    const GeglRectangle *rect,
                                    const Babl          *format,
                                    const void          *src,
                                    gint                 rowstride)
1266 1267
{
  if (format == NULL)
1268
    format = buffer->soft_format;
1269

Victor Oliveira's avatar
Victor Oliveira committed
1270 1271 1272 1273 1274
  if (gegl_cl_is_accelerated ())
    {
      gegl_buffer_cl_cache_flush (buffer, rect);
    }

1275
#if 0 /* XXX: not thread safe */
1276 1277
  if (rect && rect->width == 1 && rect->height == 1) /* fast path */
    {
1278
      gegl_buffer_set_pixel (buffer, rect->x, rect->y, format, src);
1279 1280
    }
  else
1281
#endif
1282

Daniel Sabo's avatar
Daniel Sabo committed
1283 1284 1285
  gegl_buffer_iterate_write (buffer, rect, (void *) src, rowstride, format, 0);

  if (gegl_buffer_is_shared (buffer))
1286 1287 1288
    {
      gegl_buffer_flush (buffer);
    }
1289 1290
}

1291 1292 1293
void
gegl_buffer_set (GeglBuffer          *buffer,
                 const GeglRectangle *rect,
1294
                 gint                 level,
1295
                 const Babl          *format,
1296
                 const void          *src,
1297 1298 1299 1300 1301 1302 1303 1304 1305
                 gint                 rowstride)
{
  g_return_if_fail (GEGL_IS_BUFFER (buffer));

  gegl_buffer_lock (buffer);
  gegl_buffer_set_unlocked (buffer, rect, format, src, rowstride);
  gegl_buffer_unlock (buffer);
}

1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
/* Expand roi by scale so it uncludes all pixels needed
 * to satisfy a gegl_buffer_get() call at level 0.
 */
GeglRectangle
_gegl_get_required_for_scale (const Babl          *format,
                              const GeglRectangle *roi,
                              gdouble              scale)
{
  if (GEGL_FLOAT_EQUAL (scale, 1.0))
    return *roi;
  else
    {
1318 1319 1320 1321
      gint x1 = floor (roi->x / scale + GEGL_SCALE_EPSILON);
      gint x2 = ceil ((roi->x + roi->width) / scale - GEGL_SCALE_EPSILON);
      gint y1 = floor (roi->y / scale + GEGL_SCALE_EPSILON);
      gint y2 = ceil ((roi->y + roi->height) / scale - GEGL_SCALE_EPSILON);
1322 1323 1324

      gint pad = (1.0 / scale > 1.0) ? ceil (1.0 / scale) : 1;

1325
      if (scale < 1.0)
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
        {
          return *GEGL_RECTANGLE (x1 - pad,
                                  y1 - pad,
                                  x2 - x1 + 2 * pad,
                                  y2 - y1 + 2 * pad);
        }
      else
        {
          return *GEGL_RECTANGLE (x1,
                                  y1,
                                  x2 - x1,
                                  y2 - y1);
        }
      }
}
1341

1342
void
1343 1344 1345 1346 1347
gegl_buffer_get_unlocked (GeglBuffer          *buffer,
                          gdouble              scale,
                          const GeglRectangle *rect,
                          const Babl          *format,
                          gpointer             dest_buf,
1348 1349
                          gint                 rowstride,
                          GeglAbyssPolicy      repeat_mode)
1350
{
1351
  g_return_if_fail (scale > 0.0);
1352 1353

  if (format == NULL)
1354
    format = buffer->soft_format;
1355

1356 1357
#if 0
  /* not thread-safe */
1358 1359 1360 1361 1362
  if (scale == 1.0 &&
      rect &&
      rect->width == 1 &&
      rect->height == 1)  /* fast path */
    {
1363
      gegl_buffer_get_pixel (buffer, rect->x, rect->y, format, dest_buf, repeat_mode);
1364 1365
      return;
    }
1366
#endif