gimpdisplayshell-render.c 20.1 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
#include "display-types.h"
Sven Neumann's avatar
Sven Neumann committed
27

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

31
#include "core/gimpimage.h"
32
#include "core/gimpimage-projection.h"
33

34
#include "gimpdisplay.h"
Michael Natterer's avatar
Michael Natterer committed
35
#include "gimpdisplayshell.h"
36
#include "gimpdisplayshell-filter.h"
37
#include "gimpdisplayshell-render.h"
38

Elliot Lee's avatar
Elliot Lee committed
39
40
41
42
#include "gimprc.h"


typedef struct _RenderInfo  RenderInfo;
43
44

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

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


/*  accelerate transparency of image scaling  */
68
69
70
71
72
73
74
75
76
77
78
guchar *render_check_buf         = NULL;
guchar *render_empty_buf         = NULL;
guchar *render_temp_buf          = NULL;
guchar *render_blend_dark_check  = NULL;
guchar *render_blend_light_check = NULL;


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
79
80
81
82
static guchar  check_combos[6][2] =
{
  { 204, 255 },
  { 102, 153 },
83
  {   0,  51 },
Elliot Lee's avatar
Elliot Lee committed
84
85
  { 255, 255 },
  { 127, 127 },
86
  {   0,   0 }
Elliot Lee's avatar
Elliot Lee committed
87
88
89
90
};


void
91
92
render_setup (GimpCheckType check_type,
	      GimpCheckSize check_size)
Elliot Lee's avatar
Elliot Lee committed
93
{
94
95
  gint i, j;

96
  if (check_type < GIMP_LIGHT_CHECKS || check_type > GIMP_BLACK_ONLY)
97
    g_error ("invalid check_type argument to render_setup: %d", check_type);
98
  if (check_size < GIMP_SMALL_CHECKS || check_size > GIMP_LARGE_CHECKS)
99
    g_error ("invalid check_size argument to render_setup: %d", check_size);
Elliot Lee's avatar
Elliot Lee committed
100
101
102
103

  /*  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
104
105
106
  tile_shift = 0;
  while ((1 << tile_shift) < TILE_WIDTH)
    tile_shift++;
Elliot Lee's avatar
Elliot Lee committed
107
108

  /*  allocate a buffer for arranging information from a row of tiles  */
109
  if (! tile_buf)
110
    tile_buf = g_new (guchar, GIMP_DISPLAY_SHELL_RENDER_BUF_WIDTH * MAX_CHANNELS);
Elliot Lee's avatar
Elliot Lee committed
111

112
113
114
115
  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);
Elliot Lee's avatar
Elliot Lee committed
116
117
118
119

  for (i = 0; i < 256; i++)
    for (j = 0; j < 256; j++)
      {
120
	render_blend_dark_check [(i << 8) + j] = (guchar)
Elliot Lee's avatar
Elliot Lee committed
121
	  ((j * i + check_combos[check_type][0] * (255 - i)) / 255);
122
	render_blend_light_check [(i << 8) + j] = (guchar)
Elliot Lee's avatar
Elliot Lee committed
123
124
125
126
127
	  ((j * i + check_combos[check_type][1] * (255 - i)) / 255);
      }

  switch (check_size)
    {
128
    case GIMP_SMALL_CHECKS:
129
      check_mod   = 0x3;
Elliot Lee's avatar
Elliot Lee committed
130
131
      check_shift = 2;
      break;
132
    case GIMP_MEDIUM_CHECKS:
133
      check_mod   = 0x7;
Elliot Lee's avatar
Elliot Lee committed
134
135
      check_shift = 3;
      break;
136
    case GIMP_LARGE_CHECKS:
137
      check_mod   = 0xf;
Elliot Lee's avatar
Elliot Lee committed
138
139
140
141
      check_shift = 4;
      break;
    }

142
143
144
145
  g_free (render_check_buf);
  g_free (render_empty_buf);
  g_free (render_temp_buf);

