gimpdisplayshell-render.c 21.3 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
Sven Neumann's avatar
Sven Neumann committed
18
19
20

#include "config.h"

Elliot Lee's avatar
Elliot Lee committed
21
22
#include <stdlib.h>
#include <string.h>
23

Sven Neumann's avatar
Sven Neumann committed
24
25
#include <gtk/gtk.h>

26
27
#include "libgimpwidgets/gimpwidgets.h"

28
#include "display-types.h"
Sven Neumann's avatar
Sven Neumann committed
29

Michael Natterer's avatar
Michael Natterer committed
30
31
32
#include "base/tile-manager.h"
#include "base/tile.h"

33
#include "core/gimp.h"
34
#include "core/gimpimage.h"
35
#include "core/gimpimage-colormap.h"
36
#include "core/gimpimage-projection.h"
37

38
#include "gimpcanvas.h"
39
#include "gimpdisplay.h"
Michael Natterer's avatar
Michael Natterer committed
40
#include "gimpdisplayshell.h"
41
#include "gimpdisplayshell-filter.h"
42
#include "gimpdisplayshell-render.h"
43

Elliot Lee's avatar
Elliot Lee committed
44
45

typedef struct _RenderInfo  RenderInfo;
46
47

typedef void (* RenderFunc) (RenderInfo *info);
Elliot Lee's avatar
Elliot Lee committed
48
49
50

struct _RenderInfo
{
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  GimpDisplayShell *shell;
  TileManager      *src_tiles;
  guint            *alpha;
  guchar           *scale;
  guchar           *src;
  guchar           *dest;
  gint              x, y;
  gint              w, h;
  gfloat            scalex;
  gfloat            scaley;
  gint              src_x, src_y;
  gint              src_bpp;
  gint              dest_bpp;
  gint              dest_bpl;
  gint              dest_width;
  gint              byte_order;
Elliot Lee's avatar
Elliot Lee committed
67
68
69
};


70
static void   render_setup_notify (gpointer    config,
71
72
73
74
                                   GParamSpec *param_spec,
                                   Gimp       *gimp);


Elliot Lee's avatar
Elliot Lee committed
75
/*  accelerate transparency of image scaling  */
76
77
guchar *render_check_buf         = NULL;
guchar *render_empty_buf         = NULL;
78
guchar *render_white_buf         = NULL;
79
guchar *render_temp_buf          = NULL;
80

81
82
guchar *render_blend_dark_check  = NULL;
guchar *render_blend_light_check = NULL;
83
guchar *render_blend_white       = NULL;
84
85
86
87
88
89


static guchar *tile_buf           = NULL;
static guint   check_mod          = 0;
static guint   check_shift        = 0;
static guint   tile_shift         = 0;
Elliot Lee's avatar
Elliot Lee committed
90
91
92
93
static guchar  check_combos[6][2] =
{
  { 204, 255 },
  { 102, 153 },
94
  {   0,  51 },
Elliot Lee's avatar
Elliot Lee committed
95
96
  { 255, 255 },
  { 127, 127 },
97
  {   0,   0 }
Elliot Lee's avatar
Elliot Lee committed
98
99
100
101
};


void
102
103
104
105
render_init (Gimp *gimp)
{
  g_return_if_fail (GIMP_IS_GIMP (gimp));

106
  g_signal_connect (gimp->config, "notify::transparency-size",
107
108
                    G_CALLBACK (render_setup_notify),
                    gimp);
109
  g_signal_connect (gimp->config, "notify::transparency-type",
110
111
112
                    G_CALLBACK (render_setup_notify),
                    gimp);

113
  render_setup_notify (gimp->config, NULL, gimp);
114
115
116
117
118
119
120
}

