gimpdisplayshell-render.c 22 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 "gimpdisplay.h"
Michael Natterer's avatar
Michael Natterer committed
39
#include "gimpdisplayshell.h"
40
#include "gimpdisplayshell-filter.h"
41
#include "gimpdisplayshell-render.h"
42

Elliot Lee's avatar
Elliot Lee committed
43
44

typedef struct _RenderInfo  RenderInfo;
45
46

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

struct _RenderInfo
{
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  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
66
67
68
};


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


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

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


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
89
90
91
92
static guchar  check_combos[6][2] =
{
  { 204, 255 },
  { 102, 153 },
93
  {   0,  51 },
Elliot Lee's avatar
Elliot Lee committed
94
95
  { 255, 255 },
  { 127, 127 },
96
  {   0,   0 }
Elliot Lee's avatar
Elliot Lee committed
97
98
99
100
};


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

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

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

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

120
  g_signal_handlers_disconnect_by_func (gimp->config,
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
                                        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;
    }

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

148
149
150
151
152
153
154
155
156
157
158
159
  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;
    }

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

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


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

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

  /*  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
191
192
193
  tile_shift = 0;
  while ((1 << tile_shift) < TILE_WIDTH)
    tile_shift++;
Elliot Lee's avatar
Elliot Lee committed
194
195

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

200
201
202
203
  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);
204
205
  if (! render_blend_white)
    render_blend_white = g_new (guchar, 65536);
Elliot Lee's avatar
Elliot Lee committed
206
207
208
209

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

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

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

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

240
241
242
243
244
245
246
#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);
247

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

  memset (render_white_buf, 255, BUF_SIZE * 3);

  for (i = 0; i < BUF_SIZE; i++)
Elliot Lee's avatar
Elliot Lee committed
253
    {
254
255
256
257
258
259
260
261
262
263
264
265
      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
266
    }
267
268

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

271

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

274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
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
292
293


294
static RenderFunc render_funcs[6] =
Elliot Lee's avatar
Elliot Lee committed
295
{
296
297
298
299
300
301
  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
302
303
304
305
306
307
308
309
310
311
312
};


/*****************************************************************/
/*  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
313
314
315
316
317
gimp_display_shell_render (GimpDisplayShell *shell,
                           gint              x,
                           gint              y,
                           gint              w,
                           gint              h)
Elliot Lee's avatar
Elliot Lee committed
318
{
319
320
321
  RenderInfo     info;
  GimpImageType  image_type;
  GList         *list;
322
323

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

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

327
  if ((image_type < GIMP_RGB_IMAGE) || (image_type > GIMP_INDEXEDA_IMAGE))
Elliot Lee's avatar
Elliot Lee committed
328
    {
Manish Singh's avatar
Manish Singh committed
329
      g_message ("unknown gimage projection type: %d",
330
		 gimp_image_projection_type (shell->gdisp->gimage));
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;
352

353
      filter = (GimpColorDisplay *) list->data;
354

355
      gimp_color_display_convert (filter,
356
357
358
359
                                  shell->render_buf,
                                  w, h,
                                  3,
                                  3 * GIMP_DISPLAY_SHELL_RENDER_BUF_WIDTH);
360
361
362
363
364
365
366
367
368
369
370
371
372
    }

  /*  put it to the screen  */
  gdk_draw_rgb_image_dithalign (shell->canvas->window,
                                shell->render_gc,
                                x + shell->disp_xoffset,
                                y + shell->disp_yoffset,
                                w, h,
                                GDK_RGB_DITHER_MAX,
                                shell->render_buf,
                                3 * GIMP_DISPLAY_SHELL_RENDER_BUF_WIDTH,
                                shell->offset_x,
                                shell->offset_y);
Elliot Lee's avatar
Elliot Lee committed
373
374
375
376
377
378
379
380
}


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

