mgk.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. % %
  4. % %
  5. % %
  6. % M M GGGG K K %
  7. % MM MM G K K %
  8. % M M M G GG KKK %
  9. % M M G G K K %
  10. % M M GGG K K %
  11. % %
  12. % %
  13. % Read/Write MGK Image Format. %
  14. % %
  15. % Software Design %
  16. % John Cristy %
  17. % July 1992 %
  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. Include declarations.
  40. */
  41. #include <MagickCore/studio.h>
  42. #include <MagickCore/blob.h>
  43. #include <MagickCore/cache.h>
  44. #include <MagickCore/colorspace.h>
  45. #include <MagickCore/exception.h>
  46. #include <MagickCore/image.h>
  47. #include <MagickCore/list.h>
  48. #include <MagickCore/magick.h>
  49. #include <MagickCore/memory_.h>
  50. #include <MagickCore/monitor.h>
  51. #include <MagickCore/pixel-accessor.h>
  52. #include <MagickCore/string_.h>
  53. #include <MagickCore/module.h>
  54. /*
  55. Forward declarations.
  56. */
  57. static MagickBooleanType
  58. WriteMGKImage(const ImageInfo *,Image *,ExceptionInfo *);
  59. /*
  60. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  61. % %
  62. % %
  63. % %
  64. % I s M G K %
  65. % %
  66. % %
  67. % %
  68. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  69. %
  70. % IsMGK() returns MagickTrue if the image format type, identified by the
  71. % magick string, is MGK.
  72. %
  73. % The format of the IsMGK method is:
  74. %
  75. % MagickBooleanType IsMGK(const unsigned char *magick,const size_t length)
  76. %
  77. % A description of each parameter follows:
  78. %
  79. % o magick: This string is generally the first few bytes of an image file
  80. % or blob.
  81. %
  82. % o length: Specifies the length of the magick string.
  83. %
  84. */
  85. static MagickBooleanType IsMGK(const unsigned char *magick,const size_t length)
  86. {
  87. if (length < 7)
  88. return(MagickFalse);
  89. if (LocaleNCompare((char *) magick,"id=mgk",7) == 0)
  90. return(MagickTrue);
  91. return(MagickFalse);
  92. }
  93. /*
  94. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  95. % %
  96. % %
  97. % %
  98. % R e a d M G K I m a g e %
  99. % %
  100. % %
  101. % %
  102. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  103. %
  104. % ReadMGKImage() reads a MGK image file and returns it. It allocates the
  105. % memory necessary for the new Image structure and returns a pointer to the
  106. % new image.
  107. %
  108. % The format of the ReadMGKImage method is:
  109. %
  110. % Image *ReadMGKImage(const ImageInfo *image_info,
  111. % ExceptionInfo *exception)
  112. %
  113. % A description of each parameter follows:
  114. %
  115. % o image_info: the image info.
  116. %
  117. % o exception: return any errors or warnings in this structure.
  118. %
  119. */
  120. static Image *ReadMGKImage(const ImageInfo *image_info,ExceptionInfo *exception)
  121. {
  122. char
  123. buffer[MaxTextExtent];
  124. Image
  125. *image;
  126. long
  127. y;
  128. MagickBooleanType
  129. status;
  130. register long
  131. x;
  132. register Quantum
  133. *q;
  134. register unsigned char
  135. *p;
  136. ssize_t
  137. count;
  138. unsigned char
  139. *pixels;
  140. unsigned long
  141. columns,
  142. rows;
  143. /*
  144. Open image file.
  145. */
  146. assert(image_info != (const ImageInfo *) NULL);
  147. assert(image_info->signature == MagickCoreSignature);
  148. if (image_info->debug != MagickFalse)
  149. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
  150. image_info->filename);
  151. assert(exception != (ExceptionInfo *) NULL);
  152. assert(exception->signature == MagickCoreSignature);
  153. image=AcquireImage(image_info,exception);
  154. status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
  155. if (status == MagickFalse)
  156. {
  157. image=DestroyImageList(image);
  158. return((Image *) NULL);
  159. }
  160. /*
  161. Read MGK image.
  162. */
  163. (void) ReadBlobString(image,buffer); /* read magic number */
  164. if (IsMGK(buffer,7) == MagickFalse)
  165. ThrowReaderException(CorruptImageError,"ImproperImageHeader");
  166. (void) ReadBlobString(image,buffer);
  167. count=(ssize_t) sscanf(buffer,"%lu %lu\n",&columns,&rows);
  168. if (count <= 0)
  169. ThrowReaderException(CorruptImageError,"ImproperImageHeader");
  170. do
  171. {
  172. /*
  173. Initialize image structure.
  174. */
  175. image->columns=columns;
  176. image->rows=rows;
  177. image->depth=8;
  178. if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
  179. if (image->scene >= (image_info->scene+image_info->number_scenes-1))
  180. break;
  181. /*
  182. Convert MGK raster image to pixel packets.
  183. */
  184. if (SetImageExtent(image,image->columns,image->rows,exception) == MagickFalse)
  185. return(DestroyImageList(image));
  186. pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
  187. 3UL*sizeof(*pixels));
  188. if (pixels == (unsigned char *) NULL)
  189. ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
  190. for (y=0; y < (ssize_t) image->rows; y++)
  191. {
  192. count=(ssize_t) ReadBlob(image,(size_t) (3*image->columns),pixels);
  193. if (count != (ssize_t) (3*image->columns))
  194. ThrowReaderException(CorruptImageError,"UnableToReadImageData");
  195. p=pixels;
  196. q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
  197. if (q == (Quantum *) NULL)
  198. break;
  199. for (x=0; x < (ssize_t) image->columns; x++)
  200. {
  201. SetPixelRed(image,ScaleCharToQuantum(*p++),q);
  202. SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
  203. SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
  204. q+=GetPixelChannels(image);
  205. }
  206. if (SyncAuthenticPixels(image,exception) == MagickFalse)
  207. break;
  208. if (image->previous == (Image *) NULL)
  209. if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
  210. (QuantumTick(y,image->rows) != MagickFalse))
  211. {
  212. status=image->progress_monitor(LoadImageTag,y,image->rows,
  213. image->client_data);
  214. if (status == MagickFalse)
  215. break;
  216. }
  217. }
  218. pixels=(unsigned char *) RelinquishMagickMemory(pixels);
  219. if (EOFBlob(image) != MagickFalse)
  220. {
  221. ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
  222. image->filename);
  223. break;
  224. }
  225. /*
  226. Proceed to next image.
  227. */
  228. if (image_info->number_scenes != 0)
  229. if (image->scene >= (image_info->scene+image_info->number_scenes-1))
  230. break;
  231. *buffer='\0';
  232. (void) ReadBlobString(image,buffer);
  233. count=(ssize_t) sscanf(buffer,"%lu %lu\n",&columns,&rows);
  234. if (count > 0)
  235. {
  236. /*
  237. Allocate next image structure.
  238. */
  239. AcquireNextImage(image_info,image,exception);
  240. if (GetNextImageInList(image) == (Image *) NULL)
  241. {
  242. status=MagickFalse;
  243. break;
  244. }
  245. image=SyncNextImageInList(image);
  246. if (image->progress_monitor != (MagickProgressMonitor) NULL)
  247. {
  248. status=SetImageProgress(image,LoadImageTag,TellBlob(image),
  249. GetBlobSize(image));
  250. if (status == MagickFalse)
  251. break;
  252. }
  253. }
  254. } while (count > 0);
  255. (void) CloseBlob(image);
  256. if (status == MagickFalse)
  257. return(DestroyImageList(image));
  258. return(GetFirstImageInList(image));
  259. }
  260. /*
  261. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  262. % %
  263. % %
  264. % %
  265. % R e g i s t e r M G K I m a g e %
  266. % %
  267. % %
  268. % %
  269. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  270. %
  271. % RegisterMGKImage() adds attributes for the MGK image format to
  272. % the list of supported formats. The attributes include the image format
  273. % tag, a method to read and/or write the format, whether the format
  274. % supports the saving of more than one frame to the same file or blob,
  275. % whether the format supports native in-memory I/O, and a brief
  276. % description of the format.
  277. %
  278. % The format of the RegisterMGKImage method is:
  279. %
  280. % unsigned long RegisterMGKImage(void)
  281. %
  282. */
  283. ModuleExport unsigned long RegisterMGKImage(void)
  284. {
  285. MagickInfo
  286. *entry;
  287. entry=AcquireMagickInfo("MGK","MGK","MGK image");
  288. entry->decoder=(DecodeImageHandler *) ReadMGKImage;
  289. entry->encoder=(EncodeImageHandler *) WriteMGKImage;
  290. entry->magick=(IsImageFormatHandler *) IsMGK;
  291. (void) RegisterMagickInfo(entry);
  292. return(MagickImageCoderSignature);
  293. }
  294. /*
  295. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  296. % %
  297. % %
  298. % %
  299. % U n r e g i s t e r M G K I m a g e %
  300. % %
  301. % %
  302. % %
  303. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  304. %
  305. % UnregisterMGKImage() removes format registrations made by the
  306. % MGK module from the list of supported formats.
  307. %
  308. % The format of the UnregisterMGKImage method is:
  309. %
  310. % UnregisterMGKImage(void)
  311. %
  312. */
  313. ModuleExport void UnregisterMGKImage(void)
  314. {
  315. (void) UnregisterMagickInfo("MGK");
  316. }
  317. /*
  318. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  319. % %
  320. % %
  321. % %
  322. % W r i t e M G K I m a g e %
  323. % %
  324. % %
  325. % %
  326. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  327. %
  328. % WriteMGKImage() writes an image to a file in red, green, and blue MGK
  329. % rasterfile format.
  330. %
  331. % The format of the WriteMGKImage method is:
  332. %
  333. % MagickBooleanType WriteMGKImage(const ImageInfo *image_info,
  334. % Image *image)
  335. %
  336. % A description of each parameter follows.
  337. %
  338. % o image_info: the image info.
  339. %
  340. % o image: The image.
  341. %
  342. % o exception: return any errors or warnings in this structure.
  343. %
  344. */
  345. static MagickBooleanType WriteMGKImage(const ImageInfo *image_info,Image *image,
  346. ExceptionInfo *exception)
  347. {
  348. char
  349. buffer[MaxTextExtent];
  350. long
  351. y;
  352. MagickBooleanType
  353. status;
  354. MagickOffsetType
  355. scene;
  356. register const Quantum
  357. *p;
  358. register long
  359. x;
  360. register unsigned char
  361. *q;
  362. unsigned char
  363. *pixels;
  364. /*
  365. Open output image file.
  366. */
  367. assert(image_info != (const ImageInfo *) NULL);
  368. assert(image_info->signature == MagickCoreSignature);
  369. assert(image != (Image *) NULL);
  370. assert(image->signature == MagickCoreSignature);
  371. if (image->debug != MagickFalse)
  372. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  373. status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
  374. if (status == MagickFalse)
  375. return(status);
  376. scene=0;
  377. do
  378. {
  379. /*
  380. Allocate memory for pixels.
  381. */
  382. if (image->colorspace != RGBColorspace)
  383. (void) SetImageColorspace(image,RGBColorspace,exception);
  384. pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
  385. 3UL*sizeof(*pixels));
  386. if (pixels == (unsigned char *) NULL)
  387. ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
  388. /*
  389. Initialize raster file header.
  390. */
  391. (void) WriteBlobString(image,"id=mgk\n");
  392. (void) FormatLocaleString(buffer,MaxTextExtent,"%lu %lu\n",image->columns,
  393. image->rows);
  394. (void) WriteBlobString(image,buffer);
  395. for (y=0; y < (ssize_t) image->rows; y++)
  396. {
  397. p=GetVirtualPixels(image,0,y,image->columns,1,exception);
  398. if (p == (const Quantum *) NULL)
  399. break;
  400. q=pixels;
  401. for (x=0; x < (ssize_t) image->columns; x++)
  402. {
  403. *q++=ScaleQuantumToChar(GetPixelRed(image,p));
  404. *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
  405. *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
  406. p+=GetPixelChannels(image);
  407. }
  408. (void) WriteBlob(image,(size_t) (q-pixels),pixels);
  409. if (image->previous == (Image *) NULL)
  410. if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
  411. (QuantumTick(y,image->rows) != MagickFalse))
  412. {
  413. status=image->progress_monitor(SaveImageTag,y,image->rows,
  414. image->client_data);
  415. if (status == MagickFalse)
  416. break;
  417. }
  418. }
  419. pixels=(unsigned char *) RelinquishMagickMemory(pixels);
  420. if (GetNextImageInList(image) == (Image *) NULL)
  421. break;
  422. image=SyncNextImageInList(image);
  423. status=SetImageProgress(image,SaveImagesTag,scene,
  424. GetImageListLength(image));
  425. if (status == MagickFalse)
  426. break;
  427. scene++;
  428. } while (image_info->adjoin != MagickFalse);
  429. (void) CloseBlob(image);
  430. return(MagickTrue);
  431. }