void
render_exit (Gimp *gimp)
{
  g_return_if_fail (GIMP_IS_GIMP (gimp));

121
  g_signal_handlers_disconnect_by_func (gimp->config,
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
                                        render_setup_notify,
                                        gimp);

  if (tile_buf)
    {
      g_free (tile_buf);
      tile_buf = NULL;
    }

  if (render_blend_dark_check)
    {
      g_free (render_blend_dark_check);
      render_blend_dark_check = NULL;
    }

  if (render_blend_light_check)
    {
      g_free (render_blend_light_check);
      render_blend_light_check = NULL;
    }

143
144
145
146
147
148
  if (render_blend_white)
    {
      g_free (render_blend_white);
      render_blend_white = NULL;
    }

149
150
151
152
153
154
155
156
157
158
159
160
  if (render_check_buf)
    {
      g_free (render_check_buf);
      render_check_buf = NULL;
    }

  if (render_empty_buf)
    {
      g_free (render_empty_buf);
      render_empty_buf = NULL;
    }

161
162
163
164
165
166
  if (render_white_buf)
    {
      g_free (render_white_buf);
      render_white_buf = NULL;
    }

167
168
169
170
171
172
173
174
175
  if (render_temp_buf)
    {
      g_free (render_temp_buf);
      render_temp_buf = NULL;
    }
}


static void
176
render_setup_notify (gpointer    config,
177
178
                     GParamSpec *param_spec,
                     Gimp       *gimp)
Elliot Lee's avatar
Elliot Lee committed
179
{
180
181
182
  GimpCheckType check_type;
  GimpCheckSize check_size;
  gint          i, j;
183

184
185
186
187
  g_object_get (config,
                "transparency-type", &check_type,
                "transparency-size", &check_size,
                NULL);
Elliot Lee's avatar
Elliot Lee committed
188
189
190
191

  /*  based on the tile size, determine the tile shift amount
   *  (assume here that tile_height and tile_width are equal)
   */
Daniel Egger's avatar
Daniel Egger committed
192
193
194
  tile_shift = 0;
  while ((1 << tile_shift) < TILE_WIDTH)
    tile_shift++;
Elliot Lee's avatar
Elliot Lee committed
195
196

  /*  allocate a buffer for arranging information from a row of tiles  */
197
  if (! tile_buf)
Sven Neumann's avatar
Sven Neumann committed
198
199
    tile_buf = g_new (guchar,
                      GIMP_DISPLAY_SHELL_RENDER_BUF_WIDTH * MAX_CHANNELS);
Elliot Lee's avatar
Elliot Lee committed
200

201
202
203
204
  if (! render_blend_dark_check)
    render_blend_dark_check = g_new (guchar, 65536);
  if (! render_blend_light_check)
    render_blend_light_check = g_new (guchar, 65536);
205
206
  if (! render_blend_white)
    render_blend_white = g_new (guchar, 65536);
Elliot Lee's avatar
Elliot Lee committed
207
208
209
210

  for (i = 0; i < 256; i++)
    for (j = 0; j < 256; j++)
      {
211
	render_blend_dark_check [(i << 8) + j] = (guchar)
Elliot Lee's avatar
Elliot Lee committed
212
	  ((j * i + check_combos[check_type][0] * (255 - i)) / 255);
213
	render_blend_light_check [(i << 8) + j] = (guchar)
Elliot Lee's avatar
Elliot Lee committed
214
	  ((j * i + check_combos[check_type][1] * (255 - i)) / 255);
215
216
217

	render_blend_white [(i << 8) + j] = (guchar)
	  ((j * i + 255 * (255 - i)) / 255);
Elliot Lee's avatar
Elliot Lee committed
218
219
220
221
      }

  switch (check_size)
    {
222
    case GIMP_SMALL_CHECKS:
223
      check_mod   = 0x3;
Elliot Lee's avatar
Elliot Lee committed
224
225
      check_shift = 2;
      break;
226
    case GIMP_MEDIUM_CHECKS:
227
      check_mod   = 0x7;
Elliot Lee's avatar
Elliot Lee committed
228
229
      check_shift = 3;
      break;
230
    case GIMP_LARGE_CHECKS:
231
      check_mod   = 0xf;
Elliot Lee's avatar
Elliot Lee committed
232
233
234
235
      check_shift = 4;
      break;
    }

236
237
  g_free (render_check_buf);
  g_free (render_empty_buf);
238
  g_free (render_white_buf);
239
240
  g_free (render_temp_buf);

241
242
243
244
245
246
247
#define BUF_SIZE (MAX (GIMP_DISPLAY_SHELL_RENDER_BUF_WIDTH, \
                       GIMP_VIEWABLE_MAX_PREVIEW_SIZE) + 4)

  render_check_buf = g_new  (guchar, BUF_SIZE * 3);
  render_empty_buf = g_new0 (guchar, BUF_SIZE * 3);
  render_white_buf = g_new  (guchar, BUF_SIZE * 3);
  render_temp_buf  = g_new  (guchar, BUF_SIZE * 3);
