gimpdisplayshell-render.c 18.8 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
32
#include "core/gimpimage.h"

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

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


41
42
43
#define MAX_PREVIEW_SIZE 256  /* EEK */


Elliot Lee's avatar
Elliot Lee committed
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
  GimpDisplay *gdisp;
Elliot Lee's avatar
Elliot Lee committed
51
  TileManager *src_tiles;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  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
};


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


void
93
94
render_setup (GimpCheckType check_type,
	      GimpCheckSize check_size)
Elliot Lee's avatar
Elliot Lee committed
95
{
96
97
98
99
100
101
  gint i, j;

  if (check_type < LIGHT_CHECKS || check_type > BLACK_ONLY)
    g_error ("invalid check_type argument to render_setup: %d", check_type);
  if (check_size < SMALL_CHECKS || check_size > LARGE_CHECKS)
    g_error ("invalid check_size argument to render_setup: %d", check_size);
Elliot Lee's avatar
Elliot Lee committed
102
103
104
105

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

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

114
115
116
117
  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
118
119
120
121

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

  switch (check_size)
    {
    case SMALL_CHECKS:
131
      check_mod   = 0x3;
Elliot Lee's avatar
Elliot Lee committed
132
133
134
      check_shift = 2;
      break;
    case MEDIUM_CHECKS:
135
      check_mod   = 0x7;
Elliot Lee's avatar
Elliot Lee committed
136
137
138
      check_shift = 3;
      break;
    case LARGE_CHECKS:
139
      check_mod   = 0xf;
Elliot Lee's avatar
Elliot Lee committed
140
141
142
143
      check_shift = 4;
      break;
    }

144
145
146
147
  g_free (render_check_buf);
  g_free (render_empty_buf);
  g_free (render_temp_buf);

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
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,
198
						 GimpDisplay  *gdisp,
199
200
201
202
203
204
205
206
207
						 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
render_image (GimpDisplay *gdisp,
	      gint         x,
	      gint         y,
	      gint         w,
	      gint         h)
Elliot Lee's avatar
Elliot Lee committed
234
235
{
  RenderInfo info;
236
  gint       image_type;
Elliot Lee's avatar
Elliot Lee committed
237
238
239

  render_image_init_info (&info, gdisp, x, y, w, h);

240
  image_type = gimp_image_projection_type (gdisp->gimage);
241

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

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

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

261
  (* render_funcs[image_type]) (&info);
Elliot Lee's avatar
Elliot Lee committed
262
263
264
265
266
267
268
269
}


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

static void
270
render_image_indexed (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
271
272
273
{
  guchar *src;
  guchar *dest;
274
  guchar *cmap;
275
276
277
278
279
280
281
  gulong  val;
  gint    byte_order;
  gint    y, ye;
  gint    x, xe;
  gint    initial;
  gfloat  error;
  gfloat  step;
Elliot Lee's avatar
Elliot Lee committed
282

283
  cmap = gimp_image_cmap (info->gdisp->gimage);
Elliot Lee's avatar
Elliot Lee committed
284

285
  y  = info->y;
Elliot Lee's avatar
Elliot Lee committed
286
287
288
  ye = info->y + info->h;
  xe = info->x + info->w;

289
290
  step = 1.0 / info->scaley;

291
292
  error  = y * step;
  error -= ((gint) error) - step;
293

Elliot Lee's avatar
Elliot Lee committed
294
295
296
297
298
299
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
300
      if (!initial && (error < 1.0))
301
302
303
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
304
305
306
307
308
      else
	{
	  src = info->src;
	  dest = info->dest;

309
	  g_return_if_fail (src != NULL);
310

311
312
313
314
	  for (x = info->x; x < xe; x++)
	    {
	      val = src[INDEXED_PIX] * 3;
	      src += 1;
315

316
317
318
319
320
	      dest[0] = cmap[val+0];
	      dest[1] = cmap[val+1];
	      dest[2] = cmap[val+2];
	      dest += 3;
	    }
Elliot Lee's avatar
Elliot Lee committed
321
322
323
324
	}

      info->dest += info->dest_bpl;

325
326
327
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
328
	{
329
330
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
331
	  info->src = render_image_tile_fault (info);
332
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
333
334
	}

335
      error += step;
Elliot Lee's avatar
Elliot Lee committed
336
337
338
339
    }
}

static void
340
render_image_indexed_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
341
342
343
{
  guchar *src;
  guchar *dest;
344
  guint  *alpha;
345
  guchar *cmap;
346
347
348
349
350
351
352
353
354
355
  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
356

357
  cmap = gimp_image_cmap (info->gdisp->gimage);
358
  alpha = info->alpha;
Elliot Lee's avatar
Elliot Lee committed
359

360
  y  = info->y;
Elliot Lee's avatar
Elliot Lee committed
361
362
363
  ye = info->y + info->h;
  xe = info->x + info->w;

364
365
  step = 1.0 / info->scaley;

366
367
  error  = y * step;
  error -= ((gint) error) - step;
368

Elliot Lee's avatar
Elliot Lee committed
369
370
371
372
373
374
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
375
      if (!initial && (error < 1.0) && (y & check_mod))
376
377
378
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
379
380
381
382
383
      else
	{
	  src = info->src;
	  dest = info->dest;

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

386
	  g_return_if_fail (src != NULL);
Elliot Lee's avatar
Elliot Lee committed
387

388
389
390
391
392
393
394
395
	  for (x = info->x; x < xe; x++)
	    {
	      a = alpha[src[ALPHA_I_PIX]];
	      val = src[INDEXED_PIX] * 3;
	      src += 2;

	      if (dark_light & 0x1)
		{
396
397
398
		  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])];
399
400
401
		}
	      else
		{
402
403
404
		  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])];
