gimpdisplayshell-render.c 17.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
26
27
#include <gtk/gtk.h>

#include "apptypes.h"

Elliot Lee's avatar
Elliot Lee committed
28
#include "appenv.h"
29
30
#include "gdisplay.h"
#include "gimpimage.h"
Elliot Lee's avatar
Elliot Lee committed
31
32
33
34
35
#include "gimprc.h"
#include "gximage.h"
#include "image_render.h"
#include "pixel_region.h"
#include "scale.h"
36
37
#include "tile.h"
#include "tile_manager.h"
Elliot Lee's avatar
Elliot Lee committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52


typedef struct _RenderInfo  RenderInfo;
typedef void (*RenderFunc) (RenderInfo *info);

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


/*  accelerate transparency of image scaling  */
guchar *blend_dark_check = NULL;
guchar *blend_light_check = NULL;
guchar *tile_buf = NULL;
guchar *check_buf = NULL;
guchar *empty_buf = NULL;
guchar *temp_buf = NULL;

static guint   check_mod;
static guint   check_shift;
Daniel Egger's avatar
Daniel Egger committed
74
static guint   tile_shift;
Elliot Lee's avatar
Elliot Lee committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
static guchar  check_combos[6][2] =
{
  { 204, 255 },
  { 102, 153 },
  { 0, 51 },
  { 255, 255 },
  { 127, 127 },
  { 0, 0 }
};



void
render_setup (int check_type,
	      int check_size)
{
  int i, j;

  /*  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
96
97
98
  tile_shift = 0;
  while ((1 << tile_shift) < TILE_WIDTH)
    tile_shift++;
Elliot Lee's avatar
Elliot Lee committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

  /*  allocate a buffer for arranging information from a row of tiles  */
  if (!tile_buf)
    tile_buf = g_new (guchar, GXIMAGE_WIDTH * MAX_CHANNELS);

  if (check_type < 0 || check_type > 5)
    g_error ("invalid check_type argument to render_setup: %d", check_type);
  if (check_size < 0 || check_size > 2)
    g_error ("invalid check_size argument to render_setup: %d", check_size);

  if (!blend_dark_check)
    blend_dark_check = g_new (guchar, 65536);
  if (!blend_light_check)
    blend_light_check = g_new (guchar, 65536);

  for (i = 0; i < 256; i++)
    for (j = 0; j < 256; j++)
      {
	blend_dark_check [(i << 8) + j] = (guchar)
	  ((j * i + check_combos[check_type][0] * (255 - i)) / 255);
	blend_light_check [(i << 8) + j] = (guchar)
	  ((j * i + check_combos[check_type][1] * (255 - i)) / 255);
      }

  switch (check_size)
    {
    case SMALL_CHECKS:
      check_mod = 0x3;
      check_shift = 2;
      break;
    case MEDIUM_CHECKS:
      check_mod = 0x7;
      check_shift = 3;
      break;
    case LARGE_CHECKS:
      check_mod = 0xf;
      check_shift = 4;
      break;
    }

  /*  calculate check buffer for previews  */
  if (preview_size)
    {
      if (check_buf)
	g_free (check_buf);
      if (empty_buf)
	g_free (empty_buf);
      if (temp_buf)
	g_free (temp_buf);

      check_buf = (unsigned char *) g_malloc ((preview_size + 4) * 3);
      for (i = 0; i < (preview_size + 4); i++)
	{
	  if (i & 0x4)
	    {
	      check_buf[i * 3 + 0] = blend_dark_check[0];
	      check_buf[i * 3 + 1] = blend_dark_check[0];
	      check_buf[i * 3 + 2] = blend_dark_check[0];
	    }
	  else
	    {
	      check_buf[i * 3 + 0] = blend_light_check[0];
	      check_buf[i * 3 + 1] = blend_light_check[0];
	      check_buf[i * 3 + 2] = blend_light_check[0];
	    }
	}
      empty_buf = (unsigned char *) g_malloc ((preview_size + 4) * 3);
      memset (empty_buf, 0, (preview_size + 4) * 3);
      temp_buf = (unsigned char *) g_malloc ((preview_size + 4) * 3);
    }
  else
    {
      check_buf = NULL;
      empty_buf = NULL;
      temp_buf = NULL;
    }
}

