gimpdisplayshell-render.c 18.9 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
38
#include "gximage.h"

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


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


Elliot Lee's avatar
Elliot Lee committed
45
typedef struct _RenderInfo  RenderInfo;
46
47

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

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


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


void
94
95
render_setup (GimpCheckType check_type,
	      GimpCheckSize check_size)
Elliot Lee's avatar
Elliot Lee committed
96
{
97
98
99
100
101
102
  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
103
104
105
106

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

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

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

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

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

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

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

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

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

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

188

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

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


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


/*****************************************************************/
/*  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
230
231
232
233
234
render_image (GimpDisplay *gdisp,
	      gint         x,
	      gint         y,
	      gint         w,
	      gint         h)
Elliot Lee's avatar
Elliot Lee committed
235
236
{
  RenderInfo info;
237
  gint       image_type;
Elliot Lee's avatar
Elliot Lee committed
238
239
240

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

241
  image_type = gimp_image_projection_type (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 (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
260
261
  if (image_type != RGBA_GIMAGE && image_type != GRAYA_GIMAGE)
      g_warning ("using untested projection type %d", image_type);

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


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

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

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

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

290
291
  step = 1.0 / info->scaley;

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

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

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

310
	  g_return_if_fail (src != NULL);
311

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

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

      info->dest += info->dest_bpl;

326
327
328
      initial = FALSE;

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

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

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

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

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

365
366
  step = 1.0 / info->scaley;

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

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

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

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

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

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

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

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

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

      info->dest += info->dest_bpl;

420
421
422
      initial = FALSE;

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

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

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

451
452
  step = 1.0 / info->scaley;

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

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

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

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

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

      info->dest += info->dest_bpl;

487
488
489
      initial = FALSE;

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

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

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

  alpha = info->alpha;

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

523
524
  step = 1.0 / info->scaley;

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

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

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

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

545
546
	  g_return_if_fail (src != NULL);

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

      info->dest += info->dest_bpl;

568
569
570
      initial = FALSE;

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

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

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

  step = 1.0 / info->scaley;
599

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

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

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

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

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

      info->dest += info->dest_bpl;

634
635
636
      initial = FALSE;

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

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

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

  alpha = info->alpha;

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

  step = 1.0 / info->scaley;
671

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

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

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

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

692
693
	  g_return_if_fail (src != NULL);

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

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

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

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

      info->dest += info->dest_bpl;

724
725
726
      initial = FALSE;

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

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

static void
739
740
741
742
743
744
render_image_init_info (RenderInfo  *info,
			GimpDisplay *gdisp,
			gint         x,
			gint         y,
			gint         w,
			gint         h)
Elliot Lee's avatar
Elliot Lee committed
745
{
746
747
  info->gdisp      = gdisp;
  info->src_tiles  = gimp_image_projection (gdisp->gimage);
748
749
  info->x          = x + GIMP_DISPLAY_SHELL (gdisp->shell)->offset_x;
  info->y          = y + GIMP_DISPLAY_SHELL (gdisp->shell)->offset_y;
750
751
752
753
754
755
756
757
758
759
  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
760
761
  info->dest_width = info->w * info->dest_bpp;
  info->byte_order = gximage_get_byte_order ();
762
763
  info->scale      = render_image_accelerate_scaling (w, info->x, info->scalex);
  info->alpha      = NULL;
Elliot Lee's avatar
Elliot Lee committed
764

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

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

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

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

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

  return alpha_mult;
}

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

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

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

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

816
817
818
819
820
821
822
823
  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
824
825
826
827

  return scale;
}

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

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

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

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

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

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

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

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

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

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

889
  tile_release (tile, FALSE);
890

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