146
147
#define MAX_PREVIEW_SIZE 256  /* EEK */

Elliot Lee's avatar
Elliot Lee committed
148
  /*  calculate check buffer for previews  */
149
  if (TRUE /* preview_size */)
Elliot Lee's avatar
Elliot Lee committed
150
    {
151
      render_check_buf = g_new (guchar, (MAX_PREVIEW_SIZE + 4) * 3);
152

153
      for (i = 0; i < (MAX_PREVIEW_SIZE + 4); i++)
Elliot Lee's avatar
Elliot Lee committed
154
155
156
	{
	  if (i & 0x4)
	    {
157
158
159
	      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];
Elliot Lee's avatar
Elliot Lee committed
160
161
162
	    }
	  else
	    {
163
164
165
	      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
166
167
	    }
	}
168

169
170
      render_empty_buf = g_new0 (guchar, (MAX_PREVIEW_SIZE + 4) * 3);
      render_temp_buf  = g_new  (guchar, (MAX_PREVIEW_SIZE + 4) * 3);
Elliot Lee's avatar
Elliot Lee committed
171
172
173
    }
  else
    {
174
175
176
      render_check_buf = NULL;
      render_empty_buf = NULL;
      render_temp_buf  = NULL;
Elliot Lee's avatar
Elliot Lee committed
177
178
179
180
181
182
183
    }
}

void
render_free (void)
{
  g_free (tile_buf);
184
  g_free (render_check_buf);
Elliot Lee's avatar
Elliot Lee committed
185
186
}

187

Elliot Lee's avatar
Elliot Lee committed
188
189
/*  Render Image functions  */

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
208
209


210
static RenderFunc render_funcs[6] =
Elliot Lee's avatar
Elliot Lee committed
211
{
212
213
214
215
216
217
  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
218
219
220
221
222
223
224
225
226
227
228
};


/*****************************************************************/
/*  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
229
230
231
232
233
gimp_display_shell_render (GimpDisplayShell *shell,
                           gint              x,
                           gint              y,
                           gint              w,
                           gint              h)
Elliot Lee's avatar
Elliot Lee committed
234
235
{
  RenderInfo info;
236
  gint       image_type;
237
238
239
  GList     *list;

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

241
  image_type = gimp_image_projection_type (shell->gdisp->gimage);
242

Elliot Lee's avatar
Elliot Lee committed
243
244
  if ((image_type < 0) || (image_type > 5))
    {
Manish Singh's avatar
Manish Singh committed
245
      g_message ("unknown gimage projection type: %d",
246
		 gimp_image_projection_type (shell->gdisp->gimage));
Elliot Lee's avatar
Elliot Lee committed
247
248
249
250
251
      return;
    }

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

256
  /* Currently, only RGBA and GRAYA projection types are used - the rest
257
258
   * are in case of future need.  -- austin, 28th Nov 1998.
   */
259
  if (image_type != GIMP_RGBA_IMAGE && image_type != GIMP_GRAYA_IMAGE)
260
    g_warning ("using untested projection type %d", image_type);
261

