[r4]: cbrpager-maemo / branches / MAEMO_0_9_20 / src / image_view_drawer.c Maximize Restore History

Download this file

image_view_drawer.c    312 lines (286 with data), 11.2 kB

  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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; coding: utf-8 -*-
*
* Copyright © 2007 Björn Lindqvist <bjourne@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include "image_view_drawer.h"
#include "utils.h"
#include <string.h>
static gboolean
gdk_rectangle_contains_rect (GdkRectangle r1, GdkRectangle r2)
{
return
r1.x <= r2.x &&
r1.y <= r2.y &&
(r2.x + r2.width) <= (r1.x + r1.width) &&
(r2.y + r2.height) <= (r1.y + r1.height);
}
static void
gdk_pixbuf_copy_area_intact (GdkPixbuf *src,
int src_x,
int src_y,
int width,
int height,
GdkPixbuf *dst,
int dst_x,
int dst_y)
{
if (src_x == dst_x && src_y == dst_y && src == dst)
return;
int src_stride = gdk_pixbuf_get_rowstride (src);
int dst_stride = gdk_pixbuf_get_rowstride (dst);
int chans = gdk_pixbuf_get_n_channels (src);
int linelen = width * chans;
guchar *src_base = gdk_pixbuf_get_pixels (src);
guchar *dst_base = gdk_pixbuf_get_pixels (dst);
int src_y_ofs = src_y * src_stride;
int dst_y_ofs = dst_y * dst_stride;
int y;
if (dst_y > src_y)
{
src_y_ofs = (src_y + height - 1) * src_stride;
dst_y_ofs = (dst_y + height - 1) * dst_stride;
src_stride = -src_stride;
dst_stride = -dst_stride;
}
guchar *src_ofs = src_base + src_y_ofs + src_x * chans;
guchar *dst_ofs = dst_base + dst_y_ofs + dst_x * chans;
void (*copy_func)(void *, void *, size_t) = (void *) memcpy;
if (dst_x > src_x)
copy_func = (void *) memmove;
for (y = 0; y < height; y++)
{
copy_func (dst_ofs, src_ofs, linelen);
src_ofs += src_stride;
dst_ofs += dst_stride;
}
}
/**
* draw_settings_get_method:
* @old: The draw settings used in the last draw operation.
* @new_: The draw settings used in the current draw operation.
* @last_pixbuf: The pixbuf to be drawn.
*
* Returns which method that needs to be carried out in this draw.
**/
DrawMethod
draw_settings_get_method (DrawSettings *old,
DrawSettings *new_,
GdkPixbuf *last_pixbuf)
{
if (new_->zoom != old->zoom ||
new_->interp != old->interp ||
new_->check_color1 != old->check_color1 ||
new_->check_color2 != old->check_color2 ||
new_->pixbuf != old->pixbuf)
return DRAW_METHOD_SCALE;
if (gdk_rectangle_contains_rect (old->zoom_rect, new_->zoom_rect))
return DRAW_METHOD_CONTAINS;
return DRAW_METHOD_SCROLL;
}
ImageViewDrawer *
image_view_drawer_new ()
{
ImageViewDrawer *drawer = g_new0 (ImageViewDrawer, 1);
drawer->last_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 1, 1);
drawer->check_size = 16;
drawer->old = (DrawSettings){0,
{0, 0, 0, 0},
0, 0,
GDK_INTERP_NEAREST, drawer->last_pixbuf,
0, 0};
return drawer;
}
void
image_view_drawer_free (ImageViewDrawer *drawer)
{
g_object_unref (drawer->last_pixbuf);
g_free (drawer);
}
/**
* image_view_drawer_force_scale:
*
* This method forces the drawer to perform a %DRAW_FLAGS_SCALE
* operation the next time image_view_drawer_draw() is called.
*
* ImageViewDrawer tries to minimize the number of scale operations
* needed by caching the last drawn pixbuf. It would be inefficient to
* check the individual pixels inside the pixbuf so it assumes that if
* the memory address of the pixbuf has not changed, then the cache is
* good to use.
*
* Use this method to override that assumption.
**/
void
image_view_drawer_force_scale (ImageViewDrawer *drawer)
{
/* Set the cached zoom to a bogus value, to force a
DRAW_FLAGS_SCALE but not a DRAW_FLAGS_ALLOCATE. */
drawer->old.zoom = -1234.0;
}
static GdkPixbuf *
image_view_drawer_scroll_intersection (GdkPixbuf *pixbuf,
int new_width,
int new_height,
int src_x,
int src_y,
int inter_width,
int inter_height,
int dst_x,
int dst_y)
{
int last_width = gdk_pixbuf_get_width (pixbuf);
int last_height = gdk_pixbuf_get_height (pixbuf);
int width = MAX (last_width, new_width);
int height = MAX (last_height, new_height);
if (width > last_width || height > last_height)
{
GdkColorspace cs = gdk_pixbuf_get_colorspace (pixbuf);
int bps = gdk_pixbuf_get_bits_per_sample (pixbuf);
gboolean alpha = gdk_pixbuf_get_has_alpha (pixbuf);
GdkPixbuf *tmp = gdk_pixbuf_new (cs, alpha, bps, width, height);
gdk_pixbuf_copy_area_intact (pixbuf,
src_x, src_y,
inter_width, inter_height,
tmp,
dst_x, dst_y);
g_object_unref (pixbuf);
return tmp;
}
gdk_pixbuf_copy_area_intact (pixbuf,
src_x, src_y,
inter_width, inter_height,
pixbuf,
dst_x, dst_y);
return pixbuf;
}
/**
* image_view_drawer_intersect_draw:
*
* Updates the cache by first scrolling the still valid area in the
* cache. Then the newly exposed areas in the cache is sampled from
* the pixbuf.
**/
static void
image_view_drawer_intersect_draw (ImageViewDrawer *drawer,
DrawSettings *ds,
GdkDrawable *drawable)
{
GdkRectangle this = ds->zoom_rect;
GdkRectangle cache = drawer->old.zoom_rect;
int n;
/* If there is no intersection, we have to scale the whole area
from the source pixbuf. */
GdkRectangle inter;
GdkRectangle around[4] = {
this,
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
if (gdk_rectangle_intersect (&cache, &this, &inter))
gdk_rectangle_get_rects_around (&this, &inter, around);
drawer->last_pixbuf =
image_view_drawer_scroll_intersection (drawer->last_pixbuf,
this.width,
this.height,
inter.x - cache.x,
inter.y - cache.y,
inter.width,
inter.height,
around[1].width,
around[0].height);
for (n = 0; n < 4; n++)
{
if (!around[n].width || !around[n].height)
continue;
gdk_pixbuf_scale_blend (ds->pixbuf,
drawer->last_pixbuf,
around[n].x - this.x,
around[n].y - this.y,
around[n].width, around[n].height,
-this.x, -this.y,
ds->zoom,
ds->interp,
around[n].x, around[n].y,
drawer->check_size,
ds->check_color1,
ds->check_color2);
}
}
/**
* image_view_drawer_draw:
* @drawer: An #ImageViewDrawer.
* @ds: The #DrawSettings to use for this redraw.
**/
void
image_view_drawer_draw (ImageViewDrawer *drawer,
DrawSettings *ds,
GdkDrawable *drawable)
{
GdkRectangle this = ds->zoom_rect;
DrawMethod method = draw_settings_get_method (&drawer->old, ds,
drawer->last_pixbuf);
int deltax = 0;
int deltay = 0;
if (method == DRAW_METHOD_CONTAINS)
{
deltax = this.x - drawer->old.zoom_rect.x;
deltay = this.y - drawer->old.zoom_rect.y;
}
else if (method == DRAW_METHOD_SCROLL)
{
image_view_drawer_intersect_draw (drawer, ds, drawable);
}
else if (method == DRAW_METHOD_SCALE)
{
int last_width = gdk_pixbuf_get_width (drawer->last_pixbuf);
int last_height = gdk_pixbuf_get_height (drawer->last_pixbuf);
GdkColorspace new_cs = gdk_pixbuf_get_colorspace (ds->pixbuf);
GdkColorspace last_cs = gdk_pixbuf_get_colorspace (drawer->last_pixbuf);
int new_bps = gdk_pixbuf_get_bits_per_sample (ds->pixbuf);
int last_bps = gdk_pixbuf_get_bits_per_sample (drawer->last_pixbuf);
if (this.width > last_width || this.height > last_height ||
new_cs != last_cs || new_bps != last_bps)
{
g_object_unref (drawer->last_pixbuf);
drawer->last_pixbuf = gdk_pixbuf_new (new_cs, FALSE, new_bps,
this.width, this.height);
}
gdk_pixbuf_scale_blend (ds->pixbuf,
drawer->last_pixbuf,
0, 0,
this.width, this.height,
(double) -this.x, (double) -this.y,
ds->zoom,
ds->interp,
this.x, this.y,
drawer->check_size,
ds->check_color1,
ds->check_color2);
}
gdk_draw_pixbuf (drawable,
NULL,
drawer->last_pixbuf,
deltax, deltay,
ds->widget_x, ds->widget_y,
this.width, this.height,
GDK_RGB_DITHER_MAX,
ds->widget_x, ds->widget_y);
if (method != DRAW_METHOD_CONTAINS)
drawer->old = *ds;
}