248

Elliot Lee's avatar
Elliot Lee committed
249
  /*  calculate check buffer for previews  */
250
251
252
253

  memset (render_white_buf, 255, BUF_SIZE * 3);

  for (i = 0; i < BUF_SIZE; i++)
Elliot Lee's avatar
Elliot Lee committed
254
    {
255
256
257
258
259
260
261
262
263
264
265
266
      if (i & 0x4)
        {
          render_check_buf[i * 3 + 0] = render_blend_dark_check[0];
          render_check_buf[i * 3 + 1] = render_blend_dark_check[0];
          render_check_buf[i * 3 + 2] = render_blend_dark_check[0];
        }
      else
        {
          render_check_buf[i * 3 + 0] = render_blend_light_check[0];
          render_check_buf[i * 3 + 1] = render_blend_light_check[0];
          render_check_buf[i * 3 + 2] = render_blend_light_check[0];
        }
Elliot Lee's avatar
Elliot Lee committed
267
    }
268
269

#undef BUF_SIZE
Elliot Lee's avatar
Elliot Lee committed
270
271
}

272

Elliot Lee's avatar
Elliot Lee committed
273
274
/*  Render Image functions  */

275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
static void     render_image_rgb                (RenderInfo       *info);
static void     render_image_rgb_a              (RenderInfo       *info);
static void     render_image_gray               (RenderInfo       *info);
static void     render_image_gray_a             (RenderInfo       *info);
static void     render_image_indexed            (RenderInfo       *info);
static void     render_image_indexed_a          (RenderInfo       *info);

static void     render_image_init_info          (RenderInfo       *info,
						 GimpDisplayShell *shell,
						 gint              x,
						 gint              y,
						 gint              w,
						 gint              h);
static guint  * render_image_init_alpha         (gint              mult);
static guchar * render_image_accelerate_scaling (gint              width,
						 gint              start,
						 gfloat            scalex);
static guchar * render_image_tile_fault         (RenderInfo       *info);
Elliot Lee's avatar
Elliot Lee committed
293
294


295
static RenderFunc render_funcs[6] =
Elliot Lee's avatar
Elliot Lee committed
296
{
297
298
299
300
301
302
  render_image_rgb,
  render_image_rgb_a,
  render_image_gray,
  render_image_gray_a,
  render_image_indexed,
  render_image_indexed_a,
Elliot Lee's avatar
Elliot Lee committed
303
304
305
306
307
308
309
310
311
312
313
};


/*****************************************************************/
/*  This function is the core of the display--it offsets and     */
/*  scales the image according to the current parameters in the  */
/*  gdisp object.  It handles color, grayscale, 8, 15, 16, 24,   */
/*  & 32 bit output depths.                                      */
/*****************************************************************/

void
314
315
316
317
318
gimp_display_shell_render (GimpDisplayShell *shell,
                           gint              x,
                           gint              y,
                           gint              w,
                           gint              h)