void
render_free (void)
{
  g_free (tile_buf);
  g_free (check_buf);
}

/*  Render Image functions  */

186
187
188
189
190
191
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);
Elliot Lee's avatar
Elliot Lee committed
192
193
194
195
196
197
198
199
200
201

static void    render_image_init_info          (RenderInfo   *info,
						GDisplay     *gdisp,
						int           x,
						int           y,
						int           w,
						int           h);
static guint*  render_image_init_alpha         (int           mult);
static guchar* render_image_accelerate_scaling (int           width,
						int           start,
202
						float         scalex);
Elliot Lee's avatar
Elliot Lee committed
203
204
205
static guchar* render_image_tile_fault         (RenderInfo   *info);


206
static RenderFunc render_funcs[6] =
Elliot Lee's avatar
Elliot Lee committed
207
{
208
209
210
211
212
213
    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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
};


/*****************************************************************/
/*  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
render_image (GDisplay *gdisp,
	      int       x,
	      int       y,
	      int       w,
	      int       h)
{
  RenderInfo info;
  int image_type;

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

236
  image_type = gimp_image_projection_type (gdisp->gimage);
Elliot Lee's avatar
Elliot Lee committed
237
238
  if ((image_type < 0) || (image_type > 5))
    {
Manish Singh's avatar
Manish Singh committed
239
      g_message ("unknown gimage projection type: %d",
240
		 gimp_image_projection_type (gdisp->gimage));
Elliot Lee's avatar
Elliot Lee committed
241
242
243
244
245
      return;
    }

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

250
251
252
253
254
  /* Currently, only RGBA and GRAYA projection types are used - the rest
   * are in case of future need.  -- austin, 28th Nov 1998. */
  if (image_type != RGBA_GIMAGE && image_type != GRAYA_GIMAGE)
      g_warning ("using untested projection type %d", image_type);

255
  (* render_funcs[image_type]) (&info);
Elliot Lee's avatar
Elliot Lee committed
256
257
258
259
260
261
262
263
264
}



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

static void
265
render_image_indexed (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
266
267
268
{
  guchar *src;
  guchar *dest;
269
  guchar *cmap;
Elliot Lee's avatar
Elliot Lee committed
270
271
272
273
274
  gulong val;
  int byte_order;
  int y, ye;
  int x, xe;
  int initial;
275
276
  float error;
  float step;
Elliot Lee's avatar
Elliot Lee committed
277

278
  cmap = gimp_image_cmap (info->gdisp->gimage);
Elliot Lee's avatar
Elliot Lee committed
279
280
281
282
283

  y = info->y;
  ye = info->y + info->h;
  xe = info->x + info->w;

284
285
286
287
288
  step = 1.0 / info->scaley;

  error = y * step;
  error -= ((int)error) - step;

Elliot Lee's avatar
Elliot Lee committed
289
290
291
292
293
294
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
295
      if (!initial && (error < 1.0))
Elliot Lee's avatar
Elliot Lee committed
296
297
298
299
300
301
	memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
      else
	{
	  src = info->src;
	  dest = info->dest;

302
	  g_return_if_fail (src != NULL);
303
304
305
306
307
	  
	  for (x = info->x; x < xe; x++)
	    {
	      val = src[INDEXED_PIX] * 3;
	      src += 1;
308

309
310
311
312
313
	      dest[0] = cmap[val+0];
	      dest[1] = cmap[val+1];
	      dest[2] = cmap[val+2];
	      dest += 3;
	    }
Elliot Lee's avatar
Elliot Lee committed
314
315
316
317
	}

      info->dest += info->dest_bpl;

318
319
320
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
321
	{
322
323
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
324
	  info->src = render_image_tile_fault (info);
325
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
326
327
	}

328
      error += step;
Elliot Lee's avatar
Elliot Lee committed
329
330
331
332
    }
}

