gimpdisplayshell-render.c 20.2 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
Sven Neumann's avatar
Sven Neumann committed
18
19
20

#include "config.h"

Elliot Lee's avatar
Elliot Lee committed
21
22
#include <stdlib.h>
#include <string.h>
23

Sven Neumann's avatar
Sven Neumann committed
24
25
#include <gtk/gtk.h>

26
#include "display-types.h"
Sven Neumann's avatar
Sven Neumann committed
27

Michael Natterer's avatar
Michael Natterer committed
28
29
30
#include "base/tile-manager.h"
#include "base/tile.h"

31
#include "core/gimpimage.h"
32
#include "core/gimpimage-projection.h"
33

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

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


typedef struct _RenderInfo  RenderInfo;
43
44

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

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


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


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

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

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

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

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

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

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

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

146
147
148
149
#ifdef __GNUC__
#warning #define MAX_PREVIEW_SIZE 1024  /* GIMP_PREVIEW_MAX_SIZE (EEK) */
#endif
#define MAX_PREVIEW_SIZE 1024  /* GIMP_PREVIEW_MAX_SIZE (EEK) */
150

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

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

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

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

190

Elliot Lee's avatar
Elliot Lee committed
191
192
/*  Render Image functions  */

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
static void     render_image_rgb                (RenderInfo       *info);
static void     render_image_rgb_a              (RenderInfo       *info);
static void     render_image_gray               (RenderInfo       *info);
static void     render_image_gray_a             (RenderInfo       *info);
static void     render_image_indexed            (RenderInfo       *info);
static void     render_image_indexed_a          (RenderInfo       *info);

static void     render_image_init_info          (RenderInfo       *info,
						 GimpDisplayShell *shell,
						 gint              x,
						 gint              y,
						 gint              w,
						 gint              h);
static guint  * render_image_init_alpha         (gint              mult);
static guchar * render_image_accelerate_scaling (gint              width,
						 gint              start,
						 gfloat            scalex);
static guchar * render_image_tile_fault         (RenderInfo       *info);
Elliot Lee's avatar
Elliot Lee committed
211
212


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


/*****************************************************************/
/*  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
232
233
234
235
236
gimp_display_shell_render (GimpDisplayShell *shell,
                           gint              x,
                           gint              y,
                           gint              w,
                           gint              h)
Elliot Lee's avatar
Elliot Lee committed
237
238
{
  RenderInfo info;
239
  gint       image_type;
240
241
242
  GList     *list;

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

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

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

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

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

265
  (* render_funcs[image_type]) (&info);
266
267

  /*  apply filters to the rendered projection  */
268
  for (list = shell->filters; list; list = g_list_next (list))
269
    {
270
271
272
      ColorDisplayNode *node;

      node = (ColorDisplayNode *) list->data;
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291

      node->cd_convert (node->cd_ID,
                        shell->render_buf,
                        w, h,
                        3,
                        3 * GIMP_DISPLAY_SHELL_RENDER_BUF_WIDTH);
    }

  /*  put it to the screen  */
  gdk_draw_rgb_image_dithalign (shell->canvas->window,
                                shell->render_gc,
                                x + shell->disp_xoffset,
                                y + shell->disp_yoffset,
                                w, h,
                                GDK_RGB_DITHER_MAX,
                                shell->render_buf,
                                3 * GIMP_DISPLAY_SHELL_RENDER_BUF_WIDTH,
                                shell->offset_x,
                                shell->offset_y);
Elliot Lee's avatar
Elliot Lee committed
292
293
294
295
296
297
298
299
}


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

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

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

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

319
320
  step = 1.0 / info->scaley;

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

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

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

339
	  g_return_if_fail (src != NULL);
340

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

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

      info->dest += info->dest_bpl;

355
356
357
      initial = FALSE;

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

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

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

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

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

394
395
  step = 1.0 / info->scaley;

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

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

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

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

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

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

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

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

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

      info->dest += info->dest_bpl;

449
450
451
      initial = FALSE;

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

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

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

480
481
  step = 1.0 / info->scaley;

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

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

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

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

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

      info->dest += info->dest_bpl;

516
517
518
      initial = FALSE;

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

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

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

  alpha = info->alpha;

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

552
553
  step = 1.0 / info->scaley;

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

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

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

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

574
575
	  g_return_if_fail (src != NULL);

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

      info->dest += info->dest_bpl;

597
598
599
      initial = FALSE;

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

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

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

  step = 1.0 / info->scaley;
628

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

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

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

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

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

      info->dest += info->dest_bpl;

663
664
665
      initial = FALSE;

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

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

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

  alpha = info->alpha;

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

  step = 1.0 / info->scaley;
700

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

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

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

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

721
722
	  g_return_if_fail (src != NULL);

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

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

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

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

      info->dest += info->dest_bpl;

753
754
755
      initial = FALSE;

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

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

static void
768
769
770
771
772
773
render_image_init_info (RenderInfo       *info,
			GimpDisplayShell *shell,
			gint              x,
			gint              y,
			gint              w,
			gint              h)
Elliot Lee's avatar
Elliot Lee committed
774
{
775
776
777
778
  info->shell      = shell;
  info->src_tiles  = gimp_image_projection (shell->gdisp->gimage);
  info->x          = x + shell->offset_x;
  info->y          = y + shell->offset_y;
779
780
  info->w          = w;
  info->h          = h;
781
782
783
784
  info->scalex     = SCALEFACTOR_X (shell);
  info->scaley     = SCALEFACTOR_Y (shell);
  info->src_x      = UNSCALEX (shell, info->x);
  info->src_y      = UNSCALEY (shell, info->y);
785
786
787
788
  info->src_bpp    = gimp_image_projection_bytes (shell->gdisp->gimage);
  info->dest       = shell->render_buf;
  info->dest_bpp   = 3;
  info->dest_bpl   = info->dest_bpp * GIMP_DISPLAY_SHELL_RENDER_BUF_WIDTH;
Elliot Lee's avatar
Elliot Lee committed
789
  info->dest_width = info->w * info->dest_bpp;
790
  info->byte_order = GDK_MSB_FIRST;
791
792
  info->scale      = render_image_accelerate_scaling (w, info->x, info->scalex);
  info->alpha      = NULL;
Elliot Lee's avatar
Elliot Lee committed
793

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

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

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

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

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

  return alpha_mult;
}

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

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

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

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

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

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

  return scale;
}

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

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

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

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

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

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

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

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

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

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

911
  tile_release (tile, FALSE);
912

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