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

/branches/ImageMagick-6.6.4/coders/avs.c

https://github.com/trevor/ImageMagick
C | 399 lines | 222 code | 16 blank | 161 comment | 79 complexity | 696eaddc232baeabe49ff37c4c4af2dd MD5 | raw file
  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. % %
  4. % %
  5. % %
  6. % AAA V V SSSSS %
  7. % A A V V SS %
  8. % AAAAA V V SSS %
  9. % A A V V SS %
  10. % A A V SSSSS %
  11. % %
  12. % %
  13. % Read/Write AVS X Image Format %
  14. % %
  15. % Software Design %
  16. % John Cristy %
  17. % July 1992 %
  18. % %
  19. % %
  20. % Copyright 1999-2010 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/colorspace.h"
  46. #include "magick/exception.h"
  47. #include "magick/exception-private.h"
  48. #include "magick/image.h"
  49. #include "magick/image-private.h"
  50. #include "magick/list.h"
  51. #include "magick/magick.h"
  52. #include "magick/memory_.h"
  53. #include "magick/monitor.h"
  54. #include "magick/monitor-private.h"
  55. #include "magick/quantum-private.h"
  56. #include "magick/static.h"
  57. #include "magick/string_.h"
  58. #include "magick/module.h"
  59. /*
  60. Forward declarations.
  61. */
  62. static MagickBooleanType
  63. WriteAVSImage(const ImageInfo *,Image *);
  64. /*
  65. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  66. % %
  67. % %
  68. % %
  69. % R e a d A V S I m a g e %
  70. % %
  71. % %
  72. % %
  73. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  74. %
  75. % ReadAVSImage() reads an AVS X image file and returns it. It
  76. % allocates the memory necessary for the new Image structure and returns a
  77. % pointer to the new image.
  78. %
  79. % The format of the ReadAVSImage method is:
  80. %
  81. % Image *ReadAVSImage(const ImageInfo *image_info,ExceptionInfo *exception)
  82. %
  83. % A description of each parameter follows:
  84. %
  85. % o image_info: the image info.
  86. %
  87. % o exception: return any errors or warnings in this structure.
  88. %
  89. */
  90. static Image *ReadAVSImage(const ImageInfo *image_info,ExceptionInfo *exception)
  91. {
  92. Image
  93. *image;
  94. MagickBooleanType
  95. status;
  96. register ssize_t
  97. x;
  98. register PixelPacket
  99. *q;
  100. register unsigned char
  101. *p;
  102. size_t
  103. height,
  104. length,
  105. width;
  106. ssize_t
  107. count,
  108. y;
  109. unsigned char
  110. *pixels;
  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. status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
  123. if (status == MagickFalse)
  124. {
  125. image=DestroyImageList(image);
  126. return((Image *) NULL);
  127. }
  128. /*
  129. Read AVS X image.
  130. */
  131. width=ReadBlobMSBLong(image);
  132. height=ReadBlobMSBLong(image);
  133. if (EOFBlob(image) != MagickFalse)
  134. ThrowReaderException(CorruptImageError,"ImproperImageHeader");
  135. if ((width == 0UL) || (height == 0UL))
  136. ThrowReaderException(CorruptImageError,"ImproperImageHeader");
  137. do
  138. {
  139. /*
  140. Convert AVS raster image to pixel packets.
  141. */
  142. image->columns=width;
  143. image->rows=height;
  144. image->depth=8;
  145. if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
  146. if (image->scene >= (image_info->scene+image_info->number_scenes-1))
  147. break;
  148. pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
  149. 4*sizeof(*pixels));
  150. if (pixels == (unsigned char *) NULL)
  151. ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
  152. length=(size_t) 4*image->columns;
  153. for (y=0; y < (ssize_t) image->rows; y++)
  154. {
  155. count=ReadBlob(image,length,pixels);
  156. if ((size_t) count != length)
  157. ThrowReaderException(CorruptImageError,"UnableToReadImageData");
  158. p=pixels;
  159. q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
  160. if (q == (PixelPacket *) NULL)
  161. break;
  162. for (x=0; x < (ssize_t) image->columns; x++)
  163. {
  164. q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(*p++));
  165. q->red=ScaleCharToQuantum(*p++);
  166. q->green=ScaleCharToQuantum(*p++);
  167. q->blue=ScaleCharToQuantum(*p++);
  168. if (q->opacity != OpaqueOpacity)
  169. image->matte=MagickTrue;
  170. q++;
  171. }
  172. if (SyncAuthenticPixels(image,exception) == MagickFalse)
  173. break;
  174. if ((image->previous == (Image *) NULL) &&
  175. (SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,image->rows) == MagickFalse))
  176. break;
  177. }
  178. pixels=(unsigned char *) RelinquishMagickMemory(pixels);
  179. if (EOFBlob(image) != MagickFalse)
  180. {
  181. ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
  182. image->filename);
  183. break;
  184. }
  185. /*
  186. Proceed to next image.
  187. */
  188. if (image_info->number_scenes != 0)
  189. if (image->scene >= (image_info->scene+image_info->number_scenes-1))
  190. break;
  191. width=ReadBlobMSBLong(image);
  192. height=ReadBlobMSBLong(image);
  193. if ((width != 0UL) && (height != 0UL))
  194. {
  195. /*
  196. Allocate next image structure.
  197. */
  198. AcquireNextImage(image_info,image);
  199. if (GetNextImageInList(image) == (Image *) NULL)
  200. {
  201. image=DestroyImageList(image);
  202. return((Image *) NULL);
  203. }
  204. image=SyncNextImageInList(image);
  205. status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
  206. GetBlobSize(image));
  207. if (status == MagickFalse)
  208. break;
  209. }
  210. } while ((width != 0UL) && (height != 0UL));
  211. (void) CloseBlob(image);
  212. return(GetFirstImageInList(image));
  213. }
  214. /*
  215. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  216. % %
  217. % %
  218. % %
  219. % R e g i s t e r A V S I m a g e %
  220. % %
  221. % %
  222. % %
  223. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  224. %
  225. % RegisterAVSImage() adds attributes for the AVS X image format to the list
  226. % of supported formats. The attributes include the image format tag, a
  227. % method to read and/or write the format, whether the format supports the
  228. % saving of more than one frame to the same file or blob, whether the format
  229. % supports native in-memory I/O, and a brief description of the format.
  230. %
  231. % The format of the RegisterAVSImage method is:
  232. %
  233. % size_t RegisterAVSImage(void)
  234. %
  235. */
  236. ModuleExport size_t RegisterAVSImage(void)
  237. {
  238. MagickInfo
  239. *entry;
  240. entry=SetMagickInfo("AVS");
  241. entry->decoder=(DecodeImageHandler *) ReadAVSImage;
  242. entry->encoder=(EncodeImageHandler *) WriteAVSImage;
  243. entry->description=ConstantString("AVS X image");
  244. entry->module=ConstantString("AVS");
  245. (void) RegisterMagickInfo(entry);
  246. return(MagickImageCoderSignature);
  247. }
  248. /*
  249. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  250. % %
  251. % %
  252. % %
  253. % U n r e g i s t e r A V S I m a g e %
  254. % %
  255. % %
  256. % %
  257. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  258. %
  259. % UnregisterAVSImage() removes format registrations made by the
  260. % AVS module from the list of supported formats.
  261. %
  262. % The format of the UnregisterAVSImage method is:
  263. %
  264. % UnregisterAVSImage(void)
  265. %
  266. */
  267. ModuleExport void UnregisterAVSImage(void)
  268. {
  269. (void) UnregisterMagickInfo("AVS");
  270. }
  271. /*
  272. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  273. % %
  274. % %
  275. % %
  276. % W r i t e A V S I m a g e %
  277. % %
  278. % %
  279. % %
  280. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  281. %
  282. % WriteAVSImage() writes an image to a file in AVS X image format.
  283. %
  284. % The format of the WriteAVSImage method is:
  285. %
  286. % MagickBooleanType WriteAVSImage(const ImageInfo *image_info,Image *image)
  287. %
  288. % A description of each parameter follows.
  289. %
  290. % o image_info: the image info.
  291. %
  292. % o image: The image.
  293. %
  294. */
  295. static MagickBooleanType WriteAVSImage(const ImageInfo *image_info,Image *image)
  296. {
  297. MagickBooleanType
  298. status;
  299. MagickOffsetType
  300. scene;
  301. register const PixelPacket
  302. *restrict p;
  303. register ssize_t
  304. x;
  305. register unsigned char
  306. *restrict q;
  307. ssize_t
  308. count,
  309. y;
  310. unsigned char
  311. *pixels;
  312. /*
  313. Open output image file.
  314. */
  315. assert(image_info != (const ImageInfo *) NULL);
  316. assert(image_info->signature == MagickSignature);
  317. assert(image != (Image *) NULL);
  318. assert(image->signature == MagickSignature);
  319. if (image->debug != MagickFalse)
  320. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  321. status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
  322. if (status == MagickFalse)
  323. return(status);
  324. scene=0;
  325. do
  326. {
  327. /*
  328. Write AVS header.
  329. */
  330. if (image->colorspace != RGBColorspace)
  331. (void) TransformImageColorspace(image,RGBColorspace);
  332. (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
  333. (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
  334. /*
  335. Allocate memory for pixels.
  336. */
  337. pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
  338. 4*sizeof(*pixels));
  339. if (pixels == (unsigned char *) NULL)
  340. ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
  341. /*
  342. Convert MIFF to AVS raster pixels.
  343. */
  344. for (y=0; y < (ssize_t) image->rows; y++)
  345. {
  346. p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
  347. if (p == (PixelPacket *) NULL)
  348. break;
  349. q=pixels;
  350. for (x=0; x < (ssize_t) image->columns; x++)
  351. {
  352. *q++=ScaleQuantumToChar((Quantum) (QuantumRange-
  353. (image->matte != MagickFalse ? p->opacity : OpaqueOpacity)));
  354. *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
  355. *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
  356. *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
  357. p++;
  358. }
  359. count=WriteBlob(image,(size_t) (q-pixels),pixels);
  360. if (count != (ssize_t) (q-pixels))
  361. break;
  362. if ((image->previous == (Image *) NULL) &&
  363. (SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,image->rows) == MagickFalse))
  364. break;
  365. }
  366. pixels=(unsigned char *) RelinquishMagickMemory(pixels);
  367. if (GetNextImageInList(image) == (Image *) NULL)
  368. break;
  369. image=SyncNextImageInList(image);
  370. status=SetImageProgress(image,SaveImagesTag,scene++,
  371. GetImageListLength(image));
  372. if (status == MagickFalse)
  373. break;
  374. } while (image_info->adjoin != MagickFalse);
  375. (void) CloseBlob(image);
  376. return(MagickTrue);
  377. }