static void
333
render_image_indexed_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
334
335
336
{
  guchar *src;
  guchar *dest;
337
338
339
  guint *alpha;
  guchar *cmap;
  gulong r, g, b;
Elliot Lee's avatar
Elliot Lee committed
340
  gulong val;
341
342
  guint a;
  int dark_light;
Elliot Lee's avatar
Elliot Lee committed
343
344
345
346
  int byte_order;
  int y, ye;
  int x, xe;
  int initial;
347
348
  float error;
  float step;
Elliot Lee's avatar
Elliot Lee committed
349

350
  cmap = gimp_image_cmap (info->gdisp->gimage);
351
  alpha = info->alpha;
Elliot Lee's avatar
Elliot Lee committed
352
353
354
355
356

  y = info->y;
  ye = info->y + info->h;
  xe = info->x + info->w;

357
358
359
360
361
  step = 1.0 / info->scaley;

  error = y * step;
  error -= ((int)error) - step;

Elliot Lee's avatar
Elliot Lee committed
362
363
364
365
366
367
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
368
      if (!initial && (error < 1.0) && (y & check_mod))
Elliot Lee's avatar
Elliot Lee committed
369
370
371
372
373
374
	memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
      else
	{
	  src = info->src;
	  dest = info->dest;

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

377
	  g_return_if_fail (src != NULL);
Elliot Lee's avatar
Elliot Lee committed
378

379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
	  for (x = info->x; x < xe; x++)
	    {
	      a = alpha[src[ALPHA_I_PIX]];
	      val = src[INDEXED_PIX] * 3;
	      src += 2;

	      if (dark_light & 0x1)
		{
		  r = blend_dark_check[(a | cmap[val+0])];
		  g = blend_dark_check[(a | cmap[val+1])];
		  b = blend_dark_check[(a | cmap[val+2])];
		}
	      else
		{
		  r = blend_light_check[(a | cmap[val+0])];
		  g = blend_light_check[(a | cmap[val+1])];
		  b = blend_light_check[(a | cmap[val+2])];
		}

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

403
404
		if (((x + 1) & check_mod) == 0)
		  dark_light += 1;
Elliot Lee's avatar
Elliot Lee committed
405
406
407
408
409
	      }
	}

      info->dest += info->dest_bpl;

410
411
412
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
413
	{
414
415
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
416
	  info->src = render_image_tile_fault (info);
417
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
418
419
	}

420
      error += step;
Elliot Lee's avatar
Elliot Lee committed
421
422
423
424
    }
}

static void
425
render_image_gray (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
426
427
428
429
430
431
432
433
{
  guchar *src;
  guchar *dest;
  gulong val;
  int byte_order;
  int y, ye;
  int x, xe;
  int initial;
434
435
  float error;
  float step;
Elliot Lee's avatar
Elliot Lee committed
436
437
438
439
440

  y = info->y;
  ye = info->y + info->h;
  xe = info->x + info->w;

441
442
443
444
445
  step = 1.0 / info->scaley;

  error = y * step;
  error -= ((int)error) - step;

Elliot Lee's avatar
Elliot Lee committed
446
447
448
449
450
451
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
452
      if (!initial && (error < 1.0))
Elliot Lee's avatar
Elliot Lee committed
453
454
455
456
457
	memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
      else
	{
	  src = info->src;
	  dest = info->dest;
458
	  
459
460
	  g_return_if_fail (src != NULL);

461
462
463
464
	  for (x = info->x; x < xe; x++)
	    {
	      val = src[GRAY_PIX];
	      src += 1;
Elliot Lee's avatar
Elliot Lee committed
465

466
467
468
469
470
	      dest[0] = val;
	      dest[1] = val;
	      dest[2] = val;
	      dest += 3;
	    }
Elliot Lee's avatar
Elliot Lee committed
471
472
473
474
	}

      info->dest += info->dest_bpl;

475
476
477
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
478
	{
479
480
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
481
	  info->src = render_image_tile_fault (info);
482
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
483
484
	}

485
      error += step;
Elliot Lee's avatar
Elliot Lee committed
486
487
488
489
    }
}