405
406
407
408
409
		}

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

412
413
		if (((x + 1) & check_mod) == 0)
		  dark_light += 1;
Elliot Lee's avatar
Elliot Lee committed
414
415
416
417
418
	      }
	}

      info->dest += info->dest_bpl;

419
420
421
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
422
	{
423
424
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
425
	  info->src = render_image_tile_fault (info);
426
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
427
428
	}

429
      error += step;
Elliot Lee's avatar
Elliot Lee committed
430
431
432
433
    }
}

static void
434
render_image_gray (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
435
436
437
{
  guchar *src;
  guchar *dest;
438
439
440
441
442
443
444
445
446
  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
447
448
449
  ye = info->y + info->h;
  xe = info->x + info->w;

450
451
  step = 1.0 / info->scaley;

452
453
  error  = y * step;
  error -= ((gint) error) - step;
454

Elliot Lee's avatar
Elliot Lee committed
455
456
457
458
459
460
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
461
      if (!initial && (error < 1.0))
462
463
464
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
465
466
467
468
      else
	{
	  src = info->src;
	  dest = info->dest;
469
	  
470
471
	  g_return_if_fail (src != NULL);

472
473
474
475
	  for (x = info->x; x < xe; x++)
	    {
	      val = src[GRAY_PIX];
	      src += 1;
Elliot Lee's avatar
Elliot Lee committed
476

477
478
479
480
481
	      dest[0] = val;
	      dest[1] = val;
	      dest[2] = val;
	      dest += 3;
	    }
Elliot Lee's avatar
Elliot Lee committed
482
483
484
485
	}

      info->dest += info->dest_bpl;

486
487
488
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
489
	{
490
491
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
492
	  info->src = render_image_tile_fault (info);
493
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
494
495
	}

496
      error += step;
Elliot Lee's avatar
Elliot Lee committed
497
498
499
500
    }
}

static void
501
render_image_gray_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
502
503
504
{
  guchar *src;
  guchar *dest;
505
506
507
508
509
510
511
512
513
514
  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
515
516
517

  alpha = info->alpha;

518
  y  = info->y;
Elliot Lee's avatar
Elliot Lee committed
519
520
521
  ye = info->y + info->h;
  xe = info->x + info->w;

522
523
  step = 1.0 / info->scaley;

524
525
  error  = y * step;
  error -= ((gint) error) - step;
526

Elliot Lee's avatar
Elliot Lee committed
527
528
529
530
531
532
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
533
      if (!initial && (error < 1.0) && (y & check_mod))
534
535
536
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
537
538
539
540
541
542
543
      else
	{
	  src = info->src;
	  dest = info->dest;

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

544
545
	  g_return_if_fail (src != NULL);

546
547
548
549
	  for (x = info->x; x < xe; x++)
	    {
	      a = alpha[src[ALPHA_G_PIX]];
	      if (dark_light & 0x1)
550
		val = render_blend_dark_check[(a | src[GRAY_PIX])];
551
	      else
552
		val = render_blend_light_check[(a | src[GRAY_PIX])];
553
554
555
556
557
558
559
560
561
562
	      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
563
564
565
566
	}

      info->dest += info->dest_bpl;

567
568
569
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
570
	{
571
572
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
573
	  info->src = render_image_tile_fault (info);
574
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
575
576
	}

577
      error += step;
Elliot Lee's avatar
Elliot Lee committed
578
579
580
581
    }
}