static void
381
render_image_indexed (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
382
383
384
{
  guchar *src;
  guchar *dest;
385
  guchar *cmap;
386
387
388
389
390
391
392
  gulong  val;
  gint    byte_order;
  gint    y, ye;
  gint    x, xe;
  gint    initial;
  gfloat  error;
  gfloat  step;
Elliot Lee's avatar
Elliot Lee committed
393

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

396
  y  = info->y;
Elliot Lee's avatar
Elliot Lee committed
397
398
399
  ye = info->y + info->h;
  xe = info->x + info->w;

400
401
  step = 1.0 / info->scaley;

402
403
  error  = y * step;
  error -= ((gint) error) - step;
404

Elliot Lee's avatar
Elliot Lee committed
405
406
407
408
409
410
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
411
      if (!initial && (error < 1.0))
412
413
414
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
415
416
417
418
419
      else
	{
	  src = info->src;
	  dest = info->dest;

420
	  g_return_if_fail (src != NULL);
421

422
423
424
425
	  for (x = info->x; x < xe; x++)
	    {
	      val = src[INDEXED_PIX] * 3;
	      src += 1;
426

427
428
429
430
431
	      dest[0] = cmap[val+0];
	      dest[1] = cmap[val+1];
	      dest[2] = cmap[val+2];
	      dest += 3;
	    }
Elliot Lee's avatar
Elliot Lee committed
432
433
434
435
	}

      info->dest += info->dest_bpl;

436
437
438
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
439
	{
440
441
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
442
	  info->src = render_image_tile_fault (info);
443
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
444
445
	}

446
      error += step;
Elliot Lee's avatar
Elliot Lee committed
447
448
449
450
    }
}

static void
451
render_image_indexed_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
452
453
454
{
  guchar *src;
  guchar *dest;
455
  guint  *alpha;
456
  guchar *cmap;
457
458
459
460
461
462
463
464
465
466
  gulong  r, g, b;
  gulong  val;
  guint   a;
  gint    dark_light;
  gint    byte_order;
  gint    y, ye;
  gint    x, xe;
  gint    initial;
  gfloat  error;
  gfloat  step;
Elliot Lee's avatar
Elliot Lee committed
467

468
  cmap = gimp_image_get_colormap (info->shell->gdisp->gimage);
469
  alpha = info->alpha;
Elliot Lee's avatar
Elliot Lee committed
470

471
  y  = info->y;
Elliot Lee's avatar
Elliot Lee committed
472
473
474
  ye = info->y + info->h;
  xe = info->x + info->w;

475
476
  step = 1.0 / info->scaley;

477
478
  error  = y * step;
  error -= ((gint) error) - step;
479

Elliot Lee's avatar
Elliot Lee committed
480
481
482
483
484
485
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
486
      if (!initial && (error < 1.0) && (y & check_mod))
487
488
489
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
490
491
492
493
494
      else
	{
	  src = info->src;
	  dest = info->dest;

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

497
	  g_return_if_fail (src != NULL);
Elliot Lee's avatar
Elliot Lee committed
498

499
500
501
502
503
504
505
506
	  for (x = info->x; x < xe; x++)
	    {
	      a = alpha[src[ALPHA_I_PIX]];
	      val = src[INDEXED_PIX] * 3;
	      src += 2;

	      if (dark_light & 0x1)
		{
507
508
509
		  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])];
510
511
512
		}
	      else
		{
513
514
515
		  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])];
516
517
518
519
520
		}

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

523
524
		if (((x + 1) & check_mod) == 0)
		  dark_light += 1;
Elliot Lee's avatar
Elliot Lee committed
525
526
527
528
529
	      }
	}

      info->dest += info->dest_bpl;

530
531
532
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
533
	{
534
535
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
536
	  info->src = render_image_tile_fault (info);
537
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
538
539
	}

540
      error += step;
Elliot Lee's avatar
Elliot Lee committed
541
542
543
544
    }
}

