PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/coders/pix.c

https://gitlab.com/jiapei100/ImageMagick
C | 306 lines | 171 code | 8 blank | 127 comment | 63 complexity | b1be2c77dae8c7f305bdfd5c4abbe7ed MD5 | raw file
  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. % %
  4. % %
  5. % %
  6. % PPPP IIIII X X %
  7. % P P I X X %
  8. % PPPP I X %
  9. % P I X X %
  10. % P IIIII X X %
  11. % %
  12. % %
  13. % Read Alias/Wavefront RLE Image Format %
  14. % %
  15. % Software Design %
  16. % Cristy %
  17. % July 1992 %
  18. % %
  19. % %
  20. % Copyright 1999-2016 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. % http://www.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/blob-private.h"
  44. #include "MagickCore/cache.h"
  45. #include "MagickCore/colormap.h"
  46. #include "MagickCore/exception.h"
  47. #include "MagickCore/exception-private.h"
  48. #include "MagickCore/image.h"
  49. #include "MagickCore/image-private.h"
  50. #include "MagickCore/list.h"
  51. #include "MagickCore/magick.h"
  52. #include "MagickCore/memory_.h"
  53. #include "MagickCore/monitor.h"
  54. #include "MagickCore/monitor-private.h"
  55. #include "MagickCore/pixel-accessor.h"
  56. #include "MagickCore/quantum-private.h"
  57. #include "MagickCore/static.h"
  58. #include "MagickCore/string_.h"
  59. #include "MagickCore/module.h"
  60. /*
  61. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  62. % %
  63. % %
  64. % %
  65. % R e a d P I X I m a g e %
  66. % %
  67. % %
  68. % %
  69. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  70. %
  71. % ReadPIXImage() reads a Alias/Wavefront RLE image file and returns it.
  72. % It allocates the memory necessary for the new Image structure and returns a
  73. % pointer to the new image.
  74. %
  75. % The format of the ReadPIXImage method is:
  76. %
  77. % Image *ReadPIXImage(const ImageInfo *image_info,ExceptionInfo *exception)
  78. %
  79. % A description of each parameter follows:
  80. %
  81. % o image_info: the image info.
  82. %
  83. % o exception: return any errors or warnings in this structure.
  84. %
  85. %
  86. */
  87. static Image *ReadPIXImage(const ImageInfo *image_info,ExceptionInfo *exception)
  88. {
  89. Image
  90. *image;
  91. MagickBooleanType
  92. status;
  93. Quantum
  94. blue,
  95. green,
  96. index,
  97. red;
  98. register ssize_t
  99. x;
  100. register Quantum
  101. *q;
  102. size_t
  103. bits_per_pixel,
  104. height,
  105. length,
  106. width;
  107. ssize_t
  108. y;
  109. /*
  110. Open image file.
  111. */
  112. assert(image_info != (const ImageInfo *) NULL);
  113. assert(image_info->signature == MagickCoreSignature);
  114. if (image_info->debug != MagickFalse)
  115. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
  116. image_info->filename);
  117. assert(exception != (ExceptionInfo *) NULL);
  118. assert(exception->signature == MagickCoreSignature);
  119. image=AcquireImage(image_info,exception);
  120. status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
  121. if (status == MagickFalse)
  122. {
  123. image=DestroyImageList(image);
  124. return((Image *) NULL);
  125. }
  126. /*
  127. Read PIX image.
  128. */
  129. width=ReadBlobMSBShort(image);
  130. height=ReadBlobMSBShort(image);
  131. (void) ReadBlobMSBShort(image); /* x-offset */
  132. (void) ReadBlobMSBShort(image); /* y-offset */
  133. bits_per_pixel=ReadBlobMSBShort(image);
  134. if ((width == 0UL) || (height == 0UL) || ((bits_per_pixel != 8) &&
  135. (bits_per_pixel != 24)))
  136. ThrowReaderException(CorruptImageError,"ImproperImageHeader");
  137. do
  138. {
  139. /*
  140. Initialize image structure.
  141. */
  142. image->columns=width;
  143. image->rows=height;
  144. if (bits_per_pixel == 8)
  145. if (AcquireImageColormap(image,256,exception) == MagickFalse)
  146. ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
  147. if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
  148. if (image->scene >= (image_info->scene+image_info->number_scenes-1))
  149. break;
  150. status=SetImageExtent(image,image->columns,image->rows,exception);
  151. if (status == MagickFalse)
  152. return(DestroyImageList(image));
  153. /*
  154. Convert PIX raster image to pixel packets.
  155. */
  156. red=(Quantum) 0;
  157. green=(Quantum) 0;
  158. blue=(Quantum) 0;
  159. index=0;
  160. length=0;
  161. for (y=0; y < (ssize_t) image->rows; y++)
  162. {
  163. q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
  164. if (q == (Quantum *) NULL)
  165. break;
  166. for (x=0; x < (ssize_t) image->columns; x++)
  167. {
  168. if (length == 0)
  169. {
  170. length=(size_t) ReadBlobByte(image);
  171. if (bits_per_pixel == 8)
  172. index=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
  173. else
  174. {
  175. blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
  176. green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
  177. red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
  178. }
  179. }
  180. if (image->storage_class == PseudoClass)
  181. SetPixelIndex(image,index,q);
  182. SetPixelBlue(image,blue,q);
  183. SetPixelGreen(image,green,q);
  184. SetPixelRed(image,red,q);
  185. length--;
  186. q+=GetPixelChannels(image);
  187. }
  188. if (SyncAuthenticPixels(image,exception) == MagickFalse)
  189. break;
  190. if (image->previous == (Image *) NULL)
  191. {
  192. status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
  193. image->rows);
  194. if (status == MagickFalse)
  195. break;
  196. }
  197. }
  198. if (image->storage_class == PseudoClass)
  199. (void) SyncImage(image,exception);
  200. if (EOFBlob(image) != MagickFalse)
  201. {
  202. ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
  203. image->filename);
  204. break;
  205. }
  206. /*
  207. Proceed to next image.
  208. */
  209. if (image_info->number_scenes != 0)
  210. if (image->scene >= (image_info->scene+image_info->number_scenes-1))
  211. break;
  212. width=ReadBlobMSBLong(image);
  213. height=ReadBlobMSBLong(image);
  214. (void) ReadBlobMSBShort(image);
  215. (void) ReadBlobMSBShort(image);
  216. bits_per_pixel=ReadBlobMSBShort(image);
  217. status=(width != 0UL) && (height == 0UL) && ((bits_per_pixel == 8) ||
  218. (bits_per_pixel == 24)) ? MagickTrue : MagickFalse;
  219. if (status != MagickFalse)
  220. {
  221. /*
  222. Allocate next image structure.
  223. */
  224. AcquireNextImage(image_info,image,exception);
  225. if (GetNextImageInList(image) == (Image *) NULL)
  226. {
  227. image=DestroyImageList(image);
  228. return((Image *) NULL);
  229. }
  230. image=SyncNextImageInList(image);
  231. status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
  232. GetBlobSize(image));
  233. if (status == MagickFalse)
  234. break;
  235. }
  236. } while (status != MagickFalse);
  237. (void) CloseBlob(image);
  238. return(GetFirstImageInList(image));
  239. }
  240. /*
  241. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  242. % %
  243. % %
  244. % %
  245. % R e g i s t e r P I X I m a g e %
  246. % %
  247. % %
  248. % %
  249. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  250. %
  251. % RegisterPIXImage() adds attributes for the PIX image format to
  252. % the list of supported formats. The attributes include the image format
  253. % tag, a method to read and/or write the format, whether the format
  254. % supports the saving of more than one frame to the same file or blob,
  255. % whether the format supports native in-memory I/O, and a brief
  256. % description of the format.
  257. %
  258. % The format of the RegisterPIXImage method is:
  259. %
  260. % size_t RegisterPIXImage(void)
  261. %
  262. */
  263. ModuleExport size_t RegisterPIXImage(void)
  264. {
  265. MagickInfo
  266. *entry;
  267. entry=AcquireMagickInfo("PIX","PIX","Alias/Wavefront RLE image format");
  268. entry->decoder=(DecodeImageHandler *) ReadPIXImage;
  269. (void) RegisterMagickInfo(entry);
  270. return(MagickImageCoderSignature);
  271. }
  272. /*
  273. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  274. % %
  275. % %
  276. % %
  277. % U n r e g i s t e r P I X I m a g e %
  278. % %
  279. % %
  280. % %
  281. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  282. %
  283. % UnregisterPIXImage() removes format registrations made by the
  284. % PIX module from the list of supported formats.
  285. %
  286. % The format of the UnregisterPIXImage method is:
  287. %
  288. % UnregisterPIXImage(void)
  289. %
  290. */
  291. ModuleExport void UnregisterPIXImage(void)
  292. {
  293. (void) UnregisterMagickInfo("PIX");
  294. }