2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20 % Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/colormap.h"
47 #include "MagickCore/constitute.h"
48 #include "MagickCore/exception.h"
49 #include "MagickCore/exception-private.h"
50 #include "MagickCore/image-private.h"
51 #include "MagickCore/list.h"
52 #include "MagickCore/magick.h"
53 #include "MagickCore/memory_.h"
54 #include "MagickCore/monitor.h"
55 #include "MagickCore/monitor-private.h"
56 #include "MagickCore/pixel-accessor.h"
57 #include "MagickCore/quantum-private.h"
58 #include "MagickCore/static.h"
59 #include "MagickCore/string_.h"
60 #include "MagickCore/module.h"
61 #if defined(MAGICKCORE_DJVU_DELEGATE)
62 #include <libdjvu/ddjvuapi.h>
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 % IsDJVU() returns MagickTrue if the image format type, identified by the
77 % magick string, is DJVU.
79 % The format of the IsDJVU method is:
81 % MagickBooleanType IsDJVU(const unsigned char *magick,const size_t length)
83 % A description of each parameter follows:
85 % o magick: compare image format pattern against these bytes.
87 % o length: Specifies the length of the magick string.
90 static MagickBooleanType IsDJVU(const unsigned char *magick,const size_t length)
94 if (memcmp(magick,"AT&TFORM",8) == 0)
99 #if defined(MAGICKCORE_DJVU_DELEGATE)
101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 % R e a d D J V U I m a g e %
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 % ReadDJVUImage() reads DJVU image and returns it. It allocates the memory
112 % necessary for the new Image structure and returns a pointer to the new
113 % image or set of images.
115 % The format of the ReadDJVUImage method is:
117 % Image *ReadDJVUImage(const ImageInfo *image_info,
118 % ExceptionInfo *exception)
120 % A description of each parameter follows:
122 % o image_info: the image info.
124 % o exception: return any errors or warnings in this structure.
128 #if defined(__cplusplus) || defined(c_plusplus)
132 typedef struct _LoadContext
137 ddjvu_context_t* context;
138 ddjvu_document_t *document;
145 #define BLOCKSIZE 65536
148 pump_data(Image *image, LoadContext* lc)
150 int blocksize = BLOCKSIZE;
151 char data[BLOCKSIZE];
154 /* i might check for a condition! */
155 while ((size = (size_t) ReadBlob(image,(size_t) blocksize,data)) == blocksize) {
156 ddjvu_stream_write(lc->document, lc->streamid, data, size);
159 ddjvu_stream_write(lc->document, lc->streamid, data, size);
160 ddjvu_stream_close(lc->document, lc->streamid, 0);
164 /* returns NULL only after all is delivered! */
165 static ddjvu_message_t*
166 pump_data_until_message(LoadContext *lc,Image *image) /* ddjvu_context_t *context, type ddjvu_document_type_t */
168 size_t blocksize = BLOCKSIZE;
169 unsigned char data[BLOCKSIZE];
171 ddjvu_message_t *message;
173 /* i might check for a condition! */
175 while (!(message = ddjvu_message_peek(lc->context))
176 && (size = (size_t) ReadBlob(image,(size_t) blocksize,data)) == blocksize) {
177 ddjvu_stream_write(lc->document, lc->streamid, (char *) data, size);
182 ddjvu_stream_write(lc->document, lc->streamid, (char *) data, size);
183 ddjvu_stream_close(lc->document, lc->streamid, 0);
190 message_tag_name(ddjvu_message_tag_t tag)
192 static char* names[] =
205 if (tag <= DDJVU_PROGRESS)
214 /* write out nice info on the message,
215 * and store in *user* data the info on progress.
218 process_message(ddjvu_message_t *message)
222 ddjvu_context_t* context= message->m_any.context;
228 printf("*** %s: %s.\n",__FUNCTION__, message_tag_name(message->m_any.tag));
232 switch (message->m_any.tag){
235 ddjvu_document_t* document= message->m_any.document;
236 /* ddjvu_document_decoding_status is set by libdjvu! */
237 /* we have some info on the document */
238 LoadContext *lc = (LoadContext *) ddjvu_document_get_user_data(document);
239 lc->pages = ddjvu_document_get_pagenum(document);
241 printf("the doc has %d pages\n", ddjvu_document_get_pagenum(document));
247 printf("the name of the chunk is: %s\n", message->m_chunk.chunkid);
256 ddjvu_page_t* page = message->m_any.page;
257 page_info* info = ddjvu_page_get_user_data(page);
259 printf("page decoding status: %d %s%s%s\n",
260 ddjvu_page_decoding_status(page),
261 status_color, status_name(ddjvu_page_decoding_status(page)), color_reset);
263 printf("the page LAYOUT changed: width x height: %d x %d @ %d dpi. Version %d, type %d\n",
264 // printf("page info:\n width x height: %d x %d @ %d dpi, version %d, type %d\n",
265 ddjvu_page_get_width(page),
266 ddjvu_page_get_height(page),
267 ddjvu_page_get_resolution(page),
268 ddjvu_page_get_version(page),
269 /* DDJVU_PAGETYPE_BITONAL */
270 ddjvu_page_get_type(page));
277 case DDJVU_REDISPLAY:
281 ddjvu_page_t* page = message->m_any.page;
282 page_info* info = ddjvu_page_get_user_data(page);
284 printf("the page can/should be REDISPLAYED\n");
292 printf("PROGRESS:\n");
296 printf("simply ERROR!\n message:\t%s\nfunction:\t%s(file %s)\nlineno:\t%d\n",
297 message->m_error.message,
298 message->m_error.function,
299 message->m_error.filename,
300 message->m_error.lineno);
304 printf("INFO: %s!\n", message->m_info.message);
308 printf("unexpected\n");
310 return(message->m_any.tag);
314 #if defined(__cplusplus) || defined(c_plusplus)
322 * DjVu advertised readiness to provide bitmap: So get it!
323 * we use the RGB format!
326 get_page_image(LoadContext *lc, ddjvu_page_t *page, int x, int y, int w, int h, ExceptionInfo *exception ) {
346 rect.w = (unsigned int) w; /* /10 */
347 rect.h = (unsigned int) h; /* /10 */
350 type = ddjvu_page_get_type(lc->page);
352 /* stride of this temporary buffer: */
353 stride = (type == DDJVU_PAGETYPE_BITONAL)?
354 (image->columns + 7)/8 : image->columns *3;
356 q = (unsigned char *) AcquireQuantumMemory(image->rows,stride);
357 if (q == (unsigned char *) NULL)
360 format = ddjvu_format_create(
361 (type == DDJVU_PAGETYPE_BITONAL)?DDJVU_FORMAT_LSBTOMSB : DDJVU_FORMAT_RGB24,
362 /* DDJVU_FORMAT_RGB24
363 * DDJVU_FORMAT_RGBMASK32*/
364 /* DDJVU_FORMAT_RGBMASK32 */
368 /* fixme: ThrowReaderException is a macro, which uses `exception' variable */
372 /* ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); */
376 ddjvu_format_set_row_order(format, 1);
377 ddjvu_format_set_y_direction(format, 1);
379 ret = ddjvu_page_render(page,
380 DDJVU_RENDER_COLOR, /* ddjvu_render_mode_t */
387 ddjvu_format_release(format);
390 if (type == DDJVU_PAGETYPE_BITONAL) {
393 printf("%s: expanding BITONAL page/image\n", __FUNCTION__);
397 for (y=0; y < (ssize_t) image->rows; y++)
399 Quantum * o = QueueAuthenticPixels(image,0,y,image->columns,1,exception);
400 if (o == (Quantum *) NULL)
405 /* fixme: the non-aligned, last =<7 bits ! that's ok!!!*/
406 for (x= 0; x < (ssize_t) image->columns; x++)
408 if (bit == 0) byte= (size_t) q[(y * stride) + (x / 8)];
410 SetPixelIndex(image,(Quantum) (((byte & 0x01) != 0) ? 0x00 : 0x01),o);
415 o+=GetPixelChannels(image);
417 if (SyncAuthenticPixels(image,exception) == MagickFalse)
421 SyncImage(image,exception);
424 printf("%s: expanding PHOTO page/image\n", __FUNCTION__);
426 /* now transfer line-wise: */
436 for (i = 0;i< (ssize_t) image->rows; i++)
439 if (i % 1000 == 0) printf("%d\n",i);
441 r = QueueAuthenticPixels(image,0,i,image->columns,1,exception);
442 if (r == (Quantum *) NULL)
444 for (x=0; x < (ssize_t) image->columns; x++)
446 SetPixelRed(image,ScaleCharToQuantum(*s++),r);
447 SetPixelGreen(image,ScaleCharToQuantum(*s++),r);
448 SetPixelBlue(image,ScaleCharToQuantum(*s++),r);
449 r+=GetPixelChannels(image);
452 (void) SyncAuthenticPixels(image,exception);
455 q=(unsigned char *) RelinquishMagickMemory(q);
459 #if defined(MAGICKCORE_DJVU_DELEGATE)
463 get_page_line(LoadContext *lc, int row, QuantumInfo* quantum_info)
477 ddjvu_rect_t rect, pagerect;
480 rect.w = lc->image->columns; /* /10 */
481 rect.h = 1; /* /10 */
485 pagerect.w = lc->image->columns;
486 pagerect.h = lc->image->rows;
489 format = ddjvu_format_create(
497 ddjvu_format_set_row_order(format, 1);
498 ddjvu_format_set_y_direction(format, 1);
504 q = (unsigned char *) AcquireQuantumMemory(lc->image->columns,stride);
506 ret = ddjvu_page_render(lc->page,
507 DDJVU_RENDER_COLOR, /* ddjvu_render_mode_t */
511 pagerect.w * 3, /* ?? */
514 ImportQuantumPixels(lc->image,
522 ,q,&lc->image->exception);
523 q=(unsigned char *) RelinquishMagickMemory(q);
524 ddjvu_format_release(format);
531 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
535 % R e a d O n e D J V U I m a g e %
539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
541 % ReadOneDJVUImage() reads a Portable Network Graphics (DJVU) image file
542 % (minus the 8-byte signature) and returns it. It allocates the memory
543 % necessary for the new Image structure and returns a pointer to the new
546 % The format of the ReadOneDJVUImage method is:
548 % Image *ReadOneDJVUImage(MngInfo *mng_info, const ImageInfo *image_info,
549 % ExceptionInfo *exception)
551 % A description of each parameter follows:
553 % o mng_info: Specifies a pointer to a MngInfo structure.
555 % o image_info: the image info.
557 % o exception: return any errors or warnings in this structure.
561 static Image *ReadOneDJVUImage(LoadContext* lc,const int pagenum,
562 const ImageInfo *image_info,ExceptionInfo *exception)
567 ddjvu_pageinfo_t info;
568 ddjvu_message_t *message;
572 MagickBooleanType status;
574 /* so, we know that the page is there! Get its dimension, and */
576 /* Read one DJVU image */
579 /* register Quantum *q; */
581 logging=LogMagickEvent(CoderEvent,GetMagickModule(), " enter ReadOneDJVUImage()");
585 printf("==== Loading the page %d\n", pagenum);
587 lc->page = ddjvu_page_create_by_pageno(lc->document, pagenum); /* 0? */
589 /* pump data untill the page is ready for rendering. */
592 while ((message = ddjvu_message_peek(lc->context)))
594 tag=process_message(message);
596 ddjvu_message_pop(lc->context);
598 /* fixme: maybe exit? */
599 /* if (lc->error) break; */
601 message = pump_data_until_message(lc,image);
604 tag=process_message(message);
606 ddjvu_message_pop(lc->context);
607 } while ((message = ddjvu_message_peek(lc->context)));
609 } while (!ddjvu_page_decoding_done(lc->page));
611 ddjvu_document_get_pageinfo(lc->document, pagenum, &info);
613 image->resolution.x = (float) info.dpi;
614 image->resolution.y =(float) info.dpi;
615 if (image_info->density != (char *) NULL)
624 Set rendering resolution.
626 flags=ParseGeometry(image_info->density,&geometry_info);
627 image->resolution.x=geometry_info.rho;
628 image->resolution.y=geometry_info.sigma;
629 if ((flags & SigmaValue) == 0)
630 image->resolution.y=image->resolution.x;
631 info.width*=image->resolution.x/info.dpi;
632 info.height*=image->resolution.y/info.dpi;
633 info.dpi=(ssize_t) MagickMax(image->resolution.x,image->resolution.y);
635 type = ddjvu_page_get_type(lc->page);
637 /* double -> float! */
638 /* image->gamma = (float)ddjvu_page_get_gamma(lc->page); */
640 /* mmc: set image->depth */
641 /* mmc: This from the type */
643 image->columns=(size_t) info.width;
644 image->rows=(size_t) info.height;
646 /* mmc: bitonal should be palettized, and compressed! */
647 if (type == DDJVU_PAGETYPE_BITONAL){
648 image->colorspace = GRAYColorspace;
649 image->storage_class = PseudoClass;
650 image->depth = 8UL; /* i only support that? */
652 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
653 ThrowReaderException(ResourceLimitError,
654 "MemoryAllocationFailed");
656 image->colorspace = RGBColorspace;
657 image->storage_class = DirectClass;
658 /* fixme: MAGICKCORE_QUANTUM_DEPTH ?*/
659 image->depth = 8UL; /* i only support that? */
661 image->alpha_trait = BlendPixelTrait;
662 /* is this useful? */
664 status=SetImageExtent(image,image->columns,image->rows,exception);
665 if (status == MagickFalse)
666 return(DestroyImageList(image));
668 printf("now filling %.20g x %.20g\n",(double) image->columns,(double)
675 /* q = QueueAuthenticPixels(image,0,0,image->columns,image->rows); */
676 get_page_image(lc, lc->page, 0, 0, info.width, info.height, exception);
679 for (i = 0;i< image->rows; i++)
682 q = QueueAuthenticPixels(image,0,i,image->columns,1);
683 get_page_line(lc, i, quantum_info);
684 SyncAuthenticPixels(image);
687 #endif /* per_line */
691 printf("END: finished filling %.20g x %.20g\n",(double) image->columns,
692 (double) image->rows);
696 SyncImage(image,exception);
697 /* mmc: ??? Convert PNM pixels to runlength-encoded MIFF packets. */
698 /* image->colors = */
700 /* how is the line padding / stride? */
703 ddjvu_page_release(lc->page);
707 /* image->page.y=mng_info->y_off[mng_info->object_id]; */
709 image=DestroyImage(image);
711 /* end of reading one DJVU page/image */
716 if (AcquireImageColormap(image,2,exception) == MagickFalse)
717 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
719 Monochrome colormap. mmc: this the default!
721 image->colormap[0].red=QuantumRange;
722 image->colormap[0].green=QuantumRange;
723 image->colormap[0].blue=QuantumRange;
724 image->colormap[1].red=0;
725 image->colormap[1].green=0;
726 image->colormap[1].blue=0;
729 static void djvu_close_lc(LoadContext* lc)
732 ddjvu_document_release(lc->document);
734 ddjvu_context_release(lc->context);
736 ddjvu_page_release(lc->page);
737 RelinquishMagickMemory(lc);
740 static Image *ReadDJVUImage(const ImageInfo *image_info,
741 ExceptionInfo *exception)
769 assert(image_info != (const ImageInfo *) NULL);
770 assert(image_info->signature == MagickCoreSignature);
773 if (image_info->debug != MagickFalse)
774 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename);
776 assert(exception != (ExceptionInfo *) NULL);
777 assert(exception->signature == MagickCoreSignature);
780 logging = LogMagickEvent(CoderEvent,GetMagickModule(),"enter ReadDJVUImage()");
783 image = AcquireImage(image_info,exception); /* mmc: ?? */
786 lc = (LoadContext *) NULL;
787 status = OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
788 if (status == MagickFalse)
789 ThrowReaderException(FileOpenError,"UnableToOpenFile");
791 Verify DJVU signature.
794 count = ReadBlob(image,8,(unsigned char *) magic_number);
796 /* IsDJVU(const unsigned char *magick,const size_t length) */
797 if (memcmp(magic_number,"AT&TFORM",8) != 0)
798 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
803 * Allocate a LoadContext structure.
805 lc = (LoadContext *) AcquireMagickMemory(sizeof(*lc));
807 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
811 * Initialize members of the MngInfo structure.
813 (void) memset(lc,0,sizeof(LoadContext));
817 lc->context = ddjvu_context_create("ImageMagick djvu loader"); /* g_program_name */
819 ddjvu_cache_set_size(lc->context, 1); /* right? */
821 /* document: here we don't have a filename, but, for the sake of generality, a FILE* ! */
822 url="http://www.imagemagick.org/fake.djvu";
823 lc->document = ddjvu_document_create(lc->context, url, use_cache); /* don't cache */
824 ddjvu_document_set_user_data(lc->document, lc);
827 /* now we wait the message-request for data: */
828 message = ddjvu_message_wait(lc->context);
830 if (message->m_any.tag != DDJVU_NEWSTREAM) {
831 /* fixme: the djvu context, document! */
833 ddjvu_document_release(lc->document);
834 ddjvu_context_release(lc->context);
836 RelinquishMagickMemory(lc);
838 ThrowReaderException(ResourceLimitError,"Djvu initial message: unexpected type");
839 return NULL; /* error! */
842 lc->streamid = message->m_newstream.streamid;
843 ddjvu_message_pop(lc->context);
845 message = pump_data_until_message(lc,image);
846 /* now process the messages: */
850 process_message(message);
851 ddjvu_message_pop(lc->context);
852 } while ((message = ddjvu_message_peek(lc->context)));
854 /* fixme: i hope we have not read any messages pertinent(?) related to the page itself! */
856 while (lc->pages == 0) {
857 message = ddjvu_message_wait(lc->context);
858 process_message(message);
859 ddjvu_message_pop(lc->context);
862 images=NewImageList();
864 if (image_info->number_scenes != 0)
866 for ( ; i < (ssize_t) lc->pages; i++)
868 image=ReadOneDJVUImage(lc,i,image_info,exception);
869 if (image == (Image *) NULL)
872 AppendImageToList(&images,CloneImageList(image,exception));
873 images->extent=GetBlobSize(image);
874 if (image_info->number_scenes != 0)
875 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
879 if (images != (Image *) NULL)
880 (void) CloseBlob(images);
881 if (image != (Image *) NULL)
882 image=DestroyImageList(image);
885 if ((image->page.width == 0) && (image->page.height == 0))
887 image->page.width = image->columns+image->page.x;
888 image->page.height = image->rows+image->page.y;
890 if (image->columns == 0 || image->rows == 0)
892 if (logging != MagickFalse)
893 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
894 "exit ReadDJVUImage() with error.");
895 ThrowReaderException(CorruptImageError,"CorruptImage");
898 if (logging != MagickFalse)
899 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadDJVUImage()");
903 return(GetFirstImageInList(images));
908 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
912 % R e g i s t e r D J V U I m a g e %
916 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
918 % RegisterDJVUImage() adds attributes for the DJVU image format to
919 % the list of supported formats. The attributes include the image format
920 % tag, a method to read and/or write the format, whether the format
921 % supports the saving of more than one frame to the same file or blob,
922 % whether the format supports native in-memory I/O, and a brief
923 % description of the format.
925 % The format of the RegisterDJVUImage method is:
927 % size_t RegisterDJVUImage(void)
930 ModuleExport size_t RegisterDJVUImage(void)
933 version[MagickPathExtent];
941 "See http://www.djvuzone.org/ for details about the DJVU format. The\n"
942 "DJVU 1.2 specification is available there and at\n"
943 "ftp://swrinde.nde.swri.edu/pub/djvu/documents/."
947 #if defined(DJVU_LIBDJVU_VER_STRING)
948 (void) ConcatenateMagickString(version,"libdjvu ",MagickPathExtent);
949 (void) ConcatenateMagickString(version,DJVU_LIBDJVU_VER_STRING,MagickPathExtent);
951 entry=AcquireMagickInfo("DJVU","DJVU","Deja vu");
952 #if defined(MAGICKCORE_DJVU_DELEGATE)
953 entry->decoder=(DecodeImageHandler *) ReadDJVUImage;
955 entry->magick=(IsImageFormatHandler *) IsDJVU;
956 entry->flags|=CoderRawSupportFlag;
957 entry->flags^=CoderAdjoinFlag;
958 if (*version != '\0')
959 entry->version=AcquireString(version);
960 entry->note=AcquireString(DJVUNote);
961 (void) RegisterMagickInfo(entry);
962 return(MagickImageCoderSignature);
966 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
970 % U n r e g i s t e r D J V U I m a g e %
974 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
976 % UnregisterDJVUImage() removes format registrations made by the
977 % DJVU module from the list of supported formats.
979 % The format of the UnregisterDJVUImage method is:
981 % UnregisterDJVUImage(void)
984 ModuleExport void UnregisterDJVUImage(void)
986 (void) UnregisterMagickInfo("DJVU");