static void
545
render_image_gray (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
546
547
548
{
  guchar *src;
  guchar *dest;
549
550
551
552
553
554
555
556
557
  gulong  val;
  gint    byte_order;
  gint    y, ye;
  gint    x, xe;
  gint    initial;
  gfloat  error;
  gfloat  step;

  y  = info->y;
Elliot Lee's avatar
Elliot Lee committed
558
559
560
  ye = info->y + info->h;
  xe = info->x + info->w;

561
562
  step = 1.0 / info->scaley;

563
564
  error  = y * step;
  error -= ((gint) error) - step;
565

Elliot Lee's avatar
Elliot Lee committed
566
567
568
569
570
571
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
572
      if (!initial && (error < 1.0))
573
574
575
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
576
577
578
579
      else
	{
	  src = info->src;
	  dest = info->dest;
580
	  
581
582
	  g_return_if_fail (src != NULL);

583
584
585
586
	  for (x = info->x; x < xe; x++)
	    {
	      val = src[GRAY_PIX];
	      src += 1;
Elliot Lee's avatar
Elliot Lee committed
587

588
589
590
591
592
	      dest[0] = val;
	      dest[1] = val;
	      dest[2] = val;
	      dest += 3;
	    }
Elliot Lee's avatar
Elliot Lee committed
593
594
595
596
	}

      info->dest += info->dest_bpl;

597
598
599
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
600
	{
601
602
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
603
	  info->src = render_image_tile_fault (info);
604
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
605
606
	}

607
      error += step;
Elliot Lee's avatar
Elliot Lee committed
608
609
610
611
    }
}

static void
612
render_image_gray_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
613
614
615
{
  guchar *src;
  guchar *dest;
616
617
618
619
620
621
622
623
624
625
  guint  *alpha;
  gulong  val;
  guint   a;
  gint    dark_light;
  gint    byte_order;
  gint    y, ye;
  gint    x, xe;
  gint    initial;
  gfloat  error;
  gfloat  step;
Elliot Lee's avatar
Elliot Lee committed
626
627
628

  alpha = info->alpha;

629
  y  = info->y;
Elliot Lee's avatar
Elliot Lee committed
630
631
632
  ye = info->y + info->h;
  xe = info->x + info->w;

633
634
  step = 1.0 / info->scaley;

635
636
  error  = y * step;
  error -= ((gint) error) - step;
637

Elliot Lee's avatar
Elliot Lee committed
638
639
640
641
642
643
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
644
      if (!initial && (error < 1.0) && (y & check_mod))
645
646
647
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
648
649
650
651
652
653
654
      else
	{
	  src = info->src;
	  dest = info->dest;

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

655
656
	  g_return_if_fail (src != NULL);

657
658
659
660
	  for (x = info->x; x < xe; x++)
	    {
	      a = alpha[src[ALPHA_G_PIX]];
	      if (dark_light & 0x1)
661
		val = render_blend_dark_check[(a | src[GRAY_PIX])];
662
	      else
663
		val = render_blend_light_check[(a | src[GRAY_PIX])];
664
665
666
667
668
669
670
671
672
673
	      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
674
675
676
677
	}

      info->dest += info->dest_bpl;

678
679
680
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
681
	{
682
683
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
684
	  info->src = render_image_tile_fault (info);
685
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
686
687
	}

688
      error += step;
Elliot Lee's avatar
Elliot Lee committed
689
690
691
692
    }
}

static void
693
render_image_rgb (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
694
{
695
696
  gint    byte_order;
  gint    y, ye;
697
  gint    xe;
698
699
700
701
702
  gint    initial;
  gfloat  error;
  gfloat  step;

  y  = info->y;
Daniel Egger's avatar
Daniel Egger committed
703
704
705
706
  ye = info->y + info->h;
  xe = info->x + info->w;

  step = 1.0 / info->scaley;
707

708
709
  error  = y * step;
  error -= (gint) error - step;
710

Elliot Lee's avatar
Elliot Lee committed
711
  initial = TRUE;
Daniel Egger's avatar
Daniel Egger committed
712
  byte_order = info->byte_order;
Elliot Lee's avatar
Elliot Lee committed
713
714
715
716
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
717
      if (!initial && (error < 1.0))
718
719
720
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
721
722
      else
	{
723
	  g_return_if_fail (info->src != NULL);
724

725
          memcpy (info->dest, info->src, 3 * info->w);
Elliot Lee's avatar
Elliot Lee committed
726
727
728
729
	}

      info->dest += info->dest_bpl;

730
731
732
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
733
	{
734
735
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
736
	  info->src = render_image_tile_fault (info);
737
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
738
739
	}

740
      error += step;
Elliot Lee's avatar
Elliot Lee committed
741
742
743
744
    }
}