262
  (* render_funcs[image_type]) (&info);
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

  /*  apply filters to the rendered projection  */
  for (list = shell->cd_list; list; list = g_list_next (list))
    {
      ColorDisplayNode *node = (ColorDisplayNode *) list->data;

      node->cd_convert (node->cd_ID,
                        shell->render_buf,
                        w, h,
                        3,
                        3 * GIMP_DISPLAY_SHELL_RENDER_BUF_WIDTH);
    }

  /*  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
287
288
289
290
291
292
293
294
}


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

static void
295
render_image_indexed (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
296
297
298
{
  guchar *src;
  guchar *dest;
299
  guchar *cmap;
300
301
302
303
304
305
306
  gulong  val;
  gint    byte_order;
  gint    y, ye;
  gint    x, xe;
  gint    initial;
  gfloat  error;
  gfloat  step;
Elliot Lee's avatar
Elliot Lee committed
307

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

310
  y  = info->y;
Elliot Lee's avatar
Elliot Lee committed
311
312
313
  ye = info->y + info->h;
  xe = info->x + info->w;

314
315
  step = 1.0 / info->scaley;

316
317
  error  = y * step;
  error -= ((gint) error) - step;
318

Elliot Lee's avatar
Elliot Lee committed
319
320
321
322
323
324
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
325
      if (!initial && (error < 1.0))
326
327
328
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
329
330
331
332
333
      else
	{
	  src = info->src;
	  dest = info->dest;

334
	  g_return_if_fail (src != NULL);
335

336
337
338
339
	  for (x = info->x; x < xe; x++)
	    {
	      val = src[INDEXED_PIX] * 3;
	      src += 1;
340

341
342
343
344
345
	      dest[0] = cmap[val+0];
	      dest[1] = cmap[val+1];
	      dest[2] = cmap[val+2];
	      dest += 3;
	    }
Elliot Lee's avatar
Elliot Lee committed
346
347
348
349
	}

      info->dest += info->dest_bpl;

350
351
352
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
353
	{
354
355
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
356
	  info->src = render_image_tile_fault (info);
357
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
358
359
	}

360
      error += step;
Elliot Lee's avatar
Elliot Lee committed
361
362
363
364
    }
}

static void
365
render_image_indexed_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
366
367
368
{
  guchar *src;
  guchar *dest;
369
  guint  *alpha;
370
  guchar *cmap;
371
372
373
374
375
376
377
378
379
380
  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
381

382
  cmap = gimp_image_get_colormap (info->shell->gdisp->gimage);
383
  alpha = info->alpha;
Elliot Lee's avatar
Elliot Lee committed
384

385
  y  = info->y;
Elliot Lee's avatar
Elliot Lee committed
386
387
388
  ye = info->y + info->h;
  xe = info->x + info->w;

389
390
  step = 1.0 / info->scaley;

391
392
  error  = y * step;
  error -= ((gint) error) - step;
393

Elliot Lee's avatar
Elliot Lee committed
394
395
396
397
398
399
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
400
      if (!initial && (error < 1.0) && (y & check_mod))
401
402
403
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
404
405
406
407
408
      else
	{
	  src = info->src;
	  dest = info->dest;

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

411
	  g_return_if_fail (src != NULL);
Elliot Lee's avatar
Elliot Lee committed
412

413
414
415
416
417
418
419
420
	  for (x = info->x; x < xe; x++)
	    {
	      a = alpha[src[ALPHA_I_PIX]];
	      val = src[INDEXED_PIX] * 3;
	      src += 2;

	      if (dark_light & 0x1)
		{
421
422
423
		  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])];
424
425
426
		}
	      else
		{
427
428
429
		  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])];
430
431
432
433
434
		}

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

437
438
		if (((x + 1) & check_mod) == 0)
		  dark_light += 1;
Elliot Lee's avatar
Elliot Lee committed
439
440
441
442
443
	      }
	}

      info->dest += info->dest_bpl;

444
445
446
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
447
	{
448
449
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
450
	  info->src = render_image_tile_fault (info);
451
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
452
453
	}

454
      error += step;
Elliot Lee's avatar
Elliot Lee committed
455
456
457
458
    }
}

static void
459
render_image_gray (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
460
461
462
{
  guchar *src;
  guchar *dest;
463
464
465
466
467
468
469
470
471
  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
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))
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
      else
	{
	  src = info->src;
	  dest = info->dest;
494
	  
495
496
	  g_return_if_fail (src != NULL);

497
498
499
500
	  for (x = info->x; x < xe; x++)
	    {
	      val = src[GRAY_PIX];
	      src += 1;
Elliot Lee's avatar
Elliot Lee committed
501

502
503
504
505
506
	      dest[0] = val;
	      dest[1] = val;
	      dest[2] = val;
	      dest += 3;
	    }
Elliot Lee's avatar
Elliot Lee committed
507
508
509
510
	}

      info->dest += info->dest_bpl;

511
512
513
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
514
	{
515
516
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
517
	  info->src = render_image_tile_fault (info);
518
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
519
520
	}

521
      error += step;
Elliot Lee's avatar
Elliot Lee committed
522
523
524
525
    }
}

static void
526
render_image_gray_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
527
528
529
{
  guchar *src;
  guchar *dest;
530
531
532
533
534
535
536
537
538
539
  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
540
541
542

  alpha = info->alpha;

543
  y  = info->y;
Elliot Lee's avatar
Elliot Lee committed
544
545
546
  ye = info->y + info->h;
  xe = info->x + info->w;

547
548
  step = 1.0 / info->scaley;

549
550
  error  = y * step;
  error -= ((gint) error) - step;
551

Elliot Lee's avatar
Elliot Lee committed
552
553
554
555
556
557
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
558
      if (!initial && (error < 1.0) && (y & check_mod))
559
560
561
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
562
563
564
565
566
567
568
      else
	{
	  src = info->src;
	  dest = info->dest;

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

569
570
	  g_return_if_fail (src != NULL);

571
572
573
574
	  for (x = info->x; x < xe; x++)
	    {
	      a = alpha[src[ALPHA_G_PIX]];
	      if (dark_light & 0x1)
575
		val = render_blend_dark_check[(a | src[GRAY_PIX])];
576
	      else
577
		val = render_blend_light_check[(a | src[GRAY_PIX])];
578
579
580
581
582
583
584
585
586
587
	      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
588
589
590
591
	}

      info->dest += info->dest_bpl;

592
593
594
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
595
	{
596
597
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
598
	  info->src = render_image_tile_fault (info);
599
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
600
601
	}

602
      error += step;
Elliot Lee's avatar
Elliot Lee committed
603
604
605
606
    }
}

static void
607
render_image_rgb (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
608
609
610
{
  guchar *src;
  guchar *dest;
611
612
613
614
615
616
617
618
  gint    byte_order;
  gint    y, ye;
  gint    x, xe;
  gint    initial;
  gfloat  error;
  gfloat  step;

  y  = info->y;
Daniel Egger's avatar
Daniel Egger committed
619
620
621
622
  ye = info->y + info->h;
  xe = info->x + info->w;

  step = 1.0 / info->scaley;
623

624
625
  error  = y * step;
  error -= (gint) error - step;
626

Elliot Lee's avatar
Elliot Lee committed
627
  initial = TRUE;
Daniel Egger's avatar
Daniel Egger committed
628
  byte_order = info->byte_order;
Elliot Lee's avatar
Elliot Lee committed
629
630
631
632
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
633
      if (!initial && (error < 1.0))
634
635
636
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
637
638
639
640
641
      else
	{
	  src = info->src;
	  dest = info->dest;

642
	  g_return_if_fail (src != NULL);
643
644
645
646
647
648
649
	  
	  /* replace this with memcpy, or better yet, avoid it altogether? */
	  for (x = info->x; x < xe; x++)
	    {
	      dest[0] = src[0];
	      dest[1] = src[1];
	      dest[2] = src[2];
650

651
652
653
	      src += 3;
	      dest += 3;
	    }
