gimpdisplayshell-render.c 20.2 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-render.h"
37

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


typedef struct _RenderInfo  RenderInfo;
42
43

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

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


/*  accelerate transparency of image scaling  */
67
68
69
70
71
72
73
74
75
76
77
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
78
79
80
81
static guchar  check_combos[6][2] =
{
  { 204, 255 },
  { 102, 153 },
82
  {   0,  51 },
Elliot Lee's avatar
Elliot Lee committed
83
84
  { 255, 255 },
  { 127, 127 },
85
  {   0,   0 }
Elliot Lee's avatar
Elliot Lee committed
86
87
88
89
};


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

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

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

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

111
112
113
114
  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
115
116
117
118

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

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

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

145
146
#define MAX_PREVIEW_SIZE 256  /* EEK */

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

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

168
169
      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
170
171
172
    }
  else
    {
173
174
175
      render_check_buf = NULL;
      render_empty_buf = NULL;
      render_temp_buf  = NULL;
Elliot Lee's avatar
Elliot Lee committed
176
177
178
179
180
181
182
    }
}

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

186

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

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


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


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

237
238
239
240
241
#ifdef DISPLAY_FILTERS
  GList     *list;
#endif

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

243
  image_type = gimp_image_projection_type (shell->gdisp->gimage);
244

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

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

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

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

