/src/imagemagick/coders/ashlar.c
Line | Count | Source |
1 | | /* |
2 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
3 | | % % |
4 | | % % |
5 | | % % |
6 | | % AAA SSSSS H H L AAA RRRR % |
7 | | % A A SS H H L A A R R % |
8 | | % AAAAA SSS HHHHH L AAAAA RRRR % |
9 | | % A A SS H H L A A R R % |
10 | | % A A SSSSS H H LLLLL A A R R % |
11 | | % % |
12 | | % % |
13 | | % Write Ashlar Images % |
14 | | % % |
15 | | % Software Design % |
16 | | % Cristy % |
17 | | % July 2020 % |
18 | | % % |
19 | | % % |
20 | | % Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization % |
21 | | % dedicated to making software imaging solutions freely available. % |
22 | | % % |
23 | | % You may not use this file except in compliance with the License. You may % |
24 | | % obtain a copy of the License at % |
25 | | % % |
26 | | % https://imagemagick.org/script/license.php % |
27 | | % % |
28 | | % Unless required by applicable law or agreed to in writing, software % |
29 | | % distributed under the License is distributed on an "AS IS" BASIS, % |
30 | | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
31 | | % See the License for the specific language governing permissions and % |
32 | | % limitations under the License. % |
33 | | % % |
34 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
35 | | % |
36 | | % |
37 | | */ |
38 | | |
39 | | /* |
40 | | Include declarations. |
41 | | */ |
42 | | #include "MagickCore/studio.h" |
43 | | #include "MagickCore/annotate.h" |
44 | | #include "MagickCore/blob.h" |
45 | | #include "MagickCore/blob-private.h" |
46 | | #include "MagickCore/client.h" |
47 | | #include "MagickCore/constitute.h" |
48 | | #include "MagickCore/display.h" |
49 | | #include "MagickCore/exception.h" |
50 | | #include "MagickCore/exception-private.h" |
51 | | #include "MagickCore/image.h" |
52 | | #include "MagickCore/image-private.h" |
53 | | #include "MagickCore/list.h" |
54 | | #include "MagickCore/magick.h" |
55 | | #include "MagickCore/memory_.h" |
56 | | #include "MagickCore/module.h" |
57 | | #include "MagickCore/option.h" |
58 | | #include "MagickCore/property.h" |
59 | | #include "MagickCore/quantum-private.h" |
60 | | #include "MagickCore/static.h" |
61 | | #include "MagickCore/string_.h" |
62 | | #include "MagickCore/string-private.h" |
63 | | #include "MagickCore/thread-private.h" |
64 | | #include "MagickCore/utility.h" |
65 | | #include "MagickCore/xwindow.h" |
66 | | #include "MagickCore/xwindow-private.h" |
67 | | |
68 | | /* |
69 | | Forward declarations. |
70 | | */ |
71 | | static MagickBooleanType |
72 | | WriteASHLARImage(const ImageInfo *,Image *,ExceptionInfo *); |
73 | | |
74 | | /* |
75 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
76 | | % % |
77 | | % % |
78 | | % % |
79 | | % R e g i s t e r A S H L A R I m a g e % |
80 | | % % |
81 | | % % |
82 | | % % |
83 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
84 | | % |
85 | | % RegisterASHLARImage() adds attributes for the ASHLAR image format to |
86 | | % the list of supported formats. The attributes include the image format |
87 | | % tag, a method to read and/or write the format, whether the format |
88 | | % supports the saving of more than one frame to the same file or blob, |
89 | | % whether the format supports native in-memory I/O, and a brief |
90 | | % description of the format. |
91 | | % |
92 | | % The format of the RegisterASHLARImage method is: |
93 | | % |
94 | | % size_t RegisterASHLARImage(void) |
95 | | % |
96 | | */ |
97 | | ModuleExport size_t RegisterASHLARImage(void) |
98 | 7 | { |
99 | 7 | MagickInfo |
100 | 7 | *entry; |
101 | | |
102 | 7 | entry=AcquireMagickInfo("ASHLAR","ASHLAR", |
103 | 7 | "Image sequence laid out in continuous irregular courses"); |
104 | 7 | entry->encoder=(EncodeImageHandler *) WriteASHLARImage; |
105 | 7 | (void) RegisterMagickInfo(entry); |
106 | 7 | return(MagickImageCoderSignature); |
107 | 7 | } |
108 | | |
109 | | /* |
110 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
111 | | % % |
112 | | % % |
113 | | % % |
114 | | % U n r e g i s t e r A S H L A R I m a g e % |
115 | | % % |
116 | | % % |
117 | | % % |
118 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
119 | | % |
120 | | % UnregisterASHLARImage() removes format registrations made by the |
121 | | % ASHLAR module from the list of supported formats. |
122 | | % |
123 | | % The format of the UnregisterASHLARImage method is: |
124 | | % |
125 | | % UnregisterASHLARImage(void) |
126 | | % |
127 | | */ |
128 | | ModuleExport void UnregisterASHLARImage(void) |
129 | 0 | { |
130 | 0 | (void) UnregisterMagickInfo("ASHLAR"); |
131 | 0 | } |
132 | | |
133 | | /* |
134 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
135 | | % % |
136 | | % % |
137 | | % % |
138 | | % W r i t e A S H L A R I m a g e % |
139 | | % % |
140 | | % % |
141 | | % % |
142 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
143 | | % |
144 | | % WriteASHLARImage() writes an image to a file in ASHLAR format. |
145 | | % |
146 | | % The format of the WriteASHLARImage method is: |
147 | | % |
148 | | % MagickBooleanType WriteASHLARImage(const ImageInfo *image_info, |
149 | | % Image *image,ExceptionInfo *exception) |
150 | | % |
151 | | % A description of each parameter follows. |
152 | | % |
153 | | % o image_info: the image info. |
154 | | % |
155 | | % o image: The image. |
156 | | % |
157 | | % o exception: return any errors or warnings in this structure. |
158 | | % |
159 | | */ |
160 | | |
161 | | typedef struct _NodeInfo |
162 | | { |
163 | | ssize_t |
164 | | x, |
165 | | y; |
166 | | |
167 | | struct _NodeInfo |
168 | | *next; |
169 | | } NodeInfo; |
170 | | |
171 | | typedef struct _AshlarInfo |
172 | | { |
173 | | size_t |
174 | | width, |
175 | | height, |
176 | | number_nodes; |
177 | | |
178 | | ssize_t |
179 | | align; |
180 | | |
181 | | MagickBooleanType |
182 | | best_fit; |
183 | | |
184 | | NodeInfo |
185 | | *current, |
186 | | *free, |
187 | | head, |
188 | | sentinel; |
189 | | } AshlarInfo; |
190 | | |
191 | | typedef struct _CanvasInfo |
192 | | { |
193 | | ssize_t |
194 | | id; |
195 | | |
196 | | size_t |
197 | | width, |
198 | | height; |
199 | | |
200 | | ssize_t |
201 | | x, |
202 | | y, |
203 | | order; |
204 | | } CanvasInfo; |
205 | | |
206 | | typedef struct _TileInfo |
207 | | { |
208 | | ssize_t |
209 | | x, |
210 | | y; |
211 | | |
212 | | NodeInfo |
213 | | **previous; |
214 | | } TileInfo; |
215 | | |
216 | | static inline ssize_t FindMinimumTileLocation(NodeInfo *first,const ssize_t x, |
217 | | const size_t width,ssize_t *excess) |
218 | 0 | { |
219 | 0 | NodeInfo |
220 | 0 | *node; |
221 | |
|
222 | 0 | ssize_t |
223 | 0 | extent, |
224 | 0 | y; |
225 | | |
226 | | /* |
227 | | Find minimum y location if it starts at x. |
228 | | */ |
229 | 0 | *excess=0; |
230 | 0 | y=0; |
231 | 0 | extent=0; |
232 | 0 | node=first; |
233 | 0 | while (node->x < (x+(ssize_t) width)) |
234 | 0 | { |
235 | 0 | if (node->y > y) |
236 | 0 | { |
237 | 0 | *excess+=extent*(node->y-y); |
238 | 0 | y=node->y; |
239 | 0 | if (node->x < x) |
240 | 0 | extent+=node->next->x-x; |
241 | 0 | else |
242 | 0 | extent+=node->next->x-node->x; |
243 | 0 | } |
244 | 0 | else |
245 | 0 | { |
246 | 0 | size_t delta = (size_t) (node->next->x-node->x); |
247 | 0 | if ((delta+(size_t) extent) > width) |
248 | 0 | delta=width-(size_t) extent; |
249 | 0 | *excess+=(ssize_t) delta*(y-node->y); |
250 | 0 | extent+=(ssize_t) delta; |
251 | 0 | } |
252 | 0 | node=node->next; |
253 | 0 | } |
254 | 0 | return(y); |
255 | 0 | } |
256 | | |
257 | | static inline TileInfo AssignBestTileLocation(AshlarInfo *ashlar_info, |
258 | | const size_t width,size_t const height) |
259 | 0 | { |
260 | 0 | NodeInfo |
261 | 0 | *node, |
262 | 0 | **previous, |
263 | 0 | *tail; |
264 | |
|
265 | 0 | ssize_t |
266 | 0 | min_excess; |
267 | |
|
268 | 0 | size_t |
269 | 0 | ashlar_width; |
270 | |
|
271 | 0 | TileInfo |
272 | 0 | tile; |
273 | | |
274 | | /* |
275 | | Align along left edge. |
276 | | */ |
277 | 0 | tile.previous=(NodeInfo **) NULL; |
278 | 0 | ashlar_width=(size_t) ((ssize_t) width+ashlar_info->align-1); |
279 | 0 | ashlar_width-=(size_t) ((ssize_t) ashlar_width % ashlar_info->align); |
280 | 0 | if ((ashlar_width > ashlar_info->width) || (height > ashlar_info->height)) |
281 | 0 | { |
282 | | /* |
283 | | Tile can't fit, bail. |
284 | | */ |
285 | 0 | tile.x=0; |
286 | 0 | tile.y=0; |
287 | 0 | return(tile); |
288 | 0 | } |
289 | 0 | tile.x=(ssize_t) MAGICK_SSIZE_MAX; |
290 | 0 | tile.y=(ssize_t) MAGICK_SSIZE_MAX; |
291 | 0 | min_excess=(ssize_t) MAGICK_SSIZE_MAX; |
292 | 0 | node=ashlar_info->current; |
293 | 0 | previous=(&ashlar_info->current); |
294 | 0 | while (((ssize_t) ashlar_width+node->x) <= (ssize_t) ashlar_info->width) |
295 | 0 | { |
296 | 0 | ssize_t |
297 | 0 | excess, |
298 | 0 | y; |
299 | |
|
300 | 0 | y=FindMinimumTileLocation(node,node->x,ashlar_width,&excess); |
301 | 0 | if (ashlar_info->best_fit == MagickFalse) |
302 | 0 | { |
303 | 0 | if (y < tile.y) |
304 | 0 | { |
305 | 0 | tile.y=y; |
306 | 0 | tile.previous=previous; |
307 | 0 | } |
308 | 0 | } |
309 | 0 | else |
310 | 0 | { |
311 | 0 | if (((ssize_t) height+y) <= (ssize_t) ashlar_info->height) |
312 | 0 | if ((y < tile.y) || ((y == tile.y) && (excess < min_excess))) |
313 | 0 | { |
314 | 0 | tile.y=y; |
315 | 0 | tile.previous=previous; |
316 | 0 | min_excess=excess; |
317 | 0 | } |
318 | 0 | } |
319 | 0 | previous=(&node->next); |
320 | 0 | node=node->next; |
321 | 0 | } |
322 | 0 | tile.x=(tile.previous == (NodeInfo **) NULL) ? 0 : (*tile.previous)->x; |
323 | 0 | if (ashlar_info->best_fit != MagickFalse) |
324 | 0 | { |
325 | | /* |
326 | | Align along both left and right edges. |
327 | | */ |
328 | 0 | tail=ashlar_info->current; |
329 | 0 | node=ashlar_info->current; |
330 | 0 | previous=(&ashlar_info->current); |
331 | 0 | while (tail->x < (ssize_t) ashlar_width) |
332 | 0 | tail=tail->next; |
333 | 0 | while (tail != (NodeInfo *) NULL) |
334 | 0 | { |
335 | 0 | ssize_t |
336 | 0 | excess, |
337 | 0 | x, |
338 | 0 | y; |
339 | |
|
340 | 0 | x=tail->x-(ssize_t) ashlar_width; |
341 | 0 | while (node->next->x <= x) |
342 | 0 | { |
343 | 0 | previous=(&node->next); |
344 | 0 | node=node->next; |
345 | 0 | } |
346 | 0 | y=FindMinimumTileLocation(node,x,ashlar_width,&excess); |
347 | 0 | if (((ssize_t) height+y) <= (ssize_t) ashlar_info->height) |
348 | 0 | { |
349 | 0 | if (y <= tile.y) |
350 | 0 | if ((y < tile.y) || (excess < min_excess) || |
351 | 0 | ((excess == min_excess) && (x < tile.x))) |
352 | 0 | { |
353 | 0 | tile.x=x; |
354 | 0 | tile.y=y; |
355 | 0 | min_excess=excess; |
356 | 0 | tile.previous=previous; |
357 | 0 | } |
358 | 0 | } |
359 | 0 | tail=tail->next; |
360 | 0 | } |
361 | 0 | } |
362 | 0 | return(tile); |
363 | 0 | } |
364 | | |
365 | | static inline TileInfo AssignTileLocation(AshlarInfo *ashlar_info, |
366 | | const size_t width,const size_t height) |
367 | 0 | { |
368 | 0 | NodeInfo |
369 | 0 | *current, |
370 | 0 | *node; |
371 | |
|
372 | 0 | TileInfo |
373 | 0 | tile; |
374 | | |
375 | | /* |
376 | | Find the best location in the canvas for this tile. |
377 | | */ |
378 | 0 | tile=AssignBestTileLocation(ashlar_info,width,height); |
379 | 0 | if ((tile.previous == (NodeInfo **) NULL) || |
380 | 0 | ((tile.y+(ssize_t) height) > (ssize_t) ashlar_info->height) || |
381 | 0 | (ashlar_info->free == (NodeInfo *) NULL)) |
382 | 0 | { |
383 | 0 | tile.previous=(NodeInfo **) NULL; |
384 | 0 | return(tile); |
385 | 0 | } |
386 | | /* |
387 | | Create a new node. |
388 | | */ |
389 | 0 | node=ashlar_info->free; |
390 | 0 | node->x=(ssize_t) tile.x; |
391 | 0 | node->y=tile.y+(ssize_t) height; |
392 | 0 | ashlar_info->free=node->next; |
393 | | /* |
394 | | Insert node. |
395 | | */ |
396 | 0 | current=(*tile.previous); |
397 | 0 | if (current->x >= tile.x) |
398 | 0 | *tile.previous=node; |
399 | 0 | else |
400 | 0 | { |
401 | 0 | NodeInfo *next = current->next; |
402 | 0 | current->next=node; |
403 | 0 | current=next; |
404 | 0 | } |
405 | 0 | while ((current->next != (NodeInfo *) NULL) && |
406 | 0 | (current->next->x <= (tile.x+(ssize_t) width))) |
407 | 0 | { |
408 | | /* |
409 | | Push current node to free list. |
410 | | */ |
411 | 0 | NodeInfo *next = current->next; |
412 | 0 | current->next=ashlar_info->free; |
413 | 0 | ashlar_info->free=current; |
414 | 0 | current=next; |
415 | 0 | } |
416 | 0 | node->next=current; |
417 | 0 | if (current->x < (tile.x+(ssize_t) width)) |
418 | 0 | current->x=tile.x+(ssize_t) width; |
419 | 0 | return(tile); |
420 | 0 | } |
421 | | |
422 | | static inline int CompareTileHeight(const void *p_tile,const void *q_tile) |
423 | 0 | { |
424 | 0 | const CanvasInfo |
425 | 0 | *p, |
426 | 0 | *q; |
427 | |
|
428 | 0 | p=(const CanvasInfo *) p_tile; |
429 | 0 | q=(const CanvasInfo *) q_tile; |
430 | 0 | if (p->height > q->height) |
431 | 0 | return(-1); |
432 | 0 | if (p->height < q->height) |
433 | 0 | return(1); |
434 | 0 | return((p->width > q->width) ? -1 : (p->width < q->width) ? 1 : 0); |
435 | 0 | } |
436 | | |
437 | | static inline int RestoreTileOrder(const void *p_tile,const void *q_tile) |
438 | 0 | { |
439 | 0 | const CanvasInfo |
440 | 0 | *p, |
441 | 0 | *q; |
442 | |
|
443 | 0 | p=(const CanvasInfo *) p_tile; |
444 | 0 | q=(const CanvasInfo *) q_tile; |
445 | 0 | return((p->order < q->order) ? -1 : (p->order > q->order) ? 1 : 0); |
446 | 0 | } |
447 | | |
448 | | static inline MagickBooleanType PackAshlarTiles(AshlarInfo *ashlar_info, |
449 | | const size_t number_tiles,CanvasInfo *tiles) |
450 | 0 | { |
451 | 0 | MagickBooleanType |
452 | 0 | status; |
453 | |
|
454 | 0 | ssize_t |
455 | 0 | i; |
456 | | |
457 | | /* |
458 | | Pack tiles so they fit the canvas with minimum excess. |
459 | | */ |
460 | | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
461 | | #pragma omp parallel for schedule(dynamic) shared(status) |
462 | | #endif |
463 | 0 | for (i=0; i < (ssize_t) number_tiles; i++) |
464 | 0 | tiles[i].order=(i); |
465 | 0 | qsort((void *) tiles,number_tiles,sizeof(*tiles),CompareTileHeight); |
466 | 0 | for (i=0; i < (ssize_t) number_tiles; i++) |
467 | 0 | { |
468 | 0 | tiles[i].x=0; |
469 | 0 | tiles[i].y=0; |
470 | 0 | if ((tiles[i].width != 0) && (tiles[i].height != 0)) |
471 | 0 | { |
472 | 0 | TileInfo |
473 | 0 | tile_info; |
474 | |
|
475 | 0 | tile_info=AssignTileLocation(ashlar_info,tiles[i].width, |
476 | 0 | tiles[i].height); |
477 | 0 | tiles[i].x=(ssize_t) tile_info.x; |
478 | 0 | tiles[i].y=(ssize_t) tile_info.y; |
479 | 0 | if (tile_info.previous == (NodeInfo **) NULL) |
480 | 0 | { |
481 | 0 | tiles[i].x=(ssize_t) MAGICK_SSIZE_MAX; |
482 | 0 | tiles[i].y=(ssize_t) MAGICK_SSIZE_MAX; |
483 | 0 | } |
484 | 0 | } |
485 | 0 | } |
486 | 0 | qsort((void *) tiles,number_tiles,sizeof(*tiles),RestoreTileOrder); |
487 | 0 | status=MagickTrue; |
488 | | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
489 | | #pragma omp parallel for schedule(dynamic) shared(status) |
490 | | #endif |
491 | 0 | for (i=0; i < (ssize_t) number_tiles; i++) |
492 | 0 | { |
493 | 0 | tiles[i].order=(ssize_t) ((tiles[i].x != (ssize_t) MAGICK_SSIZE_MAX) || |
494 | 0 | (tiles[i].y != (ssize_t) MAGICK_SSIZE_MAX) ? 1 : 0); |
495 | 0 | if (tiles[i].order == 0) |
496 | 0 | status=MagickFalse; |
497 | 0 | } |
498 | 0 | return(status); /* return true if room is found for all tiles */ |
499 | 0 | } |
500 | | |
501 | | static Image *ASHLARImage(ImageInfo *image_info,Image *image, |
502 | | ExceptionInfo *exception) |
503 | 0 | { |
504 | 0 | AshlarInfo |
505 | 0 | ashlar_info; |
506 | |
|
507 | 0 | CanvasInfo |
508 | 0 | *tiles; |
509 | |
|
510 | 0 | const char |
511 | 0 | *value; |
512 | |
|
513 | 0 | Image |
514 | 0 | *ashlar_image, |
515 | 0 | *next; |
516 | |
|
517 | 0 | MagickBooleanType |
518 | 0 | status; |
519 | |
|
520 | 0 | NodeInfo |
521 | 0 | *nodes; |
522 | |
|
523 | 0 | RectangleInfo |
524 | 0 | extent, |
525 | 0 | geometry; |
526 | |
|
527 | 0 | ssize_t |
528 | 0 | i, |
529 | 0 | n; |
530 | | |
531 | | /* |
532 | | Convert image sequence laid out in continuous irregular courses. |
533 | | */ |
534 | 0 | if (image_info->extract != (char *) NULL) |
535 | 0 | (void) ParseAbsoluteGeometry(image_info->extract,&geometry); |
536 | 0 | else |
537 | 0 | { |
538 | | /* |
539 | | Determine a sane canvas size and border width. |
540 | | */ |
541 | 0 | (void) ParseAbsoluteGeometry("0x0+0+0",&geometry); |
542 | 0 | for (next=image; next != (Image *) NULL; next=GetNextImageInList(next)) |
543 | 0 | { |
544 | 0 | geometry.width+=next->columns; |
545 | 0 | geometry.height+=next->rows; |
546 | 0 | } |
547 | 0 | geometry.width=(size_t) geometry.width/7; |
548 | 0 | geometry.height=(size_t) geometry.height/7; |
549 | 0 | geometry.x=(ssize_t) pow((double) geometry.width,0.4); |
550 | 0 | geometry.y=(ssize_t) pow((double) geometry.height,0.4); |
551 | 0 | image_info->extract=AcquireString(""); |
552 | 0 | if (image_info->extract != (char *) NULL) |
553 | 0 | (void) FormatLocaleString(image_info->extract,MagickPathExtent, |
554 | 0 | "%gx%g%+g%+g",(double) geometry.width,(double) geometry.height, |
555 | 0 | (double) geometry.x,(double) geometry.y); |
556 | 0 | } |
557 | | /* |
558 | | Initialize image tiles. |
559 | | */ |
560 | 0 | ashlar_image=AcquireImage(image_info,exception); |
561 | 0 | status=SetImageExtent(ashlar_image,geometry.width,geometry.height,exception); |
562 | 0 | if (status == MagickFalse) |
563 | 0 | { |
564 | 0 | ashlar_image=DestroyImageList(ashlar_image); |
565 | 0 | return((Image *) NULL); |
566 | 0 | } |
567 | 0 | (void) SetImageBackgroundColor(ashlar_image,exception); |
568 | 0 | tiles=(CanvasInfo *) AcquireQuantumMemory(GetImageListLength(image), |
569 | 0 | sizeof(*tiles)); |
570 | 0 | ashlar_info.number_nodes=2*geometry.width; |
571 | 0 | nodes=(NodeInfo *) AcquireQuantumMemory(ashlar_info.number_nodes, |
572 | 0 | sizeof(*nodes)); |
573 | 0 | if ((tiles == (CanvasInfo *) NULL) || (nodes == (NodeInfo *) NULL)) |
574 | 0 | { |
575 | 0 | if (tiles != (CanvasInfo *) NULL) |
576 | 0 | tiles=(CanvasInfo *) RelinquishMagickMemory(tiles); |
577 | 0 | if (nodes != (NodeInfo *) NULL) |
578 | 0 | nodes=(NodeInfo *) RelinquishMagickMemory(tiles); |
579 | 0 | ashlar_image=DestroyImageList(ashlar_image); |
580 | 0 | ThrowImageException(ResourceLimitError,"MemoryAllocationFailed"); |
581 | 0 | } |
582 | | /* |
583 | | Iterate until we find a tile size that fits the canvas. |
584 | | */ |
585 | 0 | value=GetImageOption(image_info,"ashlar:best-fit"); |
586 | 0 | for (i=20; i > 0; i--) |
587 | 0 | { |
588 | 0 | ssize_t |
589 | 0 | j; |
590 | |
|
591 | 0 | n=0; |
592 | 0 | for (next=image; next != (Image *) NULL; next=GetNextImageInList(next)) |
593 | 0 | { |
594 | 0 | tiles[n].id=n; |
595 | 0 | tiles[n].width=(size_t) (0.05*i*next->columns+2*geometry.x); |
596 | 0 | tiles[n].height=(size_t) (0.05*i*next->rows+2*geometry.y); |
597 | 0 | n++; |
598 | 0 | } |
599 | 0 | for (j=0; j < (ssize_t) ashlar_info.number_nodes-1; j++) |
600 | 0 | nodes[j].next=nodes+j+1; |
601 | 0 | nodes[j].next=(NodeInfo *) NULL; |
602 | 0 | ashlar_info.best_fit=IsStringTrue(value) != MagickFalse ? MagickTrue : |
603 | 0 | MagickFalse; |
604 | 0 | ashlar_info.free=nodes; |
605 | 0 | ashlar_info.current=(&ashlar_info.head); |
606 | 0 | ashlar_info.width=geometry.width; |
607 | 0 | ashlar_info.height=geometry.height; |
608 | 0 | ashlar_info.align=(ssize_t) ((ashlar_info.width+ashlar_info.number_nodes-1)/ |
609 | 0 | ashlar_info.number_nodes); |
610 | 0 | ashlar_info.head.x=0; |
611 | 0 | ashlar_info.head.y=0; |
612 | 0 | ashlar_info.head.next=(&ashlar_info.sentinel); |
613 | 0 | ashlar_info.sentinel.x=(ssize_t) geometry.width; |
614 | 0 | ashlar_info.sentinel.y=(ssize_t) MAGICK_SSIZE_MAX; |
615 | 0 | ashlar_info.sentinel.next=(NodeInfo *) NULL; |
616 | 0 | status=PackAshlarTiles(&ashlar_info,(size_t) n,tiles); |
617 | 0 | if (status != MagickFalse) |
618 | 0 | break; |
619 | 0 | } |
620 | | /* |
621 | | Determine layout of images tiles on the canvas. |
622 | | */ |
623 | 0 | value=GetImageOption(image_info,"label"); |
624 | 0 | extent.width=0; |
625 | 0 | extent.height=0; |
626 | | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
627 | | #pragma omp parallel for schedule(dynamic) shared(status,extent) \ |
628 | | magick_number_threads(image,image,n,1) |
629 | | #endif |
630 | 0 | for (i=0; i < n; i++) |
631 | 0 | { |
632 | 0 | Image |
633 | 0 | *tile_image; |
634 | |
|
635 | 0 | if (status == MagickFalse) |
636 | 0 | continue; |
637 | 0 | if ((tiles[i].x == (ssize_t) MAGICK_SSIZE_MAX) || |
638 | 0 | (tiles[i].y == (ssize_t) MAGICK_SSIZE_MAX)) |
639 | 0 | continue; |
640 | 0 | tile_image=ResizeImage(GetImageFromList(image,tiles[i].id),(size_t) |
641 | 0 | ((ssize_t) tiles[i].width-2*geometry.x),(size_t) |
642 | 0 | ((ssize_t) tiles[i].height-2*geometry.y),image->filter,exception); |
643 | 0 | if (tile_image == (Image *) NULL) |
644 | 0 | { |
645 | 0 | status=MagickFalse; |
646 | 0 | continue; |
647 | 0 | } |
648 | 0 | status=CompositeImage(ashlar_image,tile_image,image->compose,MagickTrue, |
649 | 0 | tiles[i].x+geometry.x,tiles[i].y+geometry.y,exception); |
650 | 0 | if (status == MagickFalse) |
651 | 0 | continue; |
652 | 0 | if (value != (const char *) NULL) |
653 | 0 | { |
654 | 0 | char |
655 | 0 | *label, |
656 | 0 | offset[MagickPathExtent]; |
657 | |
|
658 | 0 | DrawInfo |
659 | 0 | *draw_info = CloneDrawInfo(image_info,(DrawInfo *) NULL); |
660 | |
|
661 | 0 | label=InterpretImageProperties((ImageInfo *) image_info,tile_image, |
662 | 0 | value,exception); |
663 | 0 | if (label != (const char *) NULL) |
664 | 0 | { |
665 | 0 | (void) CloneString(&draw_info->text,label); |
666 | 0 | label=DestroyString(label); |
667 | 0 | (void) FormatLocaleString(offset,MagickPathExtent,"%+g%+g",(double) |
668 | 0 | tiles[i].x+geometry.x,(double) tiles[i].height+tiles[i].y- |
669 | 0 | geometry.y/2.0+4); |
670 | 0 | (void) CloneString(&draw_info->geometry,offset); |
671 | 0 | status=AnnotateImage(ashlar_image,draw_info,exception); |
672 | 0 | } |
673 | 0 | } |
674 | | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
675 | | #pragma omp critical |
676 | | #endif |
677 | 0 | { |
678 | 0 | if (((ssize_t) tiles[i].width+tiles[i].x) > (ssize_t) extent.width) |
679 | 0 | extent.width=(size_t) ((ssize_t) tiles[i].width+tiles[i].x); |
680 | 0 | if (((ssize_t) tiles[i].height+tiles[i].y+geometry.y+2) > (ssize_t) extent.height) |
681 | 0 | extent.height=(size_t) ((ssize_t) tiles[i].height+tiles[i].y+ |
682 | 0 | geometry.y+2); |
683 | 0 | } |
684 | 0 | tile_image=DestroyImage(tile_image); |
685 | 0 | } |
686 | 0 | if (image_info->extract != (char *) NULL) |
687 | 0 | (void) ParseAbsoluteGeometry(image_info->extract,&extent); |
688 | 0 | (void) SetImageExtent(ashlar_image,extent.width,extent.height,exception); |
689 | 0 | nodes=(NodeInfo *) RelinquishMagickMemory(nodes); |
690 | 0 | tiles=(CanvasInfo *) RelinquishMagickMemory(tiles); |
691 | 0 | if (status == MagickFalse) |
692 | 0 | ashlar_image=DestroyImage(ashlar_image); |
693 | 0 | return(ashlar_image); |
694 | 0 | } |
695 | | |
696 | | static MagickBooleanType WriteASHLARImage(const ImageInfo *image_info, |
697 | | Image *image,ExceptionInfo *exception) |
698 | 0 | { |
699 | 0 | const char |
700 | 0 | *value; |
701 | |
|
702 | 0 | const MagickInfo |
703 | 0 | *magick_info; |
704 | |
|
705 | 0 | Image |
706 | 0 | *ashlar_images; |
707 | |
|
708 | 0 | ImageInfo |
709 | 0 | *write_info; |
710 | |
|
711 | 0 | MagickBooleanType |
712 | 0 | status; |
713 | |
|
714 | 0 | size_t |
715 | 0 | tiles_per_page; |
716 | |
|
717 | 0 | ssize_t |
718 | 0 | i; |
719 | | |
720 | | /* |
721 | | Write ASHLAR canvas. |
722 | | */ |
723 | 0 | assert(image_info != (const ImageInfo *) NULL); |
724 | 0 | assert(image_info->signature == MagickCoreSignature); |
725 | 0 | assert(image != (Image *) NULL); |
726 | 0 | assert(image->signature == MagickCoreSignature); |
727 | 0 | if (IsEventLogging() != MagickFalse) |
728 | 0 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
729 | 0 | tiles_per_page=GetImageListLength(image); |
730 | 0 | value=GetImageOption(image_info,"ashlar:tiles"); |
731 | 0 | if (value != (const char *) NULL) |
732 | 0 | tiles_per_page=(size_t) MagickMax(StringToInteger(value),1); |
733 | 0 | ashlar_images=NewImageList(); |
734 | 0 | write_info=CloneImageInfo(image_info); |
735 | 0 | for (i=0; i < (ssize_t) GetImageListLength(image); i+=(ssize_t) tiles_per_page) |
736 | 0 | { |
737 | 0 | char |
738 | 0 | scenes[MagickPathExtent]; |
739 | |
|
740 | 0 | Image |
741 | 0 | *ashlar_image, |
742 | 0 | *clone_images; |
743 | |
|
744 | 0 | (void) FormatLocaleString(scenes,MagickPathExtent,"%g-%g",(double) i, |
745 | 0 | (double) (i+(ssize_t) tiles_per_page-1)); |
746 | 0 | clone_images=CloneImages(image,scenes,exception); |
747 | 0 | if (clone_images == (Image *) NULL) |
748 | 0 | { |
749 | 0 | if (ashlar_images != (Image *) NULL) |
750 | 0 | ashlar_images=DestroyImageList(ashlar_images); |
751 | 0 | break; |
752 | 0 | } |
753 | 0 | ashlar_image=ASHLARImage(write_info,clone_images,exception); |
754 | 0 | clone_images=DestroyImageList(clone_images); |
755 | 0 | if (ashlar_image == (Image *) NULL) |
756 | 0 | { |
757 | 0 | if (ashlar_images != (Image *) NULL) |
758 | 0 | ashlar_images=DestroyImageList(ashlar_images); |
759 | 0 | break; |
760 | 0 | } |
761 | 0 | AppendImageToList(&ashlar_images,ashlar_image); |
762 | 0 | } |
763 | 0 | if (ashlar_images == (Image *) NULL) |
764 | 0 | return(MagickFalse); |
765 | 0 | ashlar_images=GetFirstImageInList(ashlar_images); |
766 | 0 | (void) CopyMagickString(ashlar_images->filename,image_info->filename, |
767 | 0 | MagickPathExtent); |
768 | 0 | *write_info->magick='\0'; |
769 | 0 | (void) SetImageInfo(write_info,(unsigned int) |
770 | 0 | GetImageListLength(ashlar_images),exception); |
771 | 0 | magick_info=GetMagickInfo(write_info->magick,exception); |
772 | 0 | if ((magick_info == (const MagickInfo*) NULL) || |
773 | 0 | (LocaleCompare(magick_info->magick_module,"ASHLAR") == 0)) |
774 | 0 | (void) FormatLocaleString(ashlar_images->filename,MagickPathExtent, |
775 | 0 | "miff:%s",write_info->filename); |
776 | 0 | status=WriteImages(write_info,ashlar_images,ashlar_images->filename, |
777 | 0 | exception); |
778 | 0 | ashlar_images=DestroyImageList(ashlar_images); |
779 | 0 | write_info=DestroyImageInfo(write_info); |
780 | 0 | return(status); |
781 | 0 | } |