2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Garmin Image Format %
19 % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization %
20 % dedicated to making software imaging solutions freely available. %
22 % You may not use this file except in compliance with the License. You may %
23 % obtain a copy of the License at %
25 % http://www.imagemagick.org/script/license.php %
27 % Unless required by applicable law or agreed to in writing, software %
28 % distributed under the License is distributed on an "AS IS" BASIS, %
29 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
30 % See the License for the specific language governing permissions and %
31 % limitations under the License. %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45 #include "MagickCore/studio.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/colorspace-private.h"
51 #include "MagickCore/draw.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/image.h"
55 #include "MagickCore/image-private.h"
56 #include "MagickCore/list.h"
57 #include "MagickCore/magick.h"
58 #include "MagickCore/memory_.h"
59 #include "MagickCore/module.h"
60 #include "MagickCore/monitor.h"
61 #include "MagickCore/monitor-private.h"
62 #include "MagickCore/property.h"
63 #include "MagickCore/pixel-accessor.h"
64 #include "MagickCore/quantum-private.h"
65 #include "MagickCore/static.h"
66 #include "MagickCore/string_.h"
68 typedef struct _JNXInfo
92 typedef struct _JNXLevelInfo
102 copyright[MagickPathExtent];
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % R e a d J N X I m a g e %
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 % ReadJNXImage() reads an image in the Garmin tile storage format and returns
117 % it. It allocates the memory necessary for the new Image structure and
118 % returns a pointer to the new image.
120 % The format of the ReadJNXImage method is:
122 % Image *ReadJNXImage(const ImageInfo *image_info,ExceptionInfo *exception)
124 % A description of each parameter follows:
126 % o image_info: the image info.
128 % o exception: return any errors or warnings in this structure.
131 static Image *ReadJNXImage(const ImageInfo *image_info,ExceptionInfo *exception)
133 #define JNXMaxLevels 20
143 jnx_level_info[JNXMaxLevels];
154 assert(image_info != (const ImageInfo *) NULL);
155 assert(image_info->signature == MagickCoreSignature);
156 if (image_info->debug != MagickFalse)
157 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
158 image_info->filename);
159 assert(exception != (ExceptionInfo *) NULL);
160 assert(exception->signature == MagickCoreSignature);
161 image=AcquireImage(image_info,exception);
162 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
163 if (status == MagickFalse)
165 image=DestroyImageList(image);
166 return((Image *) NULL);
171 (void) ResetMagickMemory(&jnx_info,0,sizeof(jnx_info));
172 jnx_info.version=ReadBlobLSBSignedLong(image);
173 if ((jnx_info.version != 3) && (jnx_info.version != 4))
174 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
175 jnx_info.serial=ReadBlobLSBSignedLong(image);
176 jnx_info.northeast.x=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
177 jnx_info.northeast.y=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
178 jnx_info.southwest.x=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
179 jnx_info.southwest.y=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
180 jnx_info.levels=ReadBlobLSBSignedLong(image);
181 if (jnx_info.levels > JNXMaxLevels)
182 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
183 jnx_info.expire=ReadBlobLSBSignedLong(image);
184 jnx_info.id=ReadBlobLSBSignedLong(image);
185 jnx_info.crc=ReadBlobLSBSignedLong(image);
186 jnx_info.signature=ReadBlobLSBSignedLong(image);
187 jnx_info.offset=ReadBlobLSBLong(image);
188 if (jnx_info.version > 3)
189 jnx_info.order=ReadBlobLSBSignedLong(image);
191 if (jnx_info.version == 3)
196 (void) ResetMagickMemory(&jnx_level_info,0,sizeof(jnx_level_info));
197 for (i=0; i < (ssize_t) jnx_info.levels; i++)
199 jnx_level_info[i].count=ReadBlobLSBSignedLong(image);
200 if (jnx_level_info[i].count > 50000)
201 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
202 jnx_level_info[i].offset=ReadBlobLSBSignedLong(image);
203 jnx_level_info[i].scale=ReadBlobLSBLong(image);
204 if (jnx_info.version > 3)
212 (void) ReadBlobLSBLong(image);
214 while ((c=ReadBlobLSBShort(image)) != 0)
215 if (j < (MagickPathExtent-1))
216 jnx_level_info[i].copyright[j++]=c;
217 jnx_level_info[i].copyright[j]=0;
223 images=NewImageList();
224 for (i=0; i < (ssize_t) jnx_info.levels; i++)
232 offset=SeekBlob(image,(MagickOffsetType) jnx_level_info[i].offset,SEEK_SET);
233 if (offset != (MagickOffsetType) jnx_level_info[i].offset)
235 for (j=0; j < (ssize_t) jnx_level_info[i].count; j++)
262 northeast.x=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
263 northeast.y=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
264 southwest.x=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
265 southwest.y=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
266 (void) ReadBlobLSBShort(image); /* width */
267 (void) ReadBlobLSBShort(image); /* height */
268 tile_length=ReadBlobLSBLong(image);
269 tile_offset=ReadBlobLSBSignedLong(image);
270 if (tile_offset == -1)
272 restore_offset=TellBlob(image);
273 if (restore_offset < 0)
275 offset=SeekBlob(image,(MagickOffsetType) tile_offset,SEEK_SET);
276 if (offset != (MagickOffsetType) tile_offset)
281 blob=(unsigned char *) AcquireQuantumMemory((size_t) tile_length+2,
283 if (blob == (unsigned char *) NULL)
285 if (images != (Image *) NULL)
286 images=DestroyImageList(images);
287 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
291 count=ReadBlob(image,tile_length,blob+2);
292 if (count != (ssize_t) tile_length)
294 if (images != (Image *) NULL)
295 images=DestroyImageList(images);
296 blob=(unsigned char *) RelinquishMagickMemory(blob);
297 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
299 read_info=CloneImageInfo(image_info);
300 (void) CopyMagickString(read_info->magick,"JPEG",MagickPathExtent);
301 tile_image=BlobToImage(read_info,blob,tile_length+2,exception);
302 read_info=DestroyImageInfo(read_info);
303 blob=(unsigned char *) RelinquishMagickMemory(blob);
304 offset=SeekBlob(image,restore_offset,SEEK_SET);
305 if (tile_image == (Image *) NULL)
307 (void) CopyMagickString(tile_image->magick,image->magick,MagickPathExtent);
308 (void) FormatImageProperty(tile_image,"jnx:northeast","%.20g,%.20g",
309 northeast.x,northeast.y);
310 (void) FormatImageProperty(tile_image,"jnx:southwest","%.20g,%.20g",
311 southwest.x,southwest.y);
312 AppendImageToList(&images,tile_image);
314 if (image->progress_monitor != (MagickProgressMonitor) NULL)
319 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType) i,
320 (MagickSizeType) jnx_info.levels);
321 if (proceed == MagickFalse)
325 (void) CloseBlob(image);
326 image=DestroyImage(image);
327 if (images == (Image *) NULL)
328 return((Image *) NULL);
329 return(GetFirstImageInList(images));
333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
337 % R e g i s t e r J N X I m a g e %
341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343 % RegisterJNXImage() adds attributes for the JNX image format to the list
344 % of supported formats. The attributes include the image format tag, a
345 % method to read and/or write the format, whether the format supports the
346 % saving of more than one frame to the same file or blob, whether the format
347 % supports native in-memory I/O, and a brief description of the format.
349 % The format of the RegisterJNXImage method is:
351 % size_t RegisterJNXImage(void)
354 ModuleExport size_t RegisterJNXImage(void)
359 entry=AcquireMagickInfo("JNX","JNX","Garmin tile format");
360 entry->decoder=(DecodeImageHandler *) ReadJNXImage;
361 entry->flags|=CoderSeekableStreamFlag;
362 (void) RegisterMagickInfo(entry);
363 return(MagickImageCoderSignature);
367 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
371 % U n r e g i s t e r J N X I m a g e %
375 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377 % UnregisterJNXImage() removes format registrations made by the
378 % JNX module from the list of supported formats.
380 % The format of the UnregisterJNXImage method is:
382 % UnregisterJNXImage(void)
385 ModuleExport void UnregisterJNXImage(void)
387 (void) UnregisterMagickInfo("JNX");