static void
745
render_image_rgb_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
746
747
748
{
  guchar *src;
  guchar *dest;
749
750
751
752
753
754
755
756
757
758
  guint  *alpha;
  gulong  r, g, b;
  guint   a;
  gint    dark_light;
  gint    byte_order;
  gint    y, ye;
  gint    x, xe;
  gint    initial;
  gfloat  error;
  gfloat  step;
Elliot Lee's avatar
Elliot Lee committed
759
760
761

  alpha = info->alpha;

762
  y  = info->y;
Daniel Egger's avatar
Daniel Egger committed
763
764
765
766
  ye = info->y + info->h;
  xe = info->x + info->w;

  step = 1.0 / info->scaley;
767

768
769
  error  = y * step;
  error -= ((gint) error) - step;
770

Elliot Lee's avatar
Elliot Lee committed
771
772
773
774
775
776
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
777
      if (!initial && (error < 1.0) && (y & check_mod))
778
779
780
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
781
782
783
784
785
786
787
      else
	{
	  src = info->src;
	  dest = info->dest;

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

788
789
	  g_return_if_fail (src != NULL);

790
791
792
793
794
	  for (x = info->x; x < xe; x++)
	    {
	      a = alpha[src[ALPHA_PIX]];
	      if (dark_light & 0x1)
		{
795
796
797
		  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])];
798
799
800
		}
	      else
		{
801
802
803
		  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])];
804
		}
Elliot Lee's avatar
Elliot Lee committed
805

806
	      src += 4;
Elliot Lee's avatar
Elliot Lee committed
807

808
809
810
811
	      dest[0] = r;
	      dest[1] = g;
	      dest[2] = b;
	      dest += 3;
Elliot Lee's avatar
Elliot Lee committed
812

813
814
815
	      if (((x + 1) & check_mod) == 0)
		dark_light += 1;
	    }
Elliot Lee's avatar
Elliot Lee committed
816
817
818
819
	}

      info->dest += info->dest_bpl;

820
821
822
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
823
	{
824
825
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
826
	  info->src = render_image_tile_fault (info);
827
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
828
829
	}

830
      error += step;
Elliot Lee's avatar
Elliot Lee committed
831
832
833
834
    }
}

static void
835
836
837
838
839
840
render_image_init_info (RenderInfo       *info,
			GimpDisplayShell *shell,
			gint              x,
			gint              y,
			gint              w,
			gint              h)
Elliot Lee's avatar
Elliot Lee committed
841
{
842
843
844
845
  info->shell      = shell;
  info->src_tiles  = gimp_image_projection (shell->gdisp->gimage);
  info->x          = x + shell->offset_x;
  info->y          = y + shell->offset_y;
846
847
  info->w          = w;
  info->h          = h;
848
849
  info->scalex     = SCALEFACTOR_X (shell);
  info->scaley     = SCALEFACTOR_Y (shell);
850
851
  info->src_x      = (gfloat) info->x / info->scalex;
  info->src_y      = (gfloat) info->y / info->scaley;
852
853
854
855
  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
856
  info->dest_width = info->w * info->dest_bpp;
857
  info->byte_order = GDK_MSB_FIRST;
Sven Neumann's avatar
Sven Neumann committed
858
859
  info->scale      = render_image_accelerate_scaling (w,
                                                      info->x, info->scalex);
860
  info->alpha      = NULL;
Elliot Lee's avatar
Elliot Lee committed
861

862
  if (GIMP_IMAGE_TYPE_HAS_ALPHA (gimp_image_projection_type (shell->gdisp->gimage)))
Elliot Lee's avatar
Elliot Lee committed
863
    {
864
      info->alpha =
865
	render_image_init_alpha (gimp_image_projection_opacity (shell->gdisp->gimage) * 255.999);
Elliot Lee's avatar
Elliot Lee committed
866
867
868
    }
}

