PageRenderTime 25ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/Libs/ImageMagick-6.7.4-Windows/coders/otb.c

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