static void
490
render_image_gray_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
491
492
493
494
495
496
497
498
499
500
501
{
  guchar *src;
  guchar *dest;
  guint *alpha;
  gulong val;
  guint a;
  int dark_light;
  int byte_order;
  int y, ye;
  int x, xe;
  int initial;
502
503
  float error;
  float step;
Elliot Lee's avatar
Elliot Lee committed
504
505
506
507
508
509
510

  alpha = info->alpha;

  y = info->y;
  ye = info->y + info->h;
  xe = info->x + info->w;

511
512
513
514
515
  step = 1.0 / info->scaley;

  error = y * step;
  error -= ((int)error) - step;

Elliot Lee's avatar
Elliot Lee committed
516
517
518
519
520
521
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
522
      if (!initial && (error < 1.0) && (y & check_mod))
Elliot Lee's avatar
Elliot Lee committed
523
524
525
526
527
528
529
530
	memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
      else
	{
	  src = info->src;
	  dest = info->dest;

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

531
532
	  g_return_if_fail (src != NULL);

533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
	  for (x = info->x; x < xe; x++)
	    {
	      a = alpha[src[ALPHA_G_PIX]];
	      if (dark_light & 0x1)
		val = blend_dark_check[(a | src[GRAY_PIX])];
	      else
		val = blend_light_check[(a | src[GRAY_PIX])];
	      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
550
551
552
553
	}

      info->dest += info->dest_bpl;

554
555
556
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
557
	{
558
559
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
560
	  info->src = render_image_tile_fault (info);
561
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
562
563
	}

564
      error += step;
Elliot Lee's avatar
Elliot Lee committed
565
566
567
568
    }
}