static void
582
render_image_rgb (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
583
584
585
{
  guchar *src;
  guchar *dest;
586
587
588
589
590
591
592
593
  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
594
595
596
597
  ye = info->y + info->h;
  xe = info->x + info->w;

  step = 1.0 / info->scaley;
598

599
600
  error  = y * step;
  error -= (gint) error - step;
601

Elliot Lee's avatar
Elliot Lee committed
602
  initial = TRUE;
Daniel Egger's avatar
Daniel Egger committed
603
  byte_order = info->byte_order;
Elliot Lee's avatar
Elliot Lee committed
604
605
606
607
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
608
      if (!initial && (error < 1.0))
609
610
611
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
612
613
614
615
616
      else
	{
	  src = info->src;
	  dest = info->dest;

617
	  g_return_if_fail (src != NULL);
618
619
620
621
622
623
624
	  
	  /* 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];
625

626
627
628
	      src += 3;
	      dest += 3;
	    }
Elliot Lee's avatar
Elliot Lee committed
629
630
631
632
	}

      info->dest += info->dest_bpl;

633
634
635
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
636
	{
637
638
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
639
	  info->src = render_image_tile_fault (info);
640
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
641
642
	}

643
      error += step;
Elliot Lee's avatar
Elliot Lee committed
644
645
646
647
    }
}

static void
648
render_image_rgb_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
649
650
651
{
  guchar *src;
  guchar *dest;
652
653
654
655
656
657
658
659
660
661
  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
662
663
664

  alpha = info->alpha;

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

  step = 1.0 / info->scaley;
670

671
672
  error  = y * step;
  error -= ((gint) error) - step;
673

Elliot Lee's avatar
Elliot Lee committed
674
675
676
677
678
679
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
680
      if (!initial && (error < 1.0) && (y & check_mod))
681
682
683
	{
	  memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
	}
Elliot Lee's avatar
Elliot Lee committed
684
685
686
687
688
689
690
      else
	{
	  src = info->src;
	  dest = info->dest;

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

691
692
	  g_return_if_fail (src != NULL);

693
694
695
696
697
	  for (x = info->x; x < xe; x++)
	    {
	      a = alpha[src[ALPHA_PIX]];
	      if (dark_light & 0x1)
		{
698
699
700
		  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])];
701
702
703
		}
	      else
		{
704
705
706
		  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])];
707
		}
Elliot Lee's avatar
Elliot Lee committed
708

709
	      src += 4;
Elliot Lee's avatar
Elliot Lee committed
710

711
712
713
714
	      dest[0] = r;
	      dest[1] = g;
	      dest[2] = b;
	      dest += 3;
Elliot Lee's avatar
Elliot Lee committed
715

716
717
718
	      if (((x + 1) & check_mod) == 0)
		dark_light += 1;
	    }
Elliot Lee's avatar
Elliot Lee committed
719
720
721
722
	}

      info->dest += info->dest_bpl;

723
724
725
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
726
	{
727
728
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
729
	  info->src = render_image_tile_fault (info);
730
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
731
732
	}

733
      error += step;
Elliot Lee's avatar
Elliot Lee committed
734
735
736
737
    }
}

static void
738
739
740
741
742
743
render_image_init_info (RenderInfo  *info,
			GimpDisplay *gdisp,
			gint         x,
			gint         y,
			gint         w,
			gint         h)
Elliot Lee's avatar
Elliot Lee committed
744
{
745
746
747
748
749
750
751
752
753
754
755
756
757
758
  info->gdisp      = gdisp;
  info->src_tiles  = gimp_image_projection (gdisp->gimage);
  info->x          = x + gdisp->offset_x;
  info->y          = y + gdisp->offset_y;
  info->w          = w;
  info->h          = h;
  info->scalex     = SCALEFACTOR_X (gdisp);
  info->scaley     = SCALEFACTOR_Y (gdisp);
  info->src_x      = UNSCALEX (gdisp, info->x);
  info->src_y      = UNSCALEY (gdisp, info->y);
  info->src_bpp    = gimp_image_projection_bytes (gdisp->gimage);
  info->dest       = gximage_get_data ();
  info->dest_bpp   = gximage_get_bpp ();
  info->dest_bpl   = gximage_get_bpl ();
Elliot Lee's avatar
Elliot Lee committed
759
760
  info->dest_width = info->w * info->dest_bpp;
  info->byte_order = gximage_get_byte_order ();
761
762
  info->scale      = render_image_accelerate_scaling (w, info->x, info->scalex);
  info->alpha      = NULL;
Elliot Lee's avatar
Elliot Lee committed
763

764
  switch (gimp_image_projection_type (gdisp->gimage))
Elliot Lee's avatar
Elliot Lee committed
765
766
767
768
    {
    case RGBA_GIMAGE:
    case GRAYA_GIMAGE:
    case INDEXEDA_GIMAGE:
769
770
      info->alpha =
	render_image_init_alpha (gimp_image_projection_opacity (gdisp->gimage));
Elliot Lee's avatar
Elliot Lee committed
771
      break;
772
773
774
    default:
      /* nothing special needs doing */
      break;
Elliot Lee's avatar
Elliot Lee committed
775
776
777
778
    }
}

static guint*
779
render_image_init_alpha (gint mult)
Elliot Lee's avatar
Elliot Lee committed
780
781
{
  static guint *alpha_mult = NULL;
782
783
784
  static gint   alpha_val  = -1;

  gint i;
Elliot Lee's avatar
Elliot Lee committed
785
786
787
788

  if (alpha_val != mult)
    {
      if (!alpha_mult)
789
	alpha_mult = g_new (guint, 256);
Elliot Lee's avatar
Elliot Lee committed
790
791
792
793
794
795
796
797
798
799

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

  return alpha_mult;
}

static guchar*
800
801
802
render_image_accelerate_scaling (gint   width,
				 gint   start,
				 gfloat scalex)
Elliot Lee's avatar
Elliot Lee committed
803
804
{
  static guchar *scale = NULL;
805
806
807
808

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

810
811
  if (!scale)
    scale = g_new (guchar, GXIMAGE_WIDTH + 1);
Elliot Lee's avatar
Elliot Lee committed
812

813
  step = 1.0 / scalex;
Elliot Lee's avatar
Elliot Lee committed
814

815
816
817
818
819
820
821
822
  error = start * step;
  error -= ((int)error) - step;

  for (i = 0; i <= width; i++)
  {
    scale[i] = ((int)error);
    error += step - (int)error;
  }    
Elliot Lee's avatar
Elliot Lee committed
823
824
825
826

  return scale;
}

827
static guchar *
Elliot Lee's avatar
Elliot Lee committed
828
829
render_image_tile_fault (RenderInfo *info)
{
830
  Tile   *tile;
Elliot Lee's avatar
Elliot Lee committed
831
832
833
  guchar *data;
  guchar *dest;
  guchar *scale;
834
835
836
837
838
839
840
  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
841
842
843
844

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

845
846
847
  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
848
849
850
  if (!tile)
    return NULL;

scott's avatar
scott committed
851
852
853
  data = tile_data_pointer (tile, 
			    info->src_x % TILE_WIDTH,
			    info->src_y % TILE_HEIGHT);
Elliot Lee's avatar
Elliot Lee committed
854
  scale = info->scale;
855
  dest  = tile_buf;
Elliot Lee's avatar
Elliot Lee committed
856

857
  x     = info->src_x;
Elliot Lee's avatar
Elliot Lee committed
858
859
860
861
  width = info->w;

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

865
866
      step = *scale++;
      if (step != 0)
Elliot Lee's avatar
Elliot Lee committed
867
	{
868
869
	  x += step;
	  data += step * bpp;
Elliot Lee's avatar
Elliot Lee committed
870

Daniel Egger's avatar
Daniel Egger committed
871
	  if ((x >> tile_shift) != tilex)
Elliot Lee's avatar
Elliot Lee committed
872
	    {
873
	      tile_release (tile, FALSE);
Elliot Lee's avatar
Elliot Lee committed
874
875
	      tilex += 1;

876
877
	      tile = tile_manager_get_tile (info->src_tiles, srctilex=x,
					    srctiley=info->src_y, TRUE, FALSE);
Daniel Egger's avatar
Daniel Egger committed
878
879
880
	      if (!tile)
		return tile_buf;

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

888
  tile_release (tile, FALSE);
889

Elliot Lee's avatar
Elliot Lee committed
890
891
  return tile_buf;
}