Elliot Lee's avatar
Elliot Lee committed
319
{
320
321
322
  RenderInfo     info;
  GimpImageType  image_type;
  GList         *list;
323
324

  render_image_init_info (&info, shell, x, y, w, h);
Elliot Lee's avatar
Elliot Lee committed
325

326
  image_type = gimp_image_projection_type (shell->gdisp->gimage);
327

328
  if ((image_type < GIMP_RGB_IMAGE) || (image_type > GIMP_INDEXEDA_IMAGE))
Elliot Lee's avatar
Elliot Lee committed
329
    {
330
      g_message ("unknown gimage projection type: %d", image_type);
Elliot Lee's avatar
Elliot Lee committed
331
332
333
334
335
      return;
    }

  if ((info.dest_bpp < 1) || (info.dest_bpp > 4))
    {
Manish Singh's avatar
Manish Singh committed
336
      g_message ("unsupported destination bytes per pixel: %d", info.dest_bpp);
Elliot Lee's avatar
Elliot Lee committed
337
338
339
      return;
    }

340
  /* Currently, only RGBA and GRAYA projection types are used - the rest
341
342
   * are in case of future need.  -- austin, 28th Nov 1998.
   */
343
  if (image_type != GIMP_RGBA_IMAGE && image_type != GIMP_GRAYA_IMAGE)
344
    g_warning ("using untested projection type %d", image_type);
345

346
  (* render_funcs[image_type]) (&info);
347
348

  /*  apply filters to the rendered projection  */
349
  for (list = shell->filters; list; list = g_list_next (list))
350
    {
351
      GimpColorDisplay *filter = (GimpColorDisplay *) list->data;
352

353
      gimp_color_display_convert (filter,
354
355
356
357
                                  shell->render_buf,
                                  w, h,
                                  3,
                                  3 * GIMP_DISPLAY_SHELL_RENDER_BUF_WIDTH);
358
359
360
    }

  /*  put it to the screen  */
361
362
363
364
365
366
  gimp_canvas_draw_rgb (GIMP_CANVAS (shell->canvas), GIMP_CANVAS_STYLE_RENDER,
                        x + shell->disp_xoffset, y + shell->disp_yoffset,
                        w, h,
                        shell->render_buf,
                        3 * GIMP_DISPLAY_SHELL_RENDER_BUF_WIDTH,
                        shell->offset_x, shell->offset_y);
Elliot Lee's avatar
Elliot Lee committed
367
368
369
370
371
372
373
374
}


/*************************/
/*  8 Bit functions      */
/*************************/

static void
375
render_image_indexed (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
376
377
378
{
  guchar *src;
  guchar *dest;
379
  guchar *cmap;
380
381
382
383
384
  gulong  val;
  gint    byte_order;
  gint    y, ye;
  gint    x, xe;
  gint    initial;
Elliot Lee's avatar
Elliot Lee committed
385

386
  cmap = gimp_image_get_colormap (info->shell->gdisp->gimage);
Elliot Lee's avatar
Elliot Lee committed
387

388
  y  = info->y;
Elliot Lee's avatar
Elliot Lee committed
389
390
391
392
393
394
395
396
397
  ye = info->y + info->h;
  xe = info->x + info->w;

  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
398
399
400
401
      gint error = RINT (floor ((y + 1) / info->scaley)
                         - floor (y / info->scaley));

      if (!initial && (error == 0))
402
403
404
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
405
406
407
408
409
      else
	{
	  src = info->src;
	  dest = info->dest;

410
	  g_return_if_fail (src != NULL);
411

412
413
414
415
	  for (x = info->x; x < xe; x++)
	    {
	      val = src[INDEXED_PIX] * 3;
	      src += 1;
416

417
418
419
420
421
	      dest[0] = cmap[val+0];
	      dest[1] = cmap[val+1];
	      dest[2] = cmap[val+2];
	      dest += 3;
	    }
Elliot Lee's avatar
Elliot Lee committed
422
423
424
425
	}

      info->dest += info->dest_bpl;

426
427
      initial = FALSE;

428
      if (error >= 1)
Elliot Lee's avatar
Elliot Lee committed
429
	{
430
	  info->src_y += error;
Elliot Lee's avatar
Elliot Lee committed
431
	  info->src = render_image_tile_fault (info);
432
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
433
434
435
436
437
	}
    }
}

