gimpdisplayshell-render.c 17.7 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
29
30
31
32
33
34
#include "appenv.h"
#include "gimprc.h"
#include "gximage.h"
#include "image_render.h"
#include "pixel_region.h"
#include "scale.h"

scott's avatar
scott committed
35
#include "tile.h"			/* ick. */
Elliot Lee's avatar
Elliot Lee committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49

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;
50
51
  float scalex;
  float scaley;
Elliot Lee's avatar
Elliot Lee committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  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
71
static guint   tile_shift;
Elliot Lee's avatar
Elliot Lee committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
93
94
95
  tile_shift = 0;
  while ((1 << tile_shift) < TILE_WIDTH)
    tile_shift++;
Elliot Lee's avatar
Elliot Lee committed
96
97
98
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

  /*  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  */

183
184
185
186
187
188
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
189
190
191
192
193
194
195
196
197
198

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,
199
						float         scalex);
Elliot Lee's avatar
Elliot Lee committed
200
201
202
static guchar* render_image_tile_fault         (RenderInfo   *info);


203
static RenderFunc render_funcs[6] =
Elliot Lee's avatar
Elliot Lee committed
204
{
205
206
207
208
209
210
    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
211
212
213
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);

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

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

247
248
249
250
251
  /* 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);

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



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

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

275
  cmap = gimage_cmap (info->gdisp->gimage);
Elliot Lee's avatar
Elliot Lee committed
276
277
278
279
280

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

281
282
283
284
285
  step = 1.0 / info->scaley;

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

Elliot Lee's avatar
Elliot Lee committed
286
287
288
289
290
291
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

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

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

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

      info->dest += info->dest_bpl;

315
316
317
      initial = FALSE;

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

325
      error += step;
Elliot Lee's avatar
Elliot Lee committed
326
327
328
329
    }
}

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

347
348
  cmap = gimage_cmap (info->gdisp->gimage);
  alpha = info->alpha;
Elliot Lee's avatar
Elliot Lee committed
349
350
351
352
353

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

354
355
356
357
358
  step = 1.0 / info->scaley;

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

Elliot Lee's avatar
Elliot Lee committed
359
360
361
362
363
364
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

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

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

374
	  g_return_if_fail (src != NULL);
Elliot Lee's avatar
Elliot Lee committed
375

376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
	  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
398
399
		dest += 3;

400
401
		if (((x + 1) & check_mod) == 0)
		  dark_light += 1;
Elliot Lee's avatar
Elliot Lee committed
402
403
404
405
406
	      }
	}

      info->dest += info->dest_bpl;

407
408
409
      initial = FALSE;

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

417
      error += step;
Elliot Lee's avatar
Elliot Lee committed
418
419
420
421
    }
}

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

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

438
439
440
441
442
  step = 1.0 / info->scaley;

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

Elliot Lee's avatar
Elliot Lee committed
443
444
445
446
447
448
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

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

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

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

      info->dest += info->dest_bpl;

472
473
474
      initial = FALSE;

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

482
      error += step;
Elliot Lee's avatar
Elliot Lee committed
483
484
485
486
    }
}

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

  alpha = info->alpha;

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

508
509
510
511
512
  step = 1.0 / info->scaley;

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

Elliot Lee's avatar
Elliot Lee committed
513
514
515
516
517
518
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
519
      if (!initial && (error < 1.0) && (y & check_mod))
Elliot Lee's avatar
Elliot Lee committed
520
521
522
523
524
525
526
527
	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);

528
529
	  g_return_if_fail (src != NULL);

530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
	  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
547
548
549
550
	}

      info->dest += info->dest_bpl;

551
552
553
      initial = FALSE;

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

561
      error += step;
Elliot Lee's avatar
Elliot Lee committed
562
563
564
565
    }
}

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

  y = info->y;
Daniel Egger's avatar
Daniel Egger committed
578
579
580
581
  ye = info->y + info->h;
  xe = info->x + info->w;

  step = 1.0 / info->scaley;
582
583
584
585

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

Elliot Lee's avatar
Elliot Lee committed
586
  initial = TRUE;
Daniel Egger's avatar
Daniel Egger committed
587
  byte_order = info->byte_order;
Elliot Lee's avatar
Elliot Lee committed
588
589
590
591
  info->src = render_image_tile_fault (info);

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

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

608
609
610
	      src += 3;
	      dest += 3;
	    }
Elliot Lee's avatar
Elliot Lee committed
611
612
613
614
	}

      info->dest += info->dest_bpl;

615
616
617
      initial = FALSE;

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

625
      error += step;
Elliot Lee's avatar
Elliot Lee committed
626
627
628
    }
}

629
630


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

  alpha = info->alpha;

  y = info->y;
Daniel Egger's avatar
Daniel Egger committed
650
651
652
653
  ye = info->y + info->h;
  xe = info->x + info->w;

  step = 1.0 / info->scaley;
654
655
656
657

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

Elliot Lee's avatar
Elliot Lee committed
658
659
660
661
662
663
  initial = TRUE;
  byte_order = info->byte_order;
  info->src = render_image_tile_fault (info);

  for (; y < ye; y++)
    {
664
      if (!initial && (error < 1.0) && (y & check_mod))
Elliot Lee's avatar
Elliot Lee committed
665
666
667
668
669
670
671
672
	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);

673
674
	  g_return_if_fail (src != NULL);

675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
	  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
690

691
	      src += 4;
Elliot Lee's avatar
Elliot Lee committed
692

693
694
695
696
	      dest[0] = r;
	      dest[1] = g;
	      dest[2] = b;
	      dest += 3;
Elliot Lee's avatar
Elliot Lee committed
697

698
699
700
	      if (((x + 1) & check_mod) == 0)
		dark_light += 1;
	    }
Elliot Lee's avatar
Elliot Lee committed
701
702
703
704
	}

      info->dest += info->dest_bpl;

705
706
707
      initial = FALSE;

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

715
      error += step;
Elliot Lee's avatar
Elliot Lee committed
716
717
718
719
720
721
722
723
724
725
726
727
    }
}

static void
render_image_init_info (RenderInfo *info,
			GDisplay   *gdisp,
			int         x,
			int         y,
			int         w,
			int         h)
{
  info->gdisp = gdisp;
728
  info->src_tiles = gimage_projection (gdisp->gimage);
Elliot Lee's avatar
Elliot Lee committed
729
730
731
732
  info->x = x + gdisp->offset_x;
  info->y = y + gdisp->offset_y;
  info->w = w;
  info->h = h;
733
734
735
736
  info->scalex = SCALEFACTOR_X (gdisp);
  info->scaley = SCALEFACTOR_Y (gdisp);
  info->src_x = UNSCALEX (gdisp, info->x);
  info->src_y = UNSCALEY (gdisp, info->y);
Elliot Lee's avatar
Elliot Lee committed
737
738
739
740
741
742
  info->src_bpp = gimage_projection_bytes (gdisp->gimage);
  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 ();
743
  info->scale = render_image_accelerate_scaling (w, info->x, info->scalex);
Elliot Lee's avatar
Elliot Lee committed
744
745
746
747
748
749
750
751
752
  info->alpha = NULL;

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

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)
769
	alpha_mult = g_new (guint, 256);
Elliot Lee's avatar
Elliot Lee committed
770
771
772
773
774
775
776
777
778
779

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

  return alpha_mult;
}

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

789
790
  if (!scale)
    scale = g_new (guchar, GXIMAGE_WIDTH + 1);
Elliot Lee's avatar
Elliot Lee committed
791

792
  step = 1.0 / scalex;
Elliot Lee's avatar
Elliot Lee committed
793

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

  return scale;
}

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

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

824
825
826
  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
827
828
829
  if (!tile)
    return NULL;

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

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

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

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

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

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

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

867
  tile_release (tile, FALSE);
Elliot Lee's avatar
Elliot Lee committed
868
869
  return tile_buf;
}