-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtile-stream.c
336 lines (291 loc) · 8.38 KB
/
tile-stream.c
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
/**
* tile-stream.c
* Simple support for streams of tiles using a slightly simpler
* API than that the one built into the GIMP. Intended primarily
* as support for sending data over D-Bus, but potentially usable
* for other purposes.
*
* Copyright (c) 2013 Samuel A. Rebelsky. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as published
* by the Free Software Foundation, either version 3 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, see <http://www.gnu.org/licenses/>.
*/
// +---------+---------------------------------------------------------
// | Headers |
// +---------+
#include <libgimp/gimp.h>
#include <string.h> // For memcpy.
#include "tile-stream.h"
// +-----------+-------------------------------------------------------
// | Constants |
// +-----------+
/**
* The maximum number of simultaneous tile streams supported.
*/
#define MAX_TILE_STREAMS 16
// +-------+-----------------------------------------------------------
// | Types |
// +-------+
/**
* Information on a tile iterator. (While we tend to index tile streams
* with numbers, we still need the data.
*/
struct TileStream
{
int image;
int drawable;
int left;
int top;
int width;
int height;
int n;
GimpDrawable *source;
GimpDrawable *target;
gpointer iterator;
GimpPixelRgn source_region;
GimpPixelRgn target_region;
};
typedef struct TileStream TileStream;
// +---------+---------------------------------------------------------
// | Globals |
// +---------+
/**
* All of the currently active tile streams. (Put in an array so
* that we can refer to them by number.)
*/
TileStream *streams[MAX_TILE_STREAMS];
// +-----------------+-------------------------------------------------
// | Predeclarations |
// +-----------------+
static void invert_pixels (GimpPixelRgn *rgn);
// +-----------------+-------------------------------------------------
// | Local Utilities |
// +-----------------+
/**
* Copy pixels into a region.
*/
static int
copy_pixels (GimpPixelRgn *rgn, int size, guchar *data)
{
int expected_size = rgn->h * rgn->rowstride;
if (expected_size != size)
{
fprintf (stderr, "Sizes don't match: %d != %d\n", size, expected_size);
return 0;
} // if the sizes don't match
memcpy (rgn->data, data, size);
return 1;
} // copy_pixels
/**
* Invert pixels in a region. (Intended mostly for testing.)
*/
static void
invert_pixels (GimpPixelRgn *rgn)
{
guchar *data = rgn->data;
int r;
for (r = 0; r < rgn->h; r++)
{
int c;
for (c = 0; c < rgn->rowstride; c++)
{
data[c] = (guchar) (255 - data[c]);
} // for c
data += rgn->rowstride;
} // for r
} // invert_pixels
/**
* Get the next available iterator id.
*/
static int
next_iterator_id (void)
{
int i;
for (i = 0; i < MAX_TILE_STREAMS; i++)
{
if (streams[i] == NULL)
return i;
} // for
return -1;
} // next_iterator_id
// +--------------+----------------------------------------------------
// | Constructors |
// +--------------+
/**
* Get a read-write tile iterator for a portion of a drawable.
* Returns -1 if it cannot create the iterator.
*/
int
rectangle_new_tile_stream (int image, int drawable,
int left, int top,
int width, int height)
{
// Get an id to use for the iterator
int id = next_iterator_id ();
if (id < 0)
{
return id;
}
// Allocate space for information on the iterator
TileStream *stream = (TileStream *) g_malloc0 (sizeof (TileStream));
if (stream == NULL)
{
return -1;
}
// Fill in the basic data
stream->image = image;
stream->drawable = drawable;
stream->left = left;
stream->top = top;
stream->width = width;
stream->height = height;
stream->n = 0;
stream->source = gimp_drawable_get (drawable);
if (stream->source == NULL)
{
g_free (stream);
return -1;
} // if we could not get the drawable
stream->target = gimp_drawable_get (drawable);
if (stream->target == NULL)
{
g_free (stream->source);
g_free (stream);
return -1;
}
// Fill in the more advanced data
gimp_pixel_rgn_init (&(stream->source_region),
stream->source,
left, top, width, height,
FALSE, FALSE);
gimp_pixel_rgn_init (&(stream->target_region),
stream->target,
left, top, width, height,
TRUE, TRUE);
stream->iterator = gimp_pixel_rgns_register (2, &(stream->source_region),
&(stream->target_region));
// Copy pixels over in case the user doesn't change them
if (stream->iterator != NULL)
{
copy_pixels (&(stream->target_region),
stream->source_region.rowstride * stream->source_region.h,
stream->source_region.data);
} // if the iterator is not null
// And we're done
streams[id] = stream;
return id;
} // rectangle_new_tile_stream
/**
* Get a tile stream for a drawable.
*/
int
drawable_new_tile_stream (int image, int drawable)
{
return rectangle_new_tile_stream (image, drawable,
0, 0,
gimp_image_width (image),
gimp_image_height (image));
} // drawable_new_tile_stream
// +-----------------+-------------------------------------------------
// | Primary Methods |
// +-----------------+
/**
* Advance to the next tile. Returns true if it succeeds and
* false otherwise.
*/
gboolean
tile_stream_advance (int id)
{
// Sanity check
if (! tile_stream_is_valid (id))
return 0;
// Get the stream
TileStream *stream = streams[id];
// Make sure that the iterator is not already at the end
if (stream->iterator == NULL)
return 0;
// Advance the iterator. This has the side effect of changing
// streams[id]->source_region and streams[id]->target_region.
stream->iterator = gimp_pixel_rgns_process (stream->iterator);
// Update the number
++(stream->n);
// Copy pixels over in case the user doesn't change them
if (stream->iterator != NULL)
{
copy_pixels (&(stream->target_region),
stream->source_region.rowstride * stream->source_region.h,
stream->source_region.data);
} // if we're not at the end
// Did we succeed?
return (stream->iterator != NULL);
} // tile_stream_advance
/**
* Close the tile stream, writing changes back (I hope).
*/
void
tile_stream_close (int id)
{
// Validate the stream
if (! tile_stream_is_valid (id))
return;
// Advance to the end of the stream so that any remaining pixels
// get cpies.
while (tile_stream_advance (id))
;
// And update!
TileStream *stream = streams[id];
gimp_drawable_flush (stream->target);
gimp_drawable_merge_shadow (stream->drawable, TRUE);
gimp_drawable_update (stream->drawable,
stream->left, stream->top,
stream->width, stream->height);
gimp_displays_flush ();
gimp_drawable_detach (stream->source);
gimp_drawable_detach (stream->target);
g_free (stream);
streams[id] = NULL;
#ifdef DEBUG
fprintf (stderr, "closed %d\n", id);
#endif
} // tile_stream_close
/**
* Get the data from the current tile.
*/
GimpPixelRgn *
tile_stream_get (int id)
{
// Sanity checks
if (! tile_stream_is_valid (id))
return NULL;
if (streams[id]->iterator == NULL)
return NULL;
return &(streams[id]->source_region);
} // tile_stream_get
int
tile_stream_is_valid (int id)
{
return ((id >= 0)
&& (id < MAX_TILE_STREAMS)
&& (streams[id] != NULL));
} // tile_stream_is_valid
/**
* Update the pixel data in the current tile.
*/
int
tile_update (int id, int size, guchar *data)
{
if (! tile_stream_is_valid (id))
return -1;
copy_pixels (&(streams[id]->target_region), size, data);
return 0;
} // tile__update