static void
438
render_image_indexed_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
439
440
441
{
  guchar *src;
  guchar *dest;
442
  guint  *alpha;
443
  guchar *cmap;
444
445
446
447
448
449
450
451
  gulong  r, g, b;
  gulong  val;
  guint   a;
  gint    dark_light;
  gint    byte_order;
  gint    y, ye;
  gint    x, xe;
  gint    initial;
Elliot Lee's avatar
Elliot Lee committed
452

453
  cmap = gimp_image_get_colormap (info->shell->gdisp->gimage);
454
  alpha = info->alpha;
Elliot Lee's avatar
Elliot Lee committed
455

456
  y  = info->y;
Elliot Lee's avatar
Elliot Lee committed
457
458
459
460
461
462
463
464
465
  ye = info->y + info->h;
  xe = info->x + info->w;

  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
466
467
468
469
      gint error = RINT (floor ((y + 1) / info->scaley)
                         - floor (y / info->scaley));

      if (!initial && (error == 0) && (y & check_mod))
470
471
472
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
473
474
475
476
477
      else
	{
	  src = info->src;
	  dest = info->dest;

478
479
	  dark_light = (y >> check_shift) + (info->x >> check_shift);

480
	  g_return_if_fail (src != NULL);
Elliot Lee's avatar
Elliot Lee committed
481

482
483
484
485
486
487
488
489
	  for (x = info->x; x < xe; x++)
	    {
	      a = alpha[src[ALPHA_I_PIX]];
	      val = src[INDEXED_PIX] * 3;
	      src += 2;

	      if (dark_light & 0x1)
		{
490
491
492
		  r = render_blend_dark_check[(a | cmap[val + 0])];
		  g = render_blend_dark_check[(a | cmap[val + 1])];
		  b = render_blend_dark_check[(a | cmap[val + 2])];
493
494
495
		}
	      else
		{
496
497
498
		  r = render_blend_light_check[(a | cmap[val + 0])];
		  g = render_blend_light_check[(a | cmap[val + 1])];
		  b = render_blend_light_check[(a | cmap[val + 2])];
499
500
501
502
503
		}

		dest[0] = r;
		dest[1] = g;
		dest[2] = b;
Elliot Lee's avatar
Elliot Lee committed
504
505
		dest += 3;

506
507
		if (((x + 1) & check_mod) == 0)
		  dark_light += 1;
Elliot Lee's avatar
Elliot Lee committed
508
509
510
511
512
	      }
	}

      info->dest += info->dest_bpl;

513
514
      initial = FALSE;

515
      if (error >= 1)
Elliot Lee's avatar
Elliot Lee committed
516
	{
517
	  info->src_y += error;
Elliot Lee's avatar
Elliot Lee committed
518
	  info->src = render_image_tile_fault (info);
519
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
520
521
522
523
524
	}
    }
}

static void
525
render_image_gray (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
526
527
528
{
  guchar *src;
  guchar *dest;
529
530
531
532
533
534
535
  gulong  val;
  gint    byte_order;
  gint    y, ye;
  gint    x, xe;
  gint    initial;

  y  = info->y;
Elliot Lee's avatar
Elliot Lee committed
536
537
538
539
540
541
542
543
544
  ye = info->y + info->h;
  xe = info->x + info->w;

  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
545
546
547
548
      gint error = RINT (floor ((y + 1) / info->scaley)
                         - floor (y / info->scaley));

      if (!initial && (error == 0))
549
550
551
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
552
553
554
555
      else
	{
	  src = info->src;
	  dest = info->dest;
556

557
558
	  g_return_if_fail (src != NULL);

559
560
561
562
	  for (x = info->x; x < xe; x++)
	    {
	      val = src[GRAY_PIX];
	      src += 1;
Elliot Lee's avatar
Elliot Lee committed
563

564
565
566
567
568
	      dest[0] = val;
	      dest[1] = val;
	      dest[2] = val;
	      dest += 3;
	    }
Elliot Lee's avatar
Elliot Lee committed
569
570
571
572
	}

      info->dest += info->dest_bpl;

573
574
      initial = FALSE;

575
      if (error >= 1)
Elliot Lee's avatar
Elliot Lee committed
576
	{
577
	  info->src_y += error;
Elliot Lee's avatar
Elliot Lee committed
578
	  info->src = render_image_tile_fault (info);
579
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
580
581
582
583
584
	}
    }
}