#if 0
#ifdef DISPLAY_FILTERS
  /*  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);
    }
#endif /* DISPLAY_FILTERS */
#endif

  /*  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
293
294
295
296
297
298
299
300
}


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

static void
301
render_image_indexed (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
302
303
304
{
  guchar *src;
  guchar *dest;
305
  guchar *cmap;
306
307
308
309
310
311
312
  gulong  val;
  gint    byte_order;
  gint    y, ye;
  gint    x, xe;
  gint    initial;
  gfloat  error;
  gfloat  step;
Elliot Lee's avatar
Elliot Lee committed
313

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

316
  y  = info->y;
Elliot Lee's avatar
Elliot Lee committed
317
318
319
  ye = info->y + info->h;
  xe = info->x + info->w;

320
321
  step = 1.0 / info->scaley;

322
323
  error  = y * step;
  error -= ((gint) error) - step;
324

Elliot Lee's avatar
Elliot Lee committed
325
326
327
328
329
330
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
331
      if (!initial && (error < 1.0))
332
333
334
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
335
336
337
338
339
      else
	{
	  src = info->src;
	  dest = info->dest;

340
	  g_return_if_fail (src != NULL);
341

342
343
344
345
	  for (x = info->x; x < xe; x++)
	    {
	      val = src[INDEXED_PIX] * 3;
	      src += 1;
346

347
348
349
350
351
	      dest[0] = cmap[val+0];
	      dest[1] = cmap[val+1];
	      dest[2] = cmap[val+2];
	      dest += 3;
	    }
Elliot Lee's avatar
Elliot Lee committed
352
353
354
355
	}

      info->dest += info->dest_bpl;

356
357
358
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
359
	{
360
361
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
362
	  info->src = render_image_tile_fault (info);
363
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
364
365
	}

366
      error += step;
Elliot Lee's avatar
Elliot Lee committed
367
368
369
370
    }
}

static void
371
render_image_indexed_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
372
373
374
{
  guchar *src;
  guchar *dest;
375
  guint  *alpha;
376
  guchar *cmap;
377
378
379
380
381
382
383
384
385
386
  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
387

388
  cmap = gimp_image_get_colormap (info->shell->gdisp->gimage);
389
  alpha = info->alpha;
Elliot Lee's avatar
Elliot Lee committed
390

391
  y  = info->y;
Elliot Lee's avatar
Elliot Lee committed
392
393
394
  ye = info->y + info->h;
  xe = info->x + info->w;

395
396
  step = 1.0 / info->scaley;

397
398
  error  = y * step;
  error -= ((gint) error) - step;
399

Elliot Lee's avatar
Elliot Lee committed
400
401
402
403
404
405
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
406
      if (!initial && (error < 1.0) && (y & check_mod))
407
408
409
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
410
411
412
413
414
      else
	{
	  src = info->src;
	  dest = info->dest;

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

417
	  g_return_if_fail (src != NULL);
Elliot Lee's avatar
Elliot Lee committed
418

419
420
421
422
423
424
425
426
	  for (x = info->x; x < xe; x++)
	    {
	      a = alpha[src[ALPHA_I_PIX]];
	      val = src[INDEXED_PIX] * 3;
	      src += 2;

	      if (dark_light & 0x1)
		{
427
428
429
		  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])];
430
431
432
		}
	      else
		{
433
434
435
		  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])];
436
437
438
439
440
		}

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

443
444
		if (((x + 1) & check_mod) == 0)
		  dark_light += 1;
Elliot Lee's avatar
Elliot Lee committed
445
446
447
448
449
	      }
	}

      info->dest += info->dest_bpl;

450
451
452
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
453
	{
454
455
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
456
	  info->src = render_image_tile_fault (info);
457
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
458
459
	}

460
      error += step;
Elliot Lee's avatar
Elliot Lee committed
461
462
463
464
    }
}

static void
465
render_image_gray (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
466
467
468
{
  guchar *src;
  guchar *dest;
469
470
471
472
473
474
475
476
477
  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
478
479
480
  ye = info->y + info->h;
  xe = info->x + info->w;

481
482
  step = 1.0 / info->scaley;

483
484
  error  = y * step;
  error -= ((gint) error) - step;
485

Elliot Lee's avatar
Elliot Lee committed
486
487
488
489
490
491
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
492
      if (!initial && (error < 1.0))
493
494
495
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
496
497
498
499
      else
	{
	  src = info->src;
	  dest = info->dest;
500
	  
501
502
	  g_return_if_fail (src != NULL);

503
504
505
506
	  for (x = info->x; x < xe; x++)
	    {
	      val = src[GRAY_PIX];
	      src += 1;
Elliot Lee's avatar
Elliot Lee committed
507

508
509
510
511
512
	      dest[0] = val;
	      dest[1] = val;
	      dest[2] = val;
	      dest += 3;
	    }
Elliot Lee's avatar
Elliot Lee committed
513
514
515
516
	}

      info->dest += info->dest_bpl;

517
518
519
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
520
	{
521
522
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
523
	  info->src = render_image_tile_fault (info);
524
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
525
526
	}

527
      error += step;
Elliot Lee's avatar
Elliot Lee committed
528
529
530
531
    }
}

static void
532
render_image_gray_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
533
534
535
{
  guchar *src;
  guchar *dest;
536
537
538
539
540
541
542
543
544
545
  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
546
547
548

  alpha = info->alpha;

549
  y  = info->y;
Elliot Lee's avatar
Elliot Lee committed
550
551
552
  ye = info->y + info->h;
  xe = info->x + info->w;

553
554
  step = 1.0 / info->scaley;

555
556
  error  = y * step;
  error -= ((gint) error) - step;
557

Elliot Lee's avatar
Elliot Lee committed
558
559
560
561
562
563
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
564
      if (!initial && (error < 1.0) && (y & check_mod))
565
566
567
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
568
569
570
571
572
573
574
      else
	{
	  src = info->src;
	  dest = info->dest;

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

575
576
	  g_return_if_fail (src != NULL);

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

      info->dest += info->dest_bpl;

598
599
600
      initial = FALSE;

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

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

static void
613
render_image_rgb (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
614
615
616
{
  guchar *src;
  guchar *dest;
617
618
619
620
621
622
623
624
  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
625
626
627
628
  ye = info->y + info->h;
  xe = info->x + info->w;

  step = 1.0 / info->scaley;
629

630
631
  error  = y * step;
  error -= (gint) error - step;
632

Elliot Lee's avatar
Elliot Lee committed
633
  initial = TRUE;
Daniel Egger's avatar
Daniel Egger committed
634
  byte_order = info->byte_order;
Elliot Lee's avatar
Elliot Lee committed
635
636
637
638
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
639
      if (!initial && (error < 1.0))
640
641
642
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
643
644
645
646
647
      else
	{
	  src = info->src;
	  dest = info->dest;

648
	  g_return_if_fail (src != NULL);
649
650
651
652
653
654
655
	  
	  /* 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];
656

657
658
659
	      src += 3;
	      dest += 3;
	    }
Elliot Lee's avatar
Elliot Lee committed
660
661
662
663
	}

      info->dest += info->dest_bpl;

664
665
666
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
667
	{
668
669
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
670
	  info->src = render_image_tile_fault (info);
671
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
672
673
	}

674
      error += step;
Elliot Lee's avatar
Elliot Lee committed
675
676
677
678
    }
}

static void
679
render_image_rgb_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
680
681
682
{
  guchar *src;
  guchar *dest;
683
684
685
686
687
688
689
690
691
692
  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
693
694
695

  alpha = info->alpha;

696
  y  = info->y;
Daniel Egger's avatar
Daniel Egger committed
697
698
699
700
  ye = info->y + info->h;
  xe = info->x + info->w;

  step = 1.0 / info->scaley;
701

702
703
  error  = y * step;
  error -= ((gint) error) - step;
704

Elliot Lee's avatar
Elliot Lee committed
705
706
707
708
709
710
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
711
      if (!initial && (error < 1.0) && (y & check_mod))
712
713
714
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
715
716
717
718
719
720
721
      else
	{
	  src = info->src;
	  dest = info->dest;

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

722
723
	  g_return_if_fail (src != NULL);

724
725
726
727
728
	  for (x = info->x; x < xe; x++)
	    {
	      a = alpha[src[ALPHA_PIX]];
	      if (dark_light & 0x1)
		{
729
730
731
		  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])];
732
733
734
		}
	      else
		{
735
736
737
		  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])];
738
		}
Elliot Lee's avatar
Elliot Lee committed
739

740
	      src += 4;
Elliot Lee's avatar
Elliot Lee committed
741

742
743
744
745
	      dest[0] = r;
	      dest[1] = g;
	      dest[2] = b;
	      dest += 3;
Elliot Lee's avatar
Elliot Lee committed
746

747
748
749
	      if (((x + 1) & check_mod) == 0)
		dark_light += 1;
	    }
Elliot Lee's avatar
Elliot Lee committed
750
751
752
753
	}

      info->dest += info->dest_bpl;

754
755
756
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
757
	{
758
759
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
760
	  info->src = render_image_tile_fault (info);
761
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
762
763
	}

764
      error += step;
Elliot Lee's avatar
Elliot Lee committed
765
766
767
768
    }
}

static void
769
770
771
772
773
774
render_image_init_info (RenderInfo       *info,
			GimpDisplayShell *shell,
			gint              x,
			gint              y,
			gint              w,
			gint              h)
Elliot Lee's avatar
Elliot Lee committed
775
{
776
777
778
779
  info->shell      = shell;
  info->src_tiles  = gimp_image_projection (shell->gdisp->gimage);
  info->x          = x + shell->offset_x;
  info->y          = y + shell->offset_y;
780
781
  info->w          = w;
  info->h          = h;
782
783
784
785
786
787
788
789
  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
790
  info->dest_width = info->w * info->dest_bpp;
791
  info->byte_order = GDK_MSB_FIRST;
792
793
  info->scale      = render_image_accelerate_scaling (w, info->x, info->scalex);
  info->alpha      = NULL;
Elliot Lee's avatar
Elliot Lee committed
794

795
  if (GIMP_IMAGE_TYPE_HAS_ALPHA (gimp_image_projection_type (shell->gdisp->gimage)))
Elliot Lee's avatar
Elliot Lee committed
796
    {
797
      info->alpha =
798
	render_image_init_alpha (gimp_image_projection_opacity (shell->gdisp->gimage));
Elliot Lee's avatar
Elliot Lee committed
799
800
801
802
    }
}

static guint*
803
render_image_init_alpha (gint mult)
Elliot Lee's avatar
Elliot Lee committed
804
805
{
  static guint *alpha_mult = NULL;
806
807
808
  static gint   alpha_val  = -1;

  gint i;
Elliot Lee's avatar
Elliot Lee committed
809
810
811
812

  if (alpha_val != mult)
    {
      if (!alpha_mult)
813
	alpha_mult = g_new (guint, 256);
Elliot Lee's avatar
Elliot Lee committed
814
815
816
817
818
819
820
821
822

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

  return alpha_mult;
}

823
static guchar *
824
825
826
render_image_accelerate_scaling (gint   width,
				 gint   start,
				 gfloat scalex)
Elliot Lee's avatar
Elliot Lee committed
827
828
{
  static guchar *scale = NULL;
829
830
831
832

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

834
835
  if (! scale)
    scale = g_new (guchar, GIMP_DISPLAY_SHELL_RENDER_BUF_WIDTH + 1);
Elliot Lee's avatar
Elliot Lee committed
836

837
  step = 1.0 / scalex;
Elliot Lee's avatar
Elliot Lee committed
838

839
  error = start * step;
840
  error -= ((gint) error) - step;
841
842

  for (i = 0; i <= width; i++)
843
844
845
846
    {
      scale[i] = ((gint) error);
      error += step - (gint) error;
    }
Elliot Lee's avatar
Elliot Lee committed
847
848
849
850

  return scale;
}

851
static guchar *
Elliot Lee's avatar
Elliot Lee committed
852
853
render_image_tile_fault (RenderInfo *info)
{
854
  Tile   *tile;
Elliot Lee's avatar
Elliot Lee committed
855
856
857
  guchar *data;
  guchar *dest;
  guchar *scale;
858
859
860
861
862
863
864
  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
865
866
867
868

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

869
870
871
  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
872
873
874
  if (!tile)
    return NULL;

scott's avatar
scott committed
875
876
877
  data = tile_data_pointer (tile, 
			    info->src_x % TILE_WIDTH,
			    info->src_y % TILE_HEIGHT);
Elliot Lee's avatar
Elliot Lee committed
878
  scale = info->scale;
879
  dest  = tile_buf;
Elliot Lee's avatar
Elliot Lee committed
880

881
  x     = info->src_x;
Elliot Lee's avatar
Elliot Lee committed
882
883
884
885
  width = info->w;

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

889
890
      step = *scale++;
      if (step != 0)
Elliot Lee's avatar
Elliot Lee committed
891
	{
892
893
	  x += step;
	  data += step * bpp;
Elliot Lee's avatar
Elliot Lee committed
894

Daniel Egger's avatar
Daniel Egger committed
895
	  if ((x >> tile_shift) != tilex)
Elliot Lee's avatar
Elliot Lee committed
896
	    {
897
	      tile_release (tile, FALSE);
Elliot Lee's avatar
Elliot Lee committed
898
899
	      tilex += 1;

900
901
	      tile = tile_manager_get_tile (info->src_tiles, srctilex=x,
					    srctiley=info->src_y, TRUE, FALSE);
Daniel Egger's avatar
Daniel Egger committed
902
903
904
	      if (!tile)
		return tile_buf;

scott's avatar
scott committed
905
906
907
	      data = tile_data_pointer (tile, 
			    x % TILE_WIDTH,
			    info->src_y % TILE_HEIGHT);
Elliot Lee's avatar
Elliot Lee committed
908
909
910
911
	    }
	}
    }

912
  tile_release (tile, FALSE);
913

Elliot Lee's avatar
Elliot Lee committed
914
915
  return tile_buf;
}