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
|
/**
* Copyright: Mike Wey 2011
* License: zlib (See accompanying LICENSE file)
* Authors: Mike Wey
*/
module dmagick.ImageView;
import std.array;
import std.parallelism;
import std.range;
import std.string;
import core.atomic;
import dmagick.Color;
import dmagick.Exception;
import dmagick.Geometry;
import dmagick.Image;
import dmagick.c.cache;
import dmagick.c.exception;
import dmagick.c.geometry;
import dmagick.c.image : MagickCoreImage = Image;
import dmagick.c.magickType;
import dmagick.c.memory;
import dmagick.c.pixel;
//These symbols are publicly imported by dmagick.Image.
private alias dmagick.c.magickType.Quantum Quantum;
alias ptrdiff_t ssize_t;
/**
* The ImageView allows changing induvidual pixels with the slicing and
* indexing operators.
*
* --------------------
* ImageView view = image.view();
*
* //Assign a square.
* view[4..40][5..50] = new Color("red");
*
* //Reduce a view.
* view = view[10..view.extend.height-10][20..view.extend.width-20];
*
* //Assign a single row.
* view[30] = new Color("blue");
* //Or a column.
* view[][30] = new Color("blue");
* //And induvidual pixels.
* view[3][5] = new Color("green");
*
* //We can also use foreach.
* foreach ( row; view )
* {
* //This is executed in parallel.
* foreach ( ref pixel; row )
* pixel = new Color("black");
* }
* --------------------
*/
class ImageView
{
Image image;
RectangleInfo extent;
/**
* Create a new view for image.
*/
this(Image image, Geometry area)
{
if ( area.width + area.xOffset > image.columns ||
area.height + area.yOffset > image.rows )
{
throw new OptionException("Specified area is larger than the image");
}
this.image = image;
this.extent = area.rectangleInfo;
}
/**
* The width of the view.
*/
@property size_t width() const
{
return extent.width;
}
/**
* The height of the view.
*/
@property size_t height() const
{
return extent.height;
}
/**
* The height or the width of the view, depending on in which slice
* it's used.
*
* Bugs: dmd bug 3474: opDollar isn't implemented.
*/
size_t opDollar() const
{
return extent.height;
}
/**
* Indexing operators yield or modify the value at a specified index.
*/
Pixels opIndex(size_t row)
{
return Pixels(image, extent.x, extent.y + row, extent.width, 1);
}
///ditto
void opIndexAssign(Color color, size_t index)
{
this[index][] = color;
}
/**
* Sliceing operators yield or modify the value in the specified slice.
*/
ImageView opSlice()
{
return opSlice(0, extent.height);
}
///ditto
ImageView opSlice(size_t upper, size_t lower)
{
RectangleInfo newExtent = extent;
newExtent.y += upper;
newExtent.height = lower - upper;
return new Rows(image, Geometry(newExtent));
}
///ditto
void opSliceAssign(Color color)
{
foreach ( row; this )
row[] = color;
}
///ditto
void opSliceAssign(Color color, size_t upper, size_t lower)
{
foreach ( row; this[upper .. lower] )
row[] = color;
}
/**
* Support the usage of foreach to loop over the rows in the view.
* The foreach is executed in parallel.
*/
int opApply(int delegate(ref Pixels) dg)
{
shared(int) progress;
foreach ( row; taskPool.parallel(iota(extent.y, extent.y + extent.height)) )
{
int result = dg(Pixels(image, extent.x, row, extent.width, 1));
if ( result )
return result;
if ( image.monitor !is null )
{
atomicOp!"+="(progress, 1);
image.monitor()("ImageView/" ~ image.filename, progress, extent.height);
}
}
return 0;
}
}
/*
* A Rows object is returned when a ImageView is sliced, this is to support
* sliceing the columns with a sceond slice.
*/
class Rows : ImageView
{
this(Image image, Geometry area)
{
super(image, area);
}
/*
* The height or the width of the view, depending on in which slice
* it's used.
*
* Bugs: dmd bug 3474: opDollar isn't implemented.
*/
override size_t opDollar() const
{
return extent.width;
}
/*
* Indexing operators yield or modify the value at a specified index.
*/
override Pixels opIndex(size_t column)
{
return Pixels(image, extent.x + column, extent.y, 1, extent.height);
}
/*
* Sliceing operators yield or modify the value in the specified slice.
*/
override ImageView opSlice()
{
return opSlice(0, extent.width);
}
///ditto
override ImageView opSlice(size_t left, size_t right)
{
RectangleInfo newExtent = extent;
newExtent.x += left;
newExtent.width = right - left;
return new ImageView(image, Geometry(newExtent));
}
}
/**
* Row reprecents a singe row of pixels in an ImageView.
*/
struct Pixels
{
Image image;
PixelPacket[] pixels;
private size_t* refcount;
private NexusInfo nexus;
/**
* Get the pixels of the specifies area in the image.
*/
this(Image image, ssize_t x, ssize_t y, size_t columns, size_t rows)
{
this.image = image;
Quantum* data =
GetAuthenticPixelCacheNexus(image.imageRef, x, y, columns, rows, &nexus, DMagickExceptionInfo());
this.pixels = (cast(PixelPacket*)data)[0..columns*rows];
refcount = new size_t;
*refcount = 1;
}
/*
* Copy constructor.
*/
private this(Image image, PixelPacket[] pixels, size_t* refCount, NexusInfo nexus)
{
this.image = image;
this.pixels = pixels;
this.refcount = refcount;
this.nexus = nexus;
(*refcount)++;
}
this(this)
{
if ( !pixels.empty )
(*refcount)++;
}
~this()
{
if ( pixels.empty )
return;
(*refcount)--;
if ( *refcount == 0 )
{
sync();
if ( !nexus.mapped )
RelinquishMagickMemory(nexus.cache);
else
UnmapBlob(nexus.cache, cast(size_t)nexus.length);
nexus.cache = null;
}
}
/**
* The number of pixels in this row / column.
*
* Bugs: dmd bug 3474: opDollar isn't implemented.
*/
@property size_t length() const
{
return pixels.length;
}
///ditto
size_t opDollar() const
{
return pixels.length;
}
/**
* Indexing operators yield or modify the value at a specified index.
*/
Color opIndex(size_t pixel)
{
return new Color(pixels.ptr + pixel);
}
///ditto
void opIndexAssign(Color color, size_t index)
{
pixels[index] = color.pixelPacket;
}
/**
* Sliceing operators yield or modify the value in the specified slice.
*/
Pixels opSlice()
{
return this;
}
///ditto
Pixels opSilce(size_t left, size_t right)
{
return Pixels(image, pixels[left .. right], refcount, nexus);
}
///ditto
void opSliceAssign(Color color)
{
opSliceAssign(color, 0, pixels.length);
}
///ditto
void opSliceAssign(Color color, size_t left, size_t right)
{
foreach( i; left .. right )
this[i] = color;
}
/**
* Sync the pixels back to the image. The destructor does this for you.
*/
void sync()
{
SyncAuthenticPixelCacheNexus(image.imageRef, &nexus, DMagickExceptionInfo());
}
/**
* Support using foreach on a row.
*/
int opApply(T : Color)(int delegate(ref T) dg)
{
T color = new T();
foreach ( ref PixelPacket pixel; pixels )
{
color.pixelPacket = pixel;
int result = dg(color);
pixel = color.pixelPacket;
if ( result )
return result;
}
return 0;
}
unittest
{
Image image = new Image(Geometry(100, 100), new Color("Blue"));
{
Pixels row = Pixels(image, 25, 50, 50, 1);
row[] = new Color("red");
}
assert(image.view[50][50] == new Color("red"));
}
}
/*
* Note: these defenitions aren't public.
*/
private extern(C)
{
struct NexusInfo
{
MagickBooleanType
mapped;
RectangleInfo
region;
MagickSizeType
length;
Quantum*
cache,
pixels;
void*
metacontent;
size_t
signature;
}
Quantum* GetAuthenticPixelCacheNexus(MagickCoreImage* image, const ssize_t x, const ssize_t y, const size_t columns, const size_t rows, NexusInfo* nexus_info, ExceptionInfo* exception);
MagickBooleanType SyncAuthenticPixelCacheNexus(MagickCoreImage* image, NexusInfo* nexus_info, ExceptionInfo* exception);
MagickBooleanType UnmapBlob(void* map, const size_t length);
}
|