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
|
/*
% Copyright (C) 2003 - 2020 GraphicsMagick Group
% Copyright (C) 2003 ImageMagick Studio
% Copyright 1991-1999 E. I. du Pont de Nemours and Company
%
% This program is covered by multiple licenses, which are described in
% Copyright.txt. You should have received a copy of Copyright.txt with this
% package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
%
% GraphicsMagick Gradient Image Methods.
%
*/
/*
Include declarations.
*/
#include "magick/studio.h"
#include "magick/alpha_composite.h"
#include "magick/color.h"
#include "magick/colormap.h"
#include "magick/gradient.h"
#include "magick/log.h"
#include "magick/monitor.h"
#include "magick/pixel_cache.h"
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
+ G r a d i e n t I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% GradientImage() applies continuously smooth color transitions along a
% distance vector from one color to another.
%
% The default is to apply a gradient from the top of the image to the bottom.
% Since GraphicsMagick 1.3.35, this function responds to the image gravity
% attribute as follows:
%
% SouthGravity - Top to Bottom (Default)
% NorthGravity - Bottom to Top
% WestGravity - Right to Left
% EastGravity - Left to Right
% NorthWestGravity - Bottom-Right to Top-Left
% NorthEastGravity - Bottom-Left to Top-Right
% SouthWestGravity - Top-Right Bottom-Left
% SouthEastGravity - Top-Left to Bottom-Right
%
% Also, since GraphicsMagick 1.3.35, an effort is made to produce a
% PseudoClass image representation by default. If the gradient distance
% vector produces a number of points less than or equal to the maximum
% colormap size (MaxColormapSize), then a colormap is produced according
% to the order indicated by the start and stop colors. Otherwise a
% DirectClass image is created (as it always was prior to 1.3.35). The
% PseudoClass representation is suitably initialized so that changing
% the image storage class will lead to an immediately usable DirectClass
% image.
%
% Note, the interface of this method will change in the future to support
% more than one transistion.
%
% The format of the GradientImage method is:
%
% MagickPassFail GradientImage(Image *image,
% const PixelPacket *start_color,
% const PixelPacket *stop_color)
%
% A description of each parameter follows:
%
% o image: The image.
%
% o start_color: The start color.
%
% o stop_color: The stop color.
%
%
*/
#define GradientImageText "[%s] Gradient..."
MagickExport MagickPassFail GradientImage(Image *restrict image,
const PixelPacket *start_color,
const PixelPacket *stop_color)
{
PixelPacket
*pixel_packets;
double
alpha_scale,
x_origin = 0.0,
y_origin = 0.0;
size_t
span;
unsigned long
i;
long
y;
unsigned long
row_count=0;
#if defined(HAVE_OPENMP)
int num_threads = omp_get_max_threads();
#endif
MagickBool
monitor_active;
MagickPassFail
status=MagickPass;
assert(image != (const Image *) NULL);
assert(image->signature == MagickSignature);
assert(start_color != (const PixelPacket *) NULL);
assert(stop_color != (const PixelPacket *) NULL);
monitor_active=MagickMonitorActive();
/*
-define gradient:direction={NorthWest, North, Northeast, West, East, SouthWest, South, SouthEast}
South is the default
image->gravity
*/
/*
Computed required gradient span
*/
switch(image->gravity)
{
case SouthGravity:
case NorthGravity:
default:
span = image->rows;
break;
case WestGravity:
case EastGravity:
span = image->columns;
break;
case NorthWestGravity:
case NorthEastGravity:
case SouthWestGravity:
case SouthEastGravity:
span = (size_t) (sqrt(((double)image->columns-1)*((double)image->columns-1)+
((double)image->rows-1)*((double)image->rows-1))+0.5)+1;
break;
}
(void) LogMagickEvent(CoderEvent,GetMagickModule(),
"Gradient span %"MAGICK_SIZE_T_F"u", (MAGICK_SIZE_T) span);
/*
Determine origin pixel for diagonal gradients
*/
switch(image->gravity)
{
default:
break;
case NorthWestGravity:
/* Origin bottom-right */
x_origin = (double)image->columns-1;
y_origin = (double)image->rows-1;
break;
case NorthEastGravity:
/* Origin bottom-left */
x_origin = 0.0;
y_origin = (double)image->rows-1;
break;
case SouthWestGravity:
/* Origin top-right */
x_origin = (double)image->columns-1;
y_origin = 0.0;
break;
case SouthEastGravity:
/* Origin top-left */
x_origin = 0.0;
y_origin = 0.0;
break;
}
pixel_packets=MagickAllocateArray(PixelPacket *,span,sizeof(PixelPacket));
if (pixel_packets == (PixelPacket *) NULL)
ThrowBinaryException(ResourceLimitError,MemoryAllocationFailed,
image->filename);
if (span <= MaxColormapSize)
if (!AllocateImageColormap(image,(unsigned long) span))
{
MagickFreeMemory(pixel_packets);
ThrowException3(&image->exception, ResourceLimitError,MemoryAllocationFailed,
UnableToConstituteImage);
return MagickFail;
}
/*
Generate gradient pixels using alpha blending
OpenMP is not demonstrated to help here.
*/
alpha_scale = span > 1 ? ((MaxRGBDouble)/(span-1)) : MaxRGBDouble/2.0;
for (i=0; i < span; i++)
{
double alpha = (double)i*alpha_scale;
BlendCompositePixel(&pixel_packets[i],start_color,stop_color,alpha);
#if 0
fprintf(stdout, "%lu: %g (r=%u, g=%u, b=%u)\n", i, alpha,
(unsigned) pixel_packets[i].red,
(unsigned) pixel_packets[i].green,
(unsigned) pixel_packets[i].blue);
#endif
}
if (image->storage_class == PseudoClass)
(void) memcpy(image->colormap,pixel_packets,span*sizeof(PixelPacket));
/*
Copy gradient pixels to image rows
*/
#if defined(HAVE_OPENMP)
if (num_threads > 3)
num_threads = 3;
# if defined(TUNE_OPENMP)
# pragma omp parallel for if(num_threads > 1) num_threads(num_threads) schedule(runtime) shared(row_count, status)
# else
# pragma omp parallel for if(num_threads > 1) num_threads(num_threads) schedule(guided) shared(row_count, status)
# endif
#endif
for (y=0; y < (long) image->rows; y++)
{
MagickPassFail
thread_status;
register unsigned long
x;
register PixelPacket
*q;
register IndexPacket
*indexes = (IndexPacket *) NULL;
thread_status=status;
if (thread_status == MagickFail)
continue;
do
{
q=SetImagePixelsEx(image,0,y,image->columns,1,&image->exception);
if (q == (PixelPacket *) NULL)
{
thread_status=MagickFail;
break;
}
if (image->storage_class == PseudoClass)
{
indexes=AccessMutableIndexes(image);
if (indexes == (IndexPacket *) NULL)
{
thread_status=MagickFail;
break;
}
}
switch(image->gravity)
{
case SouthGravity:
default:
{
for (x=0; x < image->columns; x++)
q[x] = pixel_packets[y];
if (indexes)
for (x=0; x < image->columns; x++)
indexes[x]=(IndexPacket) y;
break;
}
case NorthGravity:
{
for (x=0; x < image->columns; x++)
q[x] = pixel_packets[image->columns-y];
if (indexes)
for (x=0; x < image->columns; x++)
indexes[x]=(IndexPacket) image->columns-y;
break;
}
case WestGravity:
{
for (x=0; x < image->columns; x++)
q[x] = pixel_packets[image->columns-x];
if (indexes)
for (x=0; x < image->columns; x++)
indexes[x]=(IndexPacket) image->columns-x;
break;
}
case EastGravity:
{
for (x=0; x < image->columns; x++)
q[x] = pixel_packets[x];
if (indexes)
for (x=0; x < image->columns; x++)
indexes[x]=(IndexPacket) x;
break;
}
case NorthWestGravity:
case NorthEastGravity:
case SouthWestGravity:
case SouthEastGravity:
{
/*
FIXME: Diagonal gradient should be based on distance
from perpendicular line!
*/
double ydf = (y_origin-(double)y)*(y_origin-(double)y);
for (x=0; x < image->columns; x++)
{
i = (unsigned long) (sqrt((x_origin-x)*(x_origin-x)+ydf)+0.5);
/* fprintf(stderr,"NW %lux%ld: %lu\n", x, y, (unsigned long) i); */
q[x] = pixel_packets[i];
if (indexes)
indexes[x]=(IndexPacket) i;
}
break;
}
}
if (!SyncImagePixelsEx(image,&image->exception))
{
thread_status=MagickFail;
break;
}
if (monitor_active)
{
unsigned long
thread_row_count;
#if defined(HAVE_OPENMP)
# pragma omp atomic
#endif
row_count++;
#if defined(HAVE_OPENMP)
# pragma omp flush (row_count)
#endif
thread_row_count=row_count;
if (QuantumTick(thread_row_count,image->rows))
if (!MagickMonitorFormatted(thread_row_count,image->rows,&image->exception,
GradientImageText,image->filename))
{
thread_status=MagickFail;
break;
}
}
} while(0);
if (thread_status == MagickFail)
{
status=MagickFail;
#if defined(HAVE_OPENMP)
# pragma omp flush (status)
#endif
}
}
if (IsGray(*start_color) && IsGray(*stop_color))
image->is_grayscale=MagickTrue;
if (IsMonochrome(*start_color) && ColorMatch(start_color,stop_color))
image->is_monochrome=MagickTrue;
MagickFreeMemory(pixel_packets);
return(status);
}
|