static void
585
render_image_gray_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
586
587
588
{
  guchar *src;
  guchar *dest;
589
590
591
592
593
594
595
596
  guint  *alpha;
  gulong  val;
  guint   a;
  gint    dark_light;
  gint    byte_order;
  gint    y, ye;
  gint    x, xe;
  gint    initial;
Elliot Lee's avatar
Elliot Lee committed
597
598
599

  alpha = info->alpha;

600
  y  = info->y;
Elliot Lee's avatar
Elliot Lee committed
601
602
603
604
605
606
607
608
609
  ye = info->y + info->h;
  xe = info->x + info->w;

  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
610
611
612
613
      gint error = RINT (floor ((y + 1) / info->scaley)
                         - floor (y / info->scaley));

      if (!initial && (error == 0) && (y & check_mod))
614
615
616
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
617
618
619
620
621
622
623
      else
	{
	  src = info->src;
	  dest = info->dest;

	  dark_light = (y >> check_shift) + (info->x >> check_shift);

624
625
	  g_return_if_fail (src != NULL);

626
627
628
629
	  for (x = info->x; x < xe; x++)
	    {
	      a = alpha[src[ALPHA_G_PIX]];
	      if (dark_light & 0x1)
630
		val = render_blend_dark_check[(a | src[GRAY_PIX])];
631
	      else
632
		val = render_blend_light_check[(a | src[GRAY_PIX])];
633
634
635
636
637
638
639
640
641
642
	      src += 2;

	      dest[0] = val;
	      dest[1] = val;
	      dest[2] = val;
	      dest += 3;

	      if (((x + 1) & check_mod) == 0)
		dark_light += 1;
	    }
Elliot Lee's avatar
Elliot Lee committed
643
644
645
646
	}

      info->dest += info->dest_bpl;

647
648
      initial = FALSE;

649
      if (error >= 1)
Elliot Lee's avatar
Elliot Lee committed
650
	{
651
	  info->src_y += error;
Elliot Lee's avatar
Elliot Lee committed
652
	  info->src = render_image_tile_fault (info);
653
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
654
655
656
657
658
	}
    }
}

static void
659
render_image_rgb (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
660
{
661
662
  gint    byte_order;
  gint    y, ye;
663
  gint    xe;
664
665
666
  gint    initial;

  y  = info->y;
Daniel Egger's avatar
Daniel Egger committed
667
668
669
  ye = info->y + info->h;
  xe = info->x + info->w;

Elliot Lee's avatar
Elliot Lee committed
670
  initial = TRUE;
Daniel Egger's avatar
Daniel Egger committed
671
  byte_order = info->byte_order;
Elliot Lee's avatar
Elliot Lee committed
672
673
674
675
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
676
677
678
679
      gint error = RINT (floor ((y + 1) / info->scaley)
                         - floor (y / info->scaley));

      if (!initial && (error == 0))
680
681
682
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
683
684
      else
	{
685
	  g_return_if_fail (info->src != NULL);
686

687
          memcpy (info->dest, info->src, 3 * info->w);
Elliot Lee's avatar
Elliot Lee committed
688
689
690
691
	}

      info->dest += info->dest_bpl;

692
693
      initial = FALSE;

694
      if (error >= 1)
Elliot Lee's avatar
Elliot Lee committed
695
	{
696
	  info->src_y += error;
Elliot Lee's avatar
Elliot Lee committed
697
	  info->src = render_image_tile_fault (info);
698
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
699
700
701
702
703
	}
    }
}

