PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/ImageMagick-6.7.0/coders/mono.c

https://github.com/trevor/ImageMagick
C | 355 lines | 190 code | 13 blank | 152 comment | 60 complexity | 5fd2d4946dbb526a95eef968dbdd1497 MD5 | raw file
  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. % %
  4. % %
  5. % %
  6. % M M OOO N N OOO %
  7. % MM MM O O NN N O O %
  8. % M M M O O N N N O O %
  9. % M M O O N NN O O %
  10. % M M OOO N N OOO %
  11. % %
  12. % %
  13. % Read/Write Raw Bi-Level Bitmap Format %
  14. % %
  15. % Software Design %
  16. % John Cristy %
  17. % July 1992 %
  18. % %
  19. % %
  20. % Copyright 1999-2011 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 "magick/studio.h"
  42. #include "magick/blob.h"
  43. #include "magick/blob-private.h"
  44. #include "magick/cache.h"
  45. #include "magick/color-private.h"
  46. #include "magick/colormap.h"
  47. #include "magick/colorspace.h"
  48. #include "magick/exception.h"
  49. #include "magick/exception-private.h"
  50. #include "magick/image.h"
  51. #include "magick/image-private.h"
  52. #include "magick/list.h"
  53. #include "magick/magick.h"
  54. #include "magick/memory_.h"
  55. #include "magick/monitor.h"
  56. #include "magick/monitor-private.h"
  57. #include "magick/quantum-private.h"
  58. #include "magick/static.h"
  59. #include "magick/string_.h"
  60. #include "magick/module.h"
  61. /*
  62. Forward declarations.
  63. */
  64. static MagickBooleanType
  65. WriteMONOImage(const ImageInfo *,Image *);
  66. /*
  67. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  68. % %
  69. % %
  70. % %
  71. % R e a d M O N O I m a g e %
  72. % %
  73. % %
  74. % %
  75. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  76. %
  77. % ReadMONOImage() reads an image of raw bites in LSB order and returns
  78. % it. It allocates the memory necessary for the new Image structure and
  79. % returns a pointer to the new image.
  80. %
  81. % The format of the ReadMONOImage method is:
  82. %
  83. % Image *ReadMONOImage(const ImageInfo *image_info,
  84. % ExceptionInfo *exception)
  85. %
  86. % A description of each parameter follows:
  87. %
  88. % o image_info: the image info.
  89. %
  90. % o exception: return any errors or warnings in this structure.
  91. %
  92. */
  93. static Image *ReadMONOImage(const ImageInfo *image_info,
  94. ExceptionInfo *exception)
  95. {
  96. Image
  97. *image;
  98. MagickBooleanType
  99. status;
  100. register IndexPacket
  101. *indexes;
  102. register PixelPacket
  103. *q;
  104. register ssize_t
  105. x;
  106. size_t
  107. bit,
  108. byte;
  109. ssize_t
  110. y;
  111. /*
  112. Open image file.
  113. */
  114. assert(image_info != (const ImageInfo *) NULL);
  115. assert(image_info->signature == MagickSignature);
  116. if (image_info->debug != MagickFalse)
  117. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
  118. image_info->filename);
  119. assert(exception != (ExceptionInfo *) NULL);
  120. assert(exception->signature == MagickSignature);
  121. image=AcquireImage(image_info);
  122. if ((image->columns == 0) || (image->rows == 0))
  123. ThrowReaderException(OptionError,"MustSpecifyImageSize");
  124. status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
  125. if (status == MagickFalse)
  126. {
  127. image=DestroyImageList(image);
  128. return((Image *) NULL);
  129. }
  130. if (DiscardBlobBytes(image,image->offset) == MagickFalse)
  131. ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
  132. image->filename);
  133. /*
  134. Initialize image colormap.
  135. */
  136. image->depth=1;
  137. if (AcquireImageColormap(image,2) == MagickFalse)
  138. ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
  139. if (image_info->ping != MagickFalse)
  140. {
  141. (void) CloseBlob(image);
  142. return(GetFirstImageInList(image));
  143. }
  144. /*
  145. Convert bi-level image to pixel packets.
  146. */
  147. for (y=0; y < (ssize_t) image->rows; y++)
  148. {
  149. q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
  150. if (q == (PixelPacket *) NULL)
  151. break;
  152. indexes=GetAuthenticIndexQueue(image);
  153. bit=0;
  154. byte=0;
  155. for (x=0; x < (ssize_t) image->columns; x++)
  156. {
  157. if (bit == 0)
  158. byte=(size_t) ReadBlobByte(image);
  159. if (image_info->endian == LSBEndian)
  160. SetPixelIndex(indexes+x,((byte & 0x01) != 0) ? 0x00 : 0x01);
  161. else
  162. SetPixelIndex(indexes+x,((byte & 0x01) != 0) ? 0x01 : 0x00);
  163. bit++;
  164. if (bit == 8)
  165. bit=0;
  166. byte>>=1;
  167. }
  168. if (SyncAuthenticPixels(image,exception) == MagickFalse)
  169. break;
  170. status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
  171. image->rows);
  172. if (status == MagickFalse)
  173. break;
  174. }
  175. (void) SyncImage(image);
  176. if (EOFBlob(image) != MagickFalse)
  177. ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
  178. image->filename);
  179. (void) CloseBlob(image);
  180. return(GetFirstImageInList(image));
  181. }
  182. /*
  183. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  184. % %
  185. % %
  186. % %
  187. % R e g i s t e r M O N O I m a g e %
  188. % %
  189. % %
  190. % %
  191. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  192. %
  193. % RegisterMONOImage() adds attributes for the MONO image format to
  194. % the list of supported formats. The attributes include the image format
  195. % tag, a method to read and/or write the format, whether the format
  196. % supports the saving of more than one frame to the same file or blob,
  197. % whether the format supports native in-memory I/O, and a brief
  198. % description of the format.
  199. %
  200. % The format of the RegisterMONOImage method is:
  201. %
  202. % size_t RegisterMONOImage(void)
  203. %
  204. */
  205. ModuleExport size_t RegisterMONOImage(void)
  206. {
  207. MagickInfo
  208. *entry;
  209. entry=SetMagickInfo("MONO");
  210. entry->decoder=(DecodeImageHandler *) ReadMONOImage;
  211. entry->encoder=(EncodeImageHandler *) WriteMONOImage;
  212. entry->raw=MagickTrue;
  213. entry->endian_support=MagickTrue;
  214. entry->adjoin=MagickFalse;
  215. entry->description=ConstantString("Raw bi-level bitmap");
  216. entry->module=ConstantString("MONO");
  217. (void) RegisterMagickInfo(entry);
  218. return(MagickImageCoderSignature);
  219. }
  220. /*
  221. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  222. % %
  223. % %
  224. % %
  225. % U n r e g i s t e r M O N O I m a g e %
  226. % %
  227. % %
  228. % %
  229. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  230. %
  231. % UnregisterMONOImage() removes format registrations made by the
  232. % MONO module from the list of supported formats.
  233. %
  234. % The format of the UnregisterMONOImage method is:
  235. %
  236. % UnregisterMONOImage(void)
  237. %
  238. */
  239. ModuleExport void UnregisterMONOImage(void)
  240. {
  241. (void) UnregisterMagickInfo("MONO");
  242. }
  243. /*
  244. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  245. % %
  246. % %
  247. % %
  248. % W r i t e M O N O I m a g e %
  249. % %
  250. % %
  251. % %
  252. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  253. %
  254. % WriteMONOImage() writes an image of raw bits in LSB order to a file.
  255. %
  256. % The format of the WriteMONOImage method is:
  257. %
  258. % MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
  259. % Image *image)
  260. %
  261. % A description of each parameter follows.
  262. %
  263. % o image_info: the image info.
  264. %
  265. % o image: The image.
  266. %
  267. */
  268. static MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
  269. Image *image)
  270. {
  271. MagickBooleanType
  272. status;
  273. register const PixelPacket
  274. *p;
  275. register ssize_t
  276. x;
  277. size_t
  278. bit,
  279. byte;
  280. ssize_t
  281. y;
  282. /*
  283. Open output image file.
  284. */
  285. assert(image_info != (const ImageInfo *) NULL);
  286. assert(image_info->signature == MagickSignature);
  287. assert(image != (Image *) NULL);
  288. assert(image->signature == MagickSignature);
  289. if (image->debug != MagickFalse)
  290. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  291. status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
  292. if (status == MagickFalse)
  293. return(status);
  294. if (image->colorspace != RGBColorspace)
  295. (void) TransformImageColorspace(image,RGBColorspace);
  296. /*
  297. Convert image to a bi-level image.
  298. */
  299. (void) SetImageType(image,BilevelType);
  300. for (y=0; y < (ssize_t) image->rows; y++)
  301. {
  302. p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
  303. if (p == (const PixelPacket *) NULL)
  304. break;
  305. bit=0;
  306. byte=0;
  307. for (x=0; x < (ssize_t) image->columns; x++)
  308. {
  309. byte>>=1;
  310. if (image->endian == LSBEndian)
  311. {
  312. if (PixelIntensity(p) < ((Quantum) QuantumRange/2.0))
  313. byte|=0x80;
  314. }
  315. else
  316. if (PixelIntensity(p) >= ((Quantum) QuantumRange/2.0))
  317. byte|=0x80;
  318. bit++;
  319. if (bit == 8)
  320. {
  321. (void) WriteBlobByte(image,(unsigned char) byte);
  322. bit=0;
  323. byte=0;
  324. }
  325. p++;
  326. }
  327. if (bit != 0)
  328. (void) WriteBlobByte(image,(unsigned char) (byte >> (8-bit)));
  329. status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
  330. image->rows);
  331. if (status == MagickFalse)
  332. break;
  333. }
  334. (void) CloseBlob(image);
  335. return(MagickTrue);
  336. }