869
static guint *
870
render_image_init_alpha (gint mult)
Elliot Lee's avatar
Elliot Lee committed
871
872
{
  static guint *alpha_mult = NULL;
873
874
875
  static gint   alpha_val  = -1;

  gint i;
Elliot Lee's avatar
Elliot Lee committed
876
877
878
879

  if (alpha_val != mult)
    {
      if (!alpha_mult)
880
	alpha_mult = g_new (guint, 256);
Elliot Lee's avatar
Elliot Lee committed
881
882
883
884
885
886
887
888
889

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

  return alpha_mult;
}

890
static guchar *
891
892
893
render_image_accelerate_scaling (gint   width,
				 gint   start,
				 gfloat scalex)
Elliot Lee's avatar
Elliot Lee committed
894
895
{
  static guchar *scale = NULL;
896
897
898
899

  gfloat error;
  gfloat step;
  gint   i;
Elliot Lee's avatar
Elliot Lee committed
900

901
902
  if (! scale)
    scale = g_new (guchar, GIMP_DISPLAY_SHELL_RENDER_BUF_WIDTH + 1);
Elliot Lee's avatar
Elliot Lee committed
903

904
  step = 1.0 / scalex;
Elliot Lee's avatar
Elliot Lee committed
905

906
  error = start * step;
907
  error -= ((gint) error) - step;
908
909

  for (i = 0; i <= width; i++)
910
911
912
913
    {
      scale[i] = ((gint) error);
      error += step - (gint) error;
    }
Elliot Lee's avatar
Elliot Lee committed
914
915
916
917

  return scale;
}

918
static guchar *
Elliot Lee's avatar
Elliot Lee committed
919
920
render_image_tile_fault (RenderInfo *info)
{
921
  Tile   *tile;
Elliot Lee's avatar
Elliot Lee committed
922
923
924
  guchar *data;
  guchar *dest;
  guchar *scale;
925
926
927
928
929
  gint    width;
  gint    tilex;
  gint    step;
  gint    bpp = info->src_bpp;
  gint    x, b;
Elliot Lee's avatar
Elliot Lee committed
930

931
  tile = tile_manager_get_tile (info->src_tiles,
Sven Neumann's avatar
Sven Neumann committed
932
				info->src_x, info->src_y,
933
				TRUE, FALSE);
Elliot Lee's avatar
Elliot Lee committed
934
935
936
  if (!tile)
    return NULL;

scott's avatar
scott committed
937
938
939
  data = tile_data_pointer (tile, 
			    info->src_x % TILE_WIDTH,
			    info->src_y % TILE_HEIGHT);
Elliot Lee's avatar
Elliot Lee committed
940
  scale = info->scale;
941
  dest  = tile_buf;
Elliot Lee's avatar
Elliot Lee committed
942

943
  x     = info->src_x;
Elliot Lee's avatar
Elliot Lee committed
944
945
  width = info->w;

946
947
  tilex = info->src_x / TILE_WIDTH;

Elliot Lee's avatar
Elliot Lee committed
948
949
  while (width--)
    {
950
      for (b = 0; b < bpp; b++)
Elliot Lee's avatar
Elliot Lee committed
951
952
	*dest++ = data[b];

953
954
      step = *scale++;
      if (step != 0)
Elliot Lee's avatar
Elliot Lee committed
955
	{
956
957
	  x += step;
	  data += step * bpp;
Elliot Lee's avatar
Elliot Lee committed
958

Daniel Egger's avatar
Daniel Egger committed
959
	  if ((x >> tile_shift) != tilex)
Elliot Lee's avatar
Elliot Lee committed
960
	    {
961
	      tile_release (tile, FALSE);
Elliot Lee's avatar
Elliot Lee committed
962
963
	      tilex += 1;

Sven Neumann's avatar
Sven Neumann committed
964
965
966
967
	      tile = tile_manager_get_tile (info->src_tiles,
                                            x, info->src_y,
                                            TRUE, FALSE);

Daniel Egger's avatar
Daniel Egger committed
968
969
970
	      if (!tile)
		return tile_buf;

scott's avatar
scott committed
971
	      data = tile_data_pointer (tile, 
Sven Neumann's avatar
Sven Neumann committed
972
973
                                        x % TILE_WIDTH,
                                        info->src_y % TILE_HEIGHT);
Elliot Lee's avatar
Elliot Lee committed
974
975
976
977
	    }
	}
    }

978
  tile_release (tile, FALSE);
979

Elliot Lee's avatar
Elliot Lee committed
980
981
  return tile_buf;
}