static void
704
render_image_rgb_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
705
706
707
{
  guchar *src;
  guchar *dest;
708
709
710
711
712
713
714
715
  guint  *alpha;
  gulong  r, g, b;
  guint   a;
  gint    dark_light;
  gint    byte_order;
  gint    y, ye;
  gint    x, xe;
  gint    initial;
Elliot Lee's avatar
Elliot Lee committed
716
717
718

  alpha = info->alpha;

719
  y  = info->y;
Daniel Egger's avatar
Daniel Egger committed
720
721
722
  ye = info->y + info->h;
  xe = info->x + info->w;

Elliot Lee's avatar
Elliot Lee committed
723
724
725
726
727
728
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
729
730
731
732
      gint error = RINT (floor ((y + 1) / info->scaley)
                         - floor (y / info->scaley));

      if (!initial && (error == 0) && (y & check_mod))
733
734
735
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
736
737
738
739
740
741
742
      else
	{
	  src = info->src;
	  dest = info->dest;

	  dark_light = (y >> check_shift) + (info->x >> check_shift);

743
744
	  g_return_if_fail (src != NULL);

745
746
747
748
749
	  for (x = info->x; x < xe; x++)
	    {
	      a = alpha[src[ALPHA_PIX]];
	      if (dark_light & 0x1)
		{
750
751
752
		  r = render_blend_dark_check[(a | src[RED_PIX])];
		  g = render_blend_dark_check[(a | src[GREEN_PIX])];
		  b = render_blend_dark_check[(a | src[BLUE_PIX])];
753
754
755
		}
	      else
		{
756
757
758
		  r = render_blend_light_check[(a | src[RED_PIX])];
		  g = render_blend_light_check[(a | src[GREEN_PIX])];
		  b = render_blend_light_check[(a | src[BLUE_PIX])];
759
		}
Elliot Lee's avatar
Elliot Lee committed
760

761
	      src += 4;
Elliot Lee's avatar
Elliot Lee committed
762

763
764
765
766
	      dest[0] = r;
	      dest[1] = g;
	      dest[2] = b;
	      dest += 3;
Elliot Lee's avatar
Elliot Lee committed
767

768
769
770
	      if (((x + 1) & check_mod) == 0)
		dark_light += 1;
	    }
Elliot Lee's avatar
Elliot Lee committed
771
772
773
774
	}

      info->dest += info->dest_bpl;

775
776
      initial = FALSE;

777
      if (error >= 1)
Elliot Lee's avatar
Elliot Lee committed
778
	{
779
	  info->src_y += error;
Elliot Lee's avatar
Elliot Lee committed
780
	  info->src = render_image_tile_fault (info);
781
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
782
783
784
785
786
	}
    }
}

static void
787
788
789
790
791
792
render_image_init_info (RenderInfo       *info,
			GimpDisplayShell *shell,
			gint              x,
			gint              y,
			gint              w,
			gint              h)
Elliot Lee's avatar
Elliot Lee committed
793
{
794
795
796
797
  info->shell      = shell;
  info->src_tiles  = gimp_image_projection (shell->gdisp->gimage);
  info->x          = x + shell->offset_x;
  info->y          = y + shell->offset_y;
798
799
  info->w          = w;
  info->h          = h;
800
801
  info->scalex     = SCALEFACTOR_X (shell);
  info->scaley     = SCALEFACTOR_Y (shell);
802
803
  info->src_x      = (gfloat) info->x / info->scalex;
  info->src_y      = (gfloat) info->y / info->scaley;
804
805
806
807
  info->src_bpp    = gimp_image_projection_bytes (shell->gdisp->gimage);
  info->dest       = shell->render_buf;
  info->dest_bpp   = 3;
  info->dest_bpl   = info->dest_bpp * GIMP_DISPLAY_SHELL_RENDER_BUF_WIDTH;
Elliot Lee's avatar
Elliot Lee committed
808
  info->dest_width = info->w * info->dest_bpp;
809
  info->byte_order = GDK_MSB_FIRST;
Sven Neumann's avatar
Sven Neumann committed
810
811
  info->scale      = render_image_accelerate_scaling (w,
                                                      info->x, info->scalex);
812
  info->alpha      = NULL;
Elliot Lee's avatar
Elliot Lee committed
813

814
  if (GIMP_IMAGE_TYPE_HAS_ALPHA (gimp_image_projection_type (shell->gdisp->gimage)))
Elliot Lee's avatar
Elliot Lee committed
815
    {
816
      info->alpha =
817
	render_image_init_alpha (gimp_image_projection_opacity (shell->gdisp->gimage) * 255.999);
Elliot Lee's avatar
Elliot Lee committed
818
819
820
    }
}