Elliot Lee's avatar
Elliot Lee committed
654
655
656
657
	}

      info->dest += info->dest_bpl;

658
659
660
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
661
	{
662
663
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
664
	  info->src = render_image_tile_fault (info);
665
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
666
667
	}

668
      error += step;
Elliot Lee's avatar
Elliot Lee committed
669
670
671
672
    }
}

static void
673
render_image_rgb_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
674
675
676
{
  guchar *src;
  guchar *dest;
677
678
679
680
681
682
683
684
685
686
  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
687
688
689

  alpha = info->alpha;

690
  y  = info->y;
Daniel Egger's avatar
Daniel Egger committed
691
692
693
694
  ye = info->y + info->h;
  xe = info->x + info->w;

  step = 1.0 / info->scaley;
695

696
697
  error  = y * step;
  error -= ((gint) error) - step;
698

Elliot Lee's avatar
Elliot Lee committed
699
700
701
702
703
704
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
705
      if (!initial && (error < 1.0) && (y & check_mod))
706
707
708
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
709
710
711
712
713
714
715
      else
	{
	  src = info->src;
	  dest = info->dest;

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

716
717
	  g_return_if_fail (src != NULL);

718
719
720
721
722
	  for (x = info->x; x < xe; x++)
	    {
	      a = alpha[src[ALPHA_PIX]];
	      if (dark_light & 0x1)
		{
723
724
725
		  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])];
726
727
728
		}
	      else
		{
729
730
731
		  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])];