static void
569
render_image_rgb (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
570
571
572
{
  guchar *src;
  guchar *dest;
Daniel Egger's avatar
Daniel Egger committed
573
574
575
576
  int byte_order;
  int y, ye;
  int x, xe;
  int initial;
577
  float error;
Daniel Egger's avatar
Daniel Egger committed
578
  float step;
Elliot Lee's avatar
Elliot Lee committed
579
580

  y = info->y;
Daniel Egger's avatar
Daniel Egger committed
581
582
583
584
  ye = info->y + info->h;
  xe = info->x + info->w;

  step = 1.0 / info->scaley;
585
586
587
588

  error = y * step;
  error -= (int)error - step;

Elliot Lee's avatar
Elliot Lee committed
589
  initial = TRUE;
Daniel Egger's avatar
Daniel Egger committed
590
  byte_order = info->byte_order;
Elliot Lee's avatar
Elliot Lee committed
591
592
593
594
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
595
      if (!initial && (error < 1.0))
Elliot Lee's avatar
Elliot Lee committed
596
597
598
599
600
601
	memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
      else
	{
	  src = info->src;
	  dest = info->dest;

602
	  g_return_if_fail (src != NULL);
603
604
605
606
607
608
609
	  
	  /* 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];
610

611
612
613
	      src += 3;
	      dest += 3;
	    }
Elliot Lee's avatar
Elliot Lee committed
614
615
616
617
	}

      info->dest += info->dest_bpl;

618
619
620
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
621
	{
622
623
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
624
	  info->src = render_image_tile_fault (info);
625
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
626
627
	}

628
      error += step;
Elliot Lee's avatar
Elliot Lee committed
629
630
631
    }
}

632
633


Elliot Lee's avatar
Elliot Lee committed
634
static void
635
render_image_rgb_a (RenderInfo *info)
Elliot Lee's avatar
Elliot Lee committed
636
637
638
639
640
641
642
{
  guchar *src;
  guchar *dest;
  guint *alpha;
  gulong r, g, b;
  guint a;
  int dark_light;
Daniel Egger's avatar
Daniel Egger committed
643
644
645
  int byte_order;
  int y, ye;
  int x, xe;
Elliot Lee's avatar
Elliot Lee committed
646
  int initial;
647
  float error;
Daniel Egger's avatar
Daniel Egger committed
648
  float step;
Elliot Lee's avatar
Elliot Lee committed
649
650
651
652

  alpha = info->alpha;

  y = info->y;
Daniel Egger's avatar
Daniel Egger committed
653
654
655
656
  ye = info->y + info->h;
  xe = info->x + info->w;

  step = 1.0 / info->scaley;
657
658
659
660

  error = y * step;
  error -= ((int)error) - step;

Elliot Lee's avatar
Elliot Lee committed
661
662
663
664
665
666
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
667
      if (!initial && (error < 1.0) && (y & check_mod))
Elliot Lee's avatar
Elliot Lee committed
668
669
670
671
672
673
674
675
	memcpy (info->dest, info->dest - info->dest_bpl, info->dest_width);
      else
	{
	  src = info->src;
	  dest = info->dest;

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

676
677
	  g_return_if_fail (src != NULL);

678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
	  for (x = info->x; x < xe; x++)
	    {
	      a = alpha[src[ALPHA_PIX]];
	      if (dark_light & 0x1)
		{
		  r = blend_dark_check[(a | src[RED_PIX])];
		  g = blend_dark_check[(a | src[GREEN_PIX])];
		  b = blend_dark_check[(a | src[BLUE_PIX])];
		}
	      else
		{
		  r = blend_light_check[(a | src[RED_PIX])];
		  g = blend_light_check[(a | src[GREEN_PIX])];
		  b = blend_light_check[(a | src[BLUE_PIX])];
		}
Elliot Lee's avatar
Elliot Lee committed
693

694
	      src += 4;
Elliot Lee's avatar
Elliot Lee committed
695

696
697
698
699
	      dest[0] = r;
	      dest[1] = g;
	      dest[2] = b;
	      dest += 3;
Elliot Lee's avatar
Elliot Lee committed
700

701
702
703
	      if (((x + 1) & check_mod) == 0)
		dark_light += 1;
	    }
Elliot Lee's avatar
Elliot Lee committed
704
705
706
707
	}

      info->dest += info->dest_bpl;

708
709
710
      initial = FALSE;

      if (error >= 1.0)
Elliot Lee's avatar
Elliot Lee committed
711
	{
712
713
	  info->src_y += (int)error;
	  error -= (int)error;
Elliot Lee's avatar
Elliot Lee committed
714
	  info->src = render_image_tile_fault (info);
715
	  initial = TRUE;
Elliot Lee's avatar
Elliot Lee committed
716
717
	}

718
      error += step;
Elliot Lee's avatar
Elliot Lee committed
719
720
721
722
723
724
725
726
727
728
729
730
    }
}

static void
render_image_init_info (RenderInfo *info,
			GDisplay   *gdisp,
			int         x,
			int         y,
			int         w,
			int         h)
{
  info->gdisp = gdisp;
731
  info->src_tiles = gimp_image_projection (gdisp->gimage);
Elliot Lee's avatar
Elliot Lee committed
732
733
734
735
  info->x = x + gdisp->offset_x;
  info->y = y + gdisp->offset_y;
  info->w = w;
  info->h = h;
736
737
738
739
  info->scalex = SCALEFACTOR_X (gdisp);
  info->scaley = SCALEFACTOR_Y (gdisp);
  info->src_x = UNSCALEX (gdisp, info->x);
  info->src_y = UNSCALEY (gdisp, info->y);
740
  info->src_bpp = gimp_image_projection_bytes (gdisp->gimage);
Elliot Lee's avatar
Elliot Lee committed
741
742
743
744
745
  info->dest = gximage_get_data ();
  info->dest_bpp = gximage_get_bpp ();
  info->dest_bpl = gximage_get_bpl ();
  info->dest_width = info->w * info->dest_bpp;
  info->byte_order = gximage_get_byte_order ();
746
  info->scale = render_image_accelerate_scaling (w, info->x, info->scalex);
Elliot Lee's avatar
Elliot Lee committed
747
748
  info->alpha = NULL;

749
  switch (gimp_image_projection_type (gdisp->gimage))
Elliot Lee's avatar
Elliot Lee committed
750
751
752
753
    {
    case RGBA_GIMAGE:
    case GRAYA_GIMAGE:
    case INDEXEDA_GIMAGE:
754
      info->alpha = render_image_init_alpha (gimp_image_projection_opacity (gdisp->gimage));
Elliot Lee's avatar
Elliot Lee committed
755
      break;
756
757
758
    default:
      /* nothing special needs doing */
      break;
Elliot Lee's avatar
Elliot Lee committed
759
760
761
762
763
764
765
766
767
768
769
770
771
    }
}

static guint*
render_image_init_alpha (int mult)
{
  static guint *alpha_mult = NULL;
  static int alpha_val = -1;
  int i;

  if (alpha_val != mult)
    {
      if (!alpha_mult)
772
	alpha_mult = g_new (guint, 256);
Elliot Lee's avatar
Elliot Lee committed
773
774
775
776
777
778
779
780
781
782

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

  return alpha_mult;
}

static guchar*
783
784
785
render_image_accelerate_scaling (int   width,
				 int   start,
				 float scalex)
Elliot Lee's avatar
Elliot Lee committed
786
787
{
  static guchar *scale = NULL;
788
789
  float error;
  float step;
Elliot Lee's avatar
Elliot Lee committed
790
791
  int i;

792
793
  if (!scale)
    scale = g_new (guchar, GXIMAGE_WIDTH + 1);
Elliot Lee's avatar
Elliot Lee committed
794

795
  step = 1.0 / scalex;
Elliot Lee's avatar
Elliot Lee committed
796

797
798
799
800
801
802
803
804
  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
805
806
807
808
809
810
811
812
813
814
815
816
817
818

  return scale;
}

static guchar*
render_image_tile_fault (RenderInfo *info)
{
  Tile *tile;
  guchar *data;
  guchar *dest;
  guchar *scale;
  int width;
  int tilex;
  int tiley;
819
  int srctilex, srctiley;
Elliot Lee's avatar
Elliot Lee committed
820
  int step;
821
  int bpp = info->src_bpp;
Elliot Lee's avatar
Elliot Lee committed
822
823
824
825
826
  int x, b;

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

827
828
829
  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
830
831
832
  if (!tile)
    return NULL;

scott's avatar
scott committed
833
834
835
  data = tile_data_pointer (tile, 
			    info->src_x % TILE_WIDTH,
			    info->src_y % TILE_HEIGHT);
Elliot Lee's avatar
Elliot Lee committed
836
837
838
839
840
841
842
843
  scale = info->scale;
  dest = tile_buf;

  x = info->src_x;
  width = info->w;

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

847
848
      step = *scale++;
      if (step != 0)
Elliot Lee's avatar
Elliot Lee committed
849
	{
850
851
	  x += step;
	  data += step * bpp;
Elliot Lee's avatar
Elliot Lee committed
852

Daniel Egger's avatar
Daniel Egger committed
853
	  if ((x >> tile_shift) != tilex)
Elliot Lee's avatar
Elliot Lee committed
854
	    {
855
	      tile_release (tile, FALSE);
Elliot Lee's avatar
Elliot Lee committed
856
857
	      tilex += 1;

858
859
	      tile = tile_manager_get_tile (info->src_tiles, srctilex=x,
					    srctiley=info->src_y, TRUE, FALSE);
Daniel Egger's avatar
Daniel Egger committed
860
861
862
	      if (!tile)
		return tile_buf;

scott's avatar
scott committed
863
864
865
	      data = tile_data_pointer (tile, 
			    x % TILE_WIDTH,
			    info->src_y % TILE_HEIGHT);
Elliot Lee's avatar
Elliot Lee committed
866
867
868
869
	    }
	}
    }

870
  tile_release (tile, FALSE);
Elliot Lee's avatar
Elliot Lee committed
871
872
  return tile_buf;
}