1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
|
/* GIMP - The GNU 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
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <glib-object.h>
#include "core-types.h"
#include "base/tile.h"
#include "base/tile-manager.h"
#include "base/tile-pyramid.h"
#include "gimp.h"
#include "gimparea.h"
#include "gimpimage.h"
#include "gimpmarshal.h"
#include "gimppickable.h"
#include "gimpprojection.h"
#include "gimpprojection-construct.h"
enum
{
UPDATE,
LAST_SIGNAL
};
/* local function prototypes */
static void gimp_projection_pickable_iface_init (GimpPickableInterface *iface);
static void gimp_projection_finalize (GObject *object);
static gint64 gimp_projection_get_memsize (GimpObject *object,
gint64 *gui_size);
static void gimp_projection_pickable_flush (GimpPickable *pickable);
static gboolean gimp_projection_get_pixel_at (GimpPickable *pickable,
gint x,
gint y,
guchar *pixel);
static gint gimp_projection_get_opacity_at (GimpPickable *pickable,
gint x,
gint y);
static void gimp_projection_add_update_area (GimpProjection *proj,
gint x,
gint y,
gint w,
gint h);
static void gimp_projection_flush_whenever (GimpProjection *proj,
gboolean now);
static void gimp_projection_idle_render_init (GimpProjection *proj);
static gboolean gimp_projection_idle_render_callback (gpointer data);
static gboolean gimp_projection_idle_render_next_area (GimpProjection *proj);
static void gimp_projection_paint_area (GimpProjection *proj,
gboolean now,
gint x,
gint y,
gint w,
gint h);
static void gimp_projection_invalidate (GimpProjection *proj,
guint x,
guint y,
guint w,
guint h);
static void gimp_projection_validate_tile (TileManager *tm,
Tile *tile,
GimpProjection *proj);
static void gimp_projection_image_update (GimpImage *image,
gint x,
gint y,
gint w,
gint h,
GimpProjection *proj);
static void gimp_projection_image_size_changed (GimpImage *image,
GimpProjection *proj);
static void gimp_projection_image_mode_changed (GimpImage *image,
GimpProjection *proj);
static void gimp_projection_image_flush (GimpImage *image,
gboolean invalidate_preview,
GimpProjection *proj);
G_DEFINE_TYPE_WITH_CODE (GimpProjection, gimp_projection, GIMP_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (GIMP_TYPE_PICKABLE,
gimp_projection_pickable_iface_init))
#define parent_class gimp_projection_parent_class
static guint projection_signals[LAST_SIGNAL] = { 0 };
static void
gimp_projection_class_init (GimpProjectionClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GimpObjectClass *gimp_object_class = GIMP_OBJECT_CLASS (klass);
projection_signals[UPDATE] =
g_signal_new ("update",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GimpProjectionClass, update),
NULL, NULL,
gimp_marshal_VOID__BOOLEAN_INT_INT_INT_INT,
G_TYPE_NONE, 5,
G_TYPE_BOOLEAN,
G_TYPE_INT,
G_TYPE_INT,
G_TYPE_INT,
G_TYPE_INT);
object_class->finalize = gimp_projection_finalize;
gimp_object_class->get_memsize = gimp_projection_get_memsize;
}
static void
gimp_projection_init (GimpProjection *proj)
{
proj->image = NULL;
proj->pyramid = NULL;
proj->update_areas = NULL;
proj->idle_render.idle_id = 0;
proj->idle_render.update_areas = NULL;
proj->construct_flag = FALSE;
}
/* sorry for the evil casts */
static void
gimp_projection_pickable_iface_init (GimpPickableInterface *iface)
{
iface->flush = gimp_projection_pickable_flush;
iface->get_image = (GimpImage * (*) (GimpPickable *pickable)) gimp_projection_get_image;
iface->get_image_type = (GimpImageType (*) (GimpPickable *pickable)) gimp_projection_get_image_type;
iface->get_bytes = (gint (*) (GimpPickable *pickable)) gimp_projection_get_bytes;
iface->get_tiles = (TileManager * (*) (GimpPickable *pickable)) gimp_projection_get_tiles;
iface->get_pixel_at = gimp_projection_get_pixel_at;
iface->get_opacity_at = gimp_projection_get_opacity_at;
}
static void
gimp_projection_finalize (GObject *object)
{
GimpProjection *proj = GIMP_PROJECTION (object);
if (proj->idle_render.idle_id)
{
g_source_remove (proj->idle_render.idle_id);
proj->idle_render.idle_id = 0;
}
gimp_area_list_free (proj->update_areas);
proj->update_areas = NULL;
gimp_area_list_free (proj->idle_render.update_areas);
proj->idle_render.update_areas = NULL;
if (proj->pyramid)
{
tile_pyramid_destroy (proj->pyramid);
proj->pyramid = NULL;
}
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static gint64
gimp_projection_get_memsize (GimpObject *object,
gint64 *gui_size)
{
GimpProjection *projection = GIMP_PROJECTION (object);
gint64 memsize = 0;
if (projection->pyramid)
memsize = tile_pyramid_get_memsize (projection->pyramid);
return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
gui_size);
}
/**
* gimp_projection_estimate_memsize:
* @type: the image base type
* @width: image width
* @height: image height
*
* Calculates a rough estimate of the memory that is required for the
* projection of an image with the given @width and @height.
*
* Return value: a rough estimate of the memory requirements.
**/
gint64
gimp_projection_estimate_memsize (GimpImageBaseType type,
gint width,
gint height)
{
gint64 bytes = 0;
switch (type)
{
case GIMP_RGB:
case GIMP_INDEXED:
bytes = 4;
break;
case GIMP_GRAY:
bytes = 2;
break;
}
/* The pyramid levels constitute a geometric sum with a ratio of 1/4. */
return bytes * (gint64) width * (gint64) height * 1.33;
}
static void
gimp_projection_pickable_flush (GimpPickable *pickable)
{
GimpProjection *proj = GIMP_PROJECTION (pickable);
gimp_projection_finish_draw (proj);
gimp_projection_flush_now (proj);
if (proj->invalidate_preview)
{
/* invalidate the preview here since it is constructed from
* the projection
*/
proj->invalidate_preview = FALSE;
gimp_viewable_invalidate_preview (GIMP_VIEWABLE (proj->image));
}
}
static gboolean
gimp_projection_get_pixel_at (GimpPickable *pickable,
gint x,
gint y,
guchar *pixel)
{
GimpProjection *proj = GIMP_PROJECTION (pickable);
if (x < 0 || y < 0 || x >= proj->image->width || y >= proj->image->height)
return FALSE;
read_pixel_data_1 (gimp_projection_get_tiles (proj), x, y, pixel);
return TRUE;
}
static gint
gimp_projection_get_opacity_at (GimpPickable *pickable,
gint x,
gint y)
{
return OPAQUE_OPACITY;
}
GimpProjection *
gimp_projection_new (GimpImage *image)
{
GimpProjection *proj;
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
proj = g_object_new (GIMP_TYPE_PROJECTION, NULL);
proj->image = image;
g_signal_connect_object (image, "update",
G_CALLBACK (gimp_projection_image_update),
proj, 0);
g_signal_connect_object (image, "size-changed",
G_CALLBACK (gimp_projection_image_size_changed),
proj, 0);
g_signal_connect_object (image, "mode-changed",
G_CALLBACK (gimp_projection_image_mode_changed),
proj, 0);
g_signal_connect_object (image, "flush",
G_CALLBACK (gimp_projection_image_flush),
proj, 0);
return proj;
}
TileManager *
gimp_projection_get_tiles (GimpProjection *proj)
{
return gimp_projection_get_tiles_at_level (proj, 0);
}
TileManager *
gimp_projection_get_tiles_at_level (GimpProjection *proj,
gint level)
{
g_return_val_if_fail (GIMP_IS_PROJECTION (proj), NULL);
if (! proj->pyramid)
{
proj->pyramid = tile_pyramid_new (gimp_projection_get_image_type (proj),
proj->image->width,
proj->image->height);
tile_pyramid_set_validate_proc (proj->pyramid,
(TileValidateProc) gimp_projection_validate_tile,
proj);
}
return tile_pyramid_get_tiles (proj->pyramid, level);
}
/**
* gimp_projection_get_level:
* @proj: pointer to a GimpProjection
* @scale_x: horizontal scale factor
* @scale_y: vertical scale factor
*
* This function returns the optimal pyramid level for a given scale factor.
*
* Return value: the pyramid level to use for a given display scale factor.
**/
gint
gimp_projection_get_level (GimpProjection *proj,
gdouble scale_x,
gdouble scale_y)
{
return tile_pyramid_get_level (proj->image->width,
proj->image->height,
MAX (scale_x, scale_y));
}
GimpImage *
gimp_projection_get_image (const GimpProjection *proj)
{
g_return_val_if_fail (GIMP_IS_PROJECTION (proj), NULL);
return proj->image;
}
GimpImageType
gimp_projection_get_image_type (const GimpProjection *proj)
{
g_return_val_if_fail (GIMP_IS_PROJECTION (proj), -1);
switch (gimp_image_base_type (proj->image))
{
case GIMP_RGB:
case GIMP_INDEXED:
return GIMP_RGBA_IMAGE;
case GIMP_GRAY:
return GIMP_GRAYA_IMAGE;
}
g_assert_not_reached ();
}
gint
gimp_projection_get_bytes (const GimpProjection *proj)
{
g_return_val_if_fail (GIMP_IS_PROJECTION (proj), 0);
return GIMP_IMAGE_TYPE_BYTES (gimp_projection_get_image_type (proj));
}
gdouble
gimp_projection_get_opacity (const GimpProjection *proj)
{
g_return_val_if_fail (GIMP_IS_PROJECTION (proj), GIMP_OPACITY_OPAQUE);
return GIMP_OPACITY_OPAQUE;
}
void
gimp_projection_flush (GimpProjection *proj)
{
g_return_if_fail (GIMP_IS_PROJECTION (proj));
/* Construct on idle time */
gimp_projection_flush_whenever (proj, FALSE);
}
void
gimp_projection_flush_now (GimpProjection *proj)
{
g_return_if_fail (GIMP_IS_PROJECTION (proj));
/* Construct NOW */
gimp_projection_flush_whenever (proj, TRUE);
}
void
gimp_projection_finish_draw (GimpProjection *proj)
{
g_return_if_fail (GIMP_IS_PROJECTION (proj));
if (proj->idle_render.idle_id)
{
#if 0
g_printerr ("%s: flushing idle render queue\n", G_STRFUNC);
#endif
g_source_remove (proj->idle_render.idle_id);
proj->idle_render.idle_id = 0;
while (gimp_projection_idle_render_callback (proj));
}
}
/* private functions */
static void
gimp_projection_add_update_area (GimpProjection *proj,
gint x,
gint y,
gint w,
gint h)
{
GimpArea *area;
g_return_if_fail (GIMP_IS_PROJECTION (proj));
area = gimp_area_new (CLAMP (x, 0, proj->image->width),
CLAMP (y, 0, proj->image->height),
CLAMP (x + w, 0, proj->image->width),
CLAMP (y + h, 0, proj->image->height));
proj->update_areas = gimp_area_list_process (proj->update_areas, area);
}
static void
gimp_projection_flush_whenever (GimpProjection *proj,
gboolean now)
{
/* First the updates... */
if (proj->update_areas)
{
if (now) /* Synchronous */
{
GSList *list;
for (list = proj->update_areas; list; list = g_slist_next (list))
{
GimpArea *area = list->data;
if ((area->x1 != area->x2) && (area->y1 != area->y2))
{
gimp_projection_paint_area (proj,
FALSE, /* sic! */
area->x1,
area->y1,
(area->x2 - area->x1),
(area->y2 - area->y1));
}
}
}
else /* Asynchronous */
{
gimp_projection_idle_render_init (proj);
}
/* Free the update lists */
gimp_area_list_free (proj->update_areas);
proj->update_areas = NULL;
}
else if (! now && proj->invalidate_preview)
{
/* invalidate the preview here since it is constructed from
* the projection
*/
proj->invalidate_preview = FALSE;
gimp_viewable_invalidate_preview (GIMP_VIEWABLE (proj->image));
}
}
static void
gimp_projection_idle_render_init (GimpProjection *proj)
{
GSList *list;
/* We need to merge the IdleRender's and the GimpProjection's update_areas
* list to keep track of which of the updates have been flushed and hence
* need to be drawn.
*/
for (list = proj->update_areas; list; list = g_slist_next (list))
{
GimpArea *area = list->data;
proj->idle_render.update_areas =
gimp_area_list_process (proj->idle_render.update_areas,
gimp_area_new (area->x1, area->y1,
area->x2, area->y2));
}
/* If an idlerender was already running, merge the remainder of its
* unrendered area with the update_areas list, and make it start work
* on the next unrendered area in the list.
*/
if (proj->idle_render.idle_id)
{
GimpArea *area =
gimp_area_new (proj->idle_render.base_x,
proj->idle_render.y,
proj->idle_render.base_x + proj->idle_render.width,
proj->idle_render.y + (proj->idle_render.height -
(proj->idle_render.y -
proj->idle_render.base_y)));
proj->idle_render.update_areas =
gimp_area_list_process (proj->idle_render.update_areas, area);
gimp_projection_idle_render_next_area (proj);
}
else
{
if (proj->idle_render.update_areas == NULL)
{
g_warning ("%s: wanted to start idle render with no update_areas",
G_STRFUNC);
return;
}
gimp_projection_idle_render_next_area (proj);
proj->idle_render.idle_id =
g_idle_add_full (G_PRIORITY_HIGH_IDLE,
gimp_projection_idle_render_callback, proj,
NULL);
}
}
/* Unless specified otherwise, projection re-rendering is organised by
* IdleRender, which amalgamates areas to be re-rendered and breaks
* them into bite-sized chunks which are chewed on in a low- priority
* idle thread. This greatly improves responsiveness for many GIMP
* operations. -- Adam
*/
static gboolean
gimp_projection_idle_render_callback (gpointer data)
{
GimpProjection *proj = data;
gint workx, worky;
gint workw, workh;
#define CHUNK_WIDTH 256
#define CHUNK_HEIGHT 128
workw = CHUNK_WIDTH;
workh = CHUNK_HEIGHT;
workx = proj->idle_render.x;
worky = proj->idle_render.y;
if (workx + workw > proj->idle_render.base_x + proj->idle_render.width)
{
workw = proj->idle_render.base_x + proj->idle_render.width - workx;
}
if (worky + workh > proj->idle_render.base_y + proj->idle_render.height)
{
workh = proj->idle_render.base_y + proj->idle_render.height - worky;
}
gimp_projection_paint_area (proj, TRUE /* sic! */,
workx, worky, workw, workh);
proj->idle_render.x += CHUNK_WIDTH;
if (proj->idle_render.x >=
proj->idle_render.base_x + proj->idle_render.width)
{
proj->idle_render.x = proj->idle_render.base_x;
proj->idle_render.y += CHUNK_HEIGHT;
if (proj->idle_render.y >=
proj->idle_render.base_y + proj->idle_render.height)
{
if (! gimp_projection_idle_render_next_area (proj))
{
/* FINISHED */
proj->idle_render.idle_id = 0;
if (proj->invalidate_preview)
{
/* invalidate the preview here since it is constructed from
* the projection
*/
proj->invalidate_preview = FALSE;
gimp_viewable_invalidate_preview (GIMP_VIEWABLE (proj->image));
}
return FALSE;
}
}
}
/* Still work to do. */
return TRUE;
}
static gboolean
gimp_projection_idle_render_next_area (GimpProjection *proj)
{
GimpArea *area;
if (! proj->idle_render.update_areas)
return FALSE;
area = proj->idle_render.update_areas->data;
proj->idle_render.update_areas =
g_slist_remove (proj->idle_render.update_areas, area);
proj->idle_render.x = proj->idle_render.base_x = area->x1;
proj->idle_render.y = proj->idle_render.base_y = area->y1;
proj->idle_render.width = area->x2 - area->x1;
proj->idle_render.height = area->y2 - area->y1;
gimp_area_free (area);
return TRUE;
}
static void
gimp_projection_paint_area (GimpProjection *proj,
gboolean now,
gint x,
gint y,
gint w,
gint h)
{
/* Bounds check */
gint x1 = CLAMP (x, 0, proj->image->width);
gint y1 = CLAMP (y, 0, proj->image->height);
gint x2 = CLAMP (x + w, 0, proj->image->width);
gint y2 = CLAMP (y + h, 0, proj->image->height);
gimp_projection_invalidate (proj, x1, y1, x2 - x1, y2 - y1);
g_signal_emit (proj, projection_signals[UPDATE], 0,
now, x1, y1, x2 - x1, y2 - y1);
}
static void
gimp_projection_invalidate (GimpProjection *proj,
guint x,
guint y,
guint w,
guint h)
{
if (proj->pyramid)
tile_pyramid_invalidate_area (proj->pyramid, x, y, w, h);
}
static void
gimp_projection_validate_tile (TileManager *tm,
Tile *tile,
GimpProjection *proj)
{
gint x, y;
/* Find the coordinates of this tile */
tile_manager_get_tile_coordinates (tm, tile, &x, &y);
gimp_projection_construct (proj,
x, y,
tile_ewidth (tile), tile_eheight (tile));
}
/* image callbacks */
static void
gimp_projection_image_update (GimpImage *image,
gint x,
gint y,
gint w,
gint h,
GimpProjection *proj)
{
gimp_projection_add_update_area (proj, x, y, w, h);
}
static void
gimp_projection_image_size_changed (GimpImage *image,
GimpProjection *proj)
{
if (proj->pyramid)
{
tile_pyramid_destroy (proj->pyramid);
proj->pyramid = NULL;
}
gimp_projection_add_update_area (proj, 0, 0, image->width, image->height);
}
static void
gimp_projection_image_mode_changed (GimpImage *image,
GimpProjection *proj)
{
if (proj->pyramid)
{
tile_pyramid_destroy (proj->pyramid);
proj->pyramid = NULL;
}
gimp_projection_add_update_area (proj, 0, 0, image->width, image->height);
}
static void
gimp_projection_image_flush (GimpImage *image,
gboolean invalidate_preview,
GimpProjection *proj)
{
if (invalidate_preview)
proj->invalidate_preview = TRUE;
gimp_projection_flush (proj);
}
|