821
static guint *
822
render_image_init_alpha (gint mult)
Elliot Lee's avatar
Elliot Lee committed
823
824
{
  static guint *alpha_mult = NULL;
825
826
827
  static gint   alpha_val  = -1;

  gint i;
Elliot Lee's avatar
Elliot Lee committed
828
829
830
831

  if (alpha_val != mult)
    {
      if (!alpha_mult)
832
	alpha_mult = g_new (guint, 256);
Elliot Lee's avatar
Elliot Lee committed
833
834
835
836
837
838
839
840
841

      alpha_val = mult;
      for (i = 0; i < 256; i++)
	alpha_mult[i] = ((mult * i) / 255) << 8;
    }

  return alpha_mult;
}

842
static guchar *
843
844
845
render_image_accelerate_scaling (gint   width,
				 gint   start,
				 gfloat scalex)
Elliot Lee's avatar
Elliot Lee committed
846
847
{
  static guchar *scale = NULL;
848

849
  gint  i;
Elliot Lee's avatar
Elliot Lee committed
850

851
852
  if (! scale)
    scale = g_new (guchar, GIMP_DISPLAY_SHELL_RENDER_BUF_WIDTH + 1);
Elliot Lee's avatar
Elliot Lee committed
853

854
  for (i = 0; i <= width; i++)
855
    {
856
857
      scale[i] = RINT (floor ((start + 1) / scalex) - floor (start / scalex));
      start++;
858
    }
Elliot Lee's avatar
Elliot Lee committed
859
860
861
862

  return scale;
}

863
static guchar *
Elliot Lee's avatar
Elliot Lee committed
864
865
render_image_tile_fault (RenderInfo *info)
{
866
  Tile   *tile;
Elliot Lee's avatar
Elliot Lee committed
867
868
869
  guchar *data;
  guchar *dest;
  guchar *scale;
870
871
872
873
874
  gint    width;
  gint    tilex;
  gint    step;
  gint    bpp = info->src_bpp;
  gint    x, b;
Elliot Lee's avatar
Elliot Lee committed
875

876
  tile = tile_manager_get_tile (info->src_tiles,
Sven Neumann's avatar
Sven Neumann committed
877
				info->src_x, info->src_y,
878
				TRUE, FALSE);
Elliot Lee's avatar
Elliot Lee committed
879
880
881
  if (!tile)
    return NULL;

882
  data = tile_data_pointer (tile,
scott's avatar
scott committed
883
884
			    info->src_x % TILE_WIDTH,
			    info->src_y % TILE_HEIGHT);
Elliot Lee's avatar
Elliot Lee committed
885
  scale = info->scale;
886
  dest  = tile_buf;
Elliot Lee's avatar
Elliot Lee committed
887

888
  x     = info->src_x;
Elliot Lee's avatar
Elliot Lee committed
889
890
  width = info->w;

891
892
  tilex = info->src_x / TILE_WIDTH;

Elliot Lee's avatar
Elliot Lee committed
893
894
  while (width--)
    {
895
      for (b = 0; b < bpp; b++)
Elliot Lee's avatar
Elliot Lee committed
896
897
	*dest++ = data[b];

898
899
      step = *scale++;
      if (step != 0)
Elliot Lee's avatar
Elliot Lee committed
900
	{
901
902
	  x += step;
	  data += step * bpp;
Elliot Lee's avatar
Elliot Lee committed
903

Daniel Egger's avatar
Daniel Egger committed
904
	  if ((x >> tile_shift) != tilex)
Elliot Lee's avatar
Elliot Lee committed
905
	    {
906
	      tile_release (tile, FALSE);
Elliot Lee's avatar
Elliot Lee committed
907
908
	      tilex += 1;

Sven Neumann's avatar
Sven Neumann committed
909
910
911
912
	      tile = tile_manager_get_tile (info->src_tiles,
                                            x, info->src_y,
                                            TRUE, FALSE);

Daniel Egger's avatar
Daniel Egger committed
913
914
915
	      if (!tile)
		return tile_buf;

916
	      data = tile_data_pointer (tile,
Sven Neumann's avatar
Sven Neumann committed
917
918
                                        x % TILE_WIDTH,
                                        info->src_y % TILE_HEIGHT);
Elliot Lee's avatar
Elliot Lee committed
919
920
921
922
	    }
	}
    }

923
  tile_release (tile, FALSE);
924

Elliot Lee's avatar
Elliot Lee committed
925
926
  return tile_buf;
}