2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Garmin Image Format %
19 % Copyright 1999-2018 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 % https://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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 #include "MagickCore/studio.h"
42 #include "MagickCore/blob.h"
43 #include "MagickCore/blob-private.h"
44 #include "MagickCore/cache.h"
45 #include "MagickCore/colorspace.h"
46 #include "MagickCore/colorspace-private.h"
47 #include "MagickCore/draw.h"
48 #include "MagickCore/exception.h"
49 #include "MagickCore/exception-private.h"
50 #include "MagickCore/image.h"
51 #include "MagickCore/image-private.h"
52 #include "MagickCore/list.h"
53 #include "MagickCore/magick.h"
54 #include "MagickCore/memory_.h"
55 #include "MagickCore/module.h"
56 #include "MagickCore/monitor.h"
57 #include "MagickCore/monitor-private.h"
58 #include "MagickCore/property.h"
59 #include "MagickCore/pixel-accessor.h"
60 #include "MagickCore/quantum-private.h"
61 #include "MagickCore/static.h"
62 #include "MagickCore/string_.h"
64 typedef struct _JNXInfo
88 typedef struct _JNXLevelInfo
98 copyright[MagickPathExtent];
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 % R e a d J N X I m a g e %
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 % ReadJNXImage() reads an image in the Garmin tile storage format and returns
113 % it. It allocates the memory necessary for the new Image structure and
114 % returns a pointer to the new image.
116 % The format of the ReadJNXImage method is:
118 % Image *ReadJNXImage(const ImageInfo *image_info,ExceptionInfo *exception)
120 % A description of each parameter follows:
122 % o image_info: the image info.
124 % o exception: return any errors or warnings in this structure.
127 static Image *ReadJNXImage(const ImageInfo *image_info,ExceptionInfo *exception)
129 #define JNXMaxLevels 20
139 jnx_level_info[JNXMaxLevels];
150 assert(image_info != (const ImageInfo *) NULL);
151 assert(image_info->signature == MagickCoreSignature);
152 if (image_info->debug != MagickFalse)
153 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
154 image_info->filename);
155 assert(exception != (ExceptionInfo *) NULL);
156 assert(exception->signature == MagickCoreSignature);
157 image=AcquireImage(image_info,exception);
158 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
159 if (status == MagickFalse)
161 image=DestroyImageList(image);
162 return((Image *) NULL);
167 (void) memset(&jnx_info,0,sizeof(jnx_info));
168 jnx_info.version=ReadBlobLSBSignedLong(image);
169 if ((jnx_info.version != 3) && (jnx_info.version != 4))
170 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
171 jnx_info.serial=ReadBlobLSBSignedLong(image);
172 jnx_info.northeast.x=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
173 jnx_info.northeast.y=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
174 jnx_info.southwest.x=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
175 jnx_info.southwest.y=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
176 jnx_info.levels=ReadBlobLSBSignedLong(image);
177 if (jnx_info.levels > JNXMaxLevels)
178 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
179 jnx_info.expire=ReadBlobLSBSignedLong(image);
180 jnx_info.id=ReadBlobLSBSignedLong(image);
181 jnx_info.crc=ReadBlobLSBSignedLong(image);
182 jnx_info.signature=ReadBlobLSBSignedLong(image);
183 jnx_info.offset=ReadBlobLSBLong(image);
184 if (jnx_info.version > 3)
185 jnx_info.order=ReadBlobLSBSignedLong(image);
187 if (jnx_info.version == 3)
189 if (EOFBlob(image) != MagickFalse)
190 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
194 (void) memset(&jnx_level_info,0,sizeof(jnx_level_info));
195 for (i=0; i < (ssize_t) jnx_info.levels; i++)
197 jnx_level_info[i].count=ReadBlobLSBSignedLong(image);
198 if (jnx_level_info[i].count > 50000)
199 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
200 jnx_level_info[i].offset=ReadBlobLSBSignedLong(image);
201 jnx_level_info[i].scale=ReadBlobLSBLong(image);
202 *jnx_level_info[i].copyright='\0';
203 if (jnx_info.version > 3)
211 (void) ReadBlobLSBLong(image);
213 while ((c=ReadBlobLSBShort(image)) != 0)
214 if (j < (MagickPathExtent-1))
215 jnx_level_info[i].copyright[j++]=c;
216 jnx_level_info[i].copyright[j]='\0';
218 if (EOFBlob(image) != MagickFalse)
219 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
224 images=NewImageList();
225 for (i=0; i < (ssize_t) jnx_info.levels; i++)
233 offset=SeekBlob(image,(MagickOffsetType) jnx_level_info[i].offset,SEEK_SET);
234 if (offset != (MagickOffsetType) jnx_level_info[i].offset)
236 for (j=0; j < (ssize_t) jnx_level_info[i].count; j++)
263 northeast.x=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
264 northeast.y=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
265 southwest.x=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
266 southwest.y=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
267 (void) ReadBlobLSBShort(image); /* width */
268 (void) ReadBlobLSBShort(image); /* height */
269 if (EOFBlob(image) != MagickFalse)
270 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
271 tile_length=ReadBlobLSBLong(image);
272 tile_offset=ReadBlobLSBSignedLong(image);
273 if (tile_offset == -1)
275 restore_offset=TellBlob(image);
276 if (restore_offset < 0)
278 offset=SeekBlob(image,(MagickOffsetType) tile_offset,SEEK_SET);
279 if (offset != (MagickOffsetType) tile_offset)
284 if (((MagickSizeType) tile_length) > GetBlobSize(image))
285 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
286 blob=(unsigned char *) AcquireQuantumMemory((size_t) tile_length+2,
288 if (blob == (unsigned char *) NULL)
290 if (images != (Image *) NULL)
291 images=DestroyImageList(images);
292 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
296 count=ReadBlob(image,tile_length,blob+2);
297 if (count != (ssize_t) tile_length)
299 if (images != (Image *) NULL)
300 images=DestroyImageList(images);
301 blob=(unsigned char *) RelinquishMagickMemory(blob);
302 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
304 read_info=CloneImageInfo(image_info);
305 (void) CopyMagickString(read_info->magick,"JPEG",MagickPathExtent);
306 tile_image=BlobToImage(read_info,blob,tile_length+2,exception);
307 read_info=DestroyImageInfo(read_info);
308 blob=(unsigned char *) RelinquishMagickMemory(blob);
309 offset=SeekBlob(image,restore_offset,SEEK_SET);
310 if (tile_image == (Image *) NULL)
312 (void) CopyMagickString(tile_image->magick,image->magick,
314 (void) FormatImageProperty(tile_image,"jnx:northeast","%.20g,%.20g",
315 northeast.x,northeast.y);
316 (void) FormatImageProperty(tile_image,"jnx:southwest","%.20g,%.20g",
317 southwest.x,southwest.y);
318 AppendImageToList(&images,tile_image);
320 if (image->progress_monitor != (MagickProgressMonitor) NULL)
325 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType) i,
326 (MagickSizeType) jnx_info.levels);
327 if (proceed == MagickFalse)
331 (void) CloseBlob(image);
332 image=DestroyImage(image);
333 if (images == (Image *) NULL)
334 return((Image *) NULL);
335 return(GetFirstImageInList(images));
339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343 % R e g i s t e r J N X I m a g e %
347 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
349 % RegisterJNXImage() adds attributes for the JNX image format to the list
350 % of supported formats. The attributes include the image format tag, a
351 % method to read and/or write the format, whether the format supports the
352 % saving of more than one frame to the same file or blob, whether the format
353 % supports native in-memory I/O, and a brief description of the format.
355 % The format of the RegisterJNXImage method is:
357 % size_t RegisterJNXImage(void)
360 ModuleExport size_t RegisterJNXImage(void)
365 entry=AcquireMagickInfo("JNX","JNX","Garmin tile format");
366 entry->decoder=(DecodeImageHandler *) ReadJNXImage;
367 entry->flags|=CoderDecoderSeekableStreamFlag;
368 (void) RegisterMagickInfo(entry);
369 return(MagickImageCoderSignature);
373 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377 % U n r e g i s t e r J N X I m a g e %
381 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
383 % UnregisterJNXImage() removes format registrations made by the
384 % JNX module from the list of supported formats.
386 % The format of the UnregisterJNXImage method is:
388 % UnregisterJNXImage(void)
391 ModuleExport void UnregisterJNXImage(void)
393 (void) UnregisterMagickInfo("JNX");