732
		}
Elliot Lee's avatar
Elliot Lee committed
733

734
	      src += 4;
Elliot Lee's avatar
Elliot Lee committed
735

736
737
738
739
	      dest[0] = r;
	      dest[1] = g;
	      dest[2] = b;
	      dest += 3;
Elliot Lee's avatar
Elliot Lee committed
740

741
742
743
	      if (((x + 1) & check_mod) == 0)
		dark_light += 1;
	    }
Elliot Lee's avatar
Elliot Lee committed
744
745
746
747
	}

      info->dest += info->dest_bpl;

748
749
750
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
751
	{
752
753
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
754
	  info->src = render_image_tile_fault (info);
755
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
756
757
	}

758
      error += step;
Elliot Lee's avatar
Elliot Lee committed
759
760
761
762
    }
}

static void
763
764
765
766
767
768
render_image_init_info (RenderInfo       *info,
			GimpDisplayShell *shell,
			gint              x,
			gint              y,
			gint              w,
			gint              h)
Elliot Lee's avatar
Elliot Lee committed
769
{
770
771
772
773
  info->shell      = shell;
  info->src_tiles  = gimp_image_projection (shell->gdisp->gimage);
  info->x          = x + shell->offset_x;
  info->y          = y + shell->offset_y;
774
775
  info->w          = w;
  info->h          = h;
776
777
778
779
780
781
782
783
  info->scalex     = SCALEFACTOR_X (shell->gdisp);
  info->scaley     = SCALEFACTOR_Y (shell->gdisp);
  info->src_x      = UNSCALEX (shell->gdisp, info->x);
  info->src_y      = UNSCALEY (shell->gdisp, info->y);
  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
784
  info->dest_width = info->w * info->dest_bpp;
785
  info->byte_order = GDK_MSB_FIRST;
786
787
  info->scale      = render_image_accelerate_scaling (w, info->x, info->scalex);
  info->alpha      = NULL;
Elliot Lee's avatar
Elliot Lee committed
788

789
  if (GIMP_IMAGE_TYPE_HAS_ALPHA (gimp_image_projection_type (shell->gdisp->gimage)))
Elliot Lee's avatar
Elliot Lee committed
790
    {
791
      info->alpha =
792
	render_image_init_alpha (gimp_image_projection_opacity (shell->gdisp->gimage) * 255.999);
Elliot Lee's avatar
Elliot Lee committed
793
794
795
796
    }
}

static guint*
797
render_image_init_alpha (gint mult)
Elliot Lee's avatar
Elliot Lee committed
798
799
{
  static guint *alpha_mult = NULL;
800
801
802
  static gint   alpha_val  = -1;

  gint i;
Elliot Lee's avatar
Elliot Lee committed
803
804
805
806

  if (alpha_val != mult)
    {
      if (!alpha_mult)
807
	alpha_mult = g_new (guint, 256);
Elliot Lee's avatar
Elliot Lee committed
808
809
810
811
812
813
814
815
816

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

  return alpha_mult;
}

817
static guchar *
818
819
820
render_image_accelerate_scaling (gint   width,
				 gint   start,
				 gfloat scalex)
Elliot Lee's avatar
Elliot Lee committed
821
822
{
  static guchar *scale = NULL;
823
824
825
826

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

828
829
  if (! scale)
    scale = g_new (guchar, GIMP_DISPLAY_SHELL_RENDER_BUF_WIDTH + 1);
Elliot Lee's avatar
Elliot Lee committed
830

831
  step = 1.0 / scalex;
Elliot Lee's avatar
Elliot Lee committed
832

833
  error = start * step;
834
  error -= ((gint) error) - step;
835
836

  for (i = 0; i <= width; i++)
837
838
839
840
    {
      scale[i] = ((gint) error);
      error += step - (gint) error;
    }
Elliot Lee's avatar
Elliot Lee committed
841
842
843
844

  return scale;
}

845
static guchar *
Elliot Lee's avatar
Elliot Lee committed
846
847
render_image_tile_fault (RenderInfo *info)
{
848
  Tile   *tile;
Elliot Lee's avatar
Elliot Lee committed
849
850
851
  guchar *data;
  guchar *dest;
  guchar *scale;
852
853
854
855
856
857
858
  gint    width;
  gint    tilex;
  gint    tiley;
  gint    srctilex, srctiley;
  gint    step;
  gint    bpp = info->src_bpp;
  gint    x, b;
Elliot Lee's avatar
Elliot Lee committed
859
860
861
862

  tilex = info->src_x / TILE_WIDTH;
  tiley = info->src_y / TILE_HEIGHT;

863
864
865
  tile = tile_manager_get_tile (info->src_tiles,
				srctilex=info->src_x, srctiley=info->src_y,
				TRUE, FALSE);
Elliot Lee's avatar
Elliot Lee committed
866
867
868
  if (!tile)
    return NULL;

scott's avatar
scott committed
869
870
871
  data = tile_data_pointer (tile, 
			    info->src_x % TILE_WIDTH,
			    info->src_y % TILE_HEIGHT);
Elliot Lee's avatar
Elliot Lee committed
872
  scale = info->scale;
873
  dest  = tile_buf;
Elliot Lee's avatar
Elliot Lee committed
874

875
  x     = info->src_x;
Elliot Lee's avatar
Elliot Lee committed
876
877
878
879
  width = info->w;

  while (width--)
    {
880
      for (b = 0; b < bpp; b++)
Elliot Lee's avatar
Elliot Lee committed
881
882
	*dest++ = data[b];

883
884
      step = *scale++;
      if (step != 0)
Elliot Lee's avatar
Elliot Lee committed
885
	{
886
887
	  x += step;
	  data += step * bpp;
Elliot Lee's avatar
Elliot Lee committed
888

Daniel Egger's avatar
Daniel Egger committed
889
	  if ((x >> tile_shift) != tilex)
Elliot Lee's avatar
Elliot Lee committed
890
	    {
891
	      tile_release (tile, FALSE);
Elliot Lee's avatar
Elliot Lee committed
892
893
	      tilex += 1;

894
895
	      tile = tile_manager_get_tile (info->src_tiles, srctilex=x,
					    srctiley=info->src_y, TRUE, FALSE);
Daniel Egger's avatar
Daniel Egger committed
896
897
898
	      if (!tile)
		return tile_buf;

scott's avatar
scott committed
899
900
901
	      data = tile_data_pointer (tile, 
			    x % TILE_WIDTH,
			    info->src_y % TILE_HEIGHT);
Elliot Lee's avatar
Elliot Lee committed
902
903
904
905
	    }
	}
    }

906
  tile_release (tile, FALSE);
907

Elliot Lee's avatar
Elliot Lee committed
908
909
  return tile_buf;
}