Leptonica  1.77.0
Image processing and image analysis suite
readfile.c
Go to the documentation of this file.
1 /*====================================================================*
2  - Copyright (C) 2001 Leptonica. All rights reserved.
3  -
4  - Redistribution and use in source and binary forms, with or without
5  - modification, are permitted provided that the following conditions
6  - are met:
7  - 1. Redistributions of source code must retain the above copyright
8  - notice, this list of conditions and the following disclaimer.
9  - 2. Redistributions in binary form must reproduce the above
10  - copyright notice, this list of conditions and the following
11  - disclaimer in the documentation and/or other materials
12  - provided with the distribution.
13  -
14  - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18  - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *====================================================================*/
26 
74 #ifdef HAVE_CONFIG_H
75 #include "config_auto.h"
76 #endif /* HAVE_CONFIG_H */
77 
78 #include <string.h>
79 #include "allheaders.h"
80 
81  /* Output files for ioFormatTest(). */
82 static const char *FILE_BMP = "/tmp/lept/format/file.bmp";
83 static const char *FILE_PNG = "/tmp/lept/format/file.png";
84 static const char *FILE_PNM = "/tmp/lept/format/file.pnm";
85 static const char *FILE_G3 = "/tmp/lept/format/file_g3.tif";
86 static const char *FILE_G4 = "/tmp/lept/format/file_g4.tif";
87 static const char *FILE_RLE = "/tmp/lept/format/file_rle.tif";
88 static const char *FILE_PB = "/tmp/lept/format/file_packbits.tif";
89 static const char *FILE_LZW = "/tmp/lept/format/file_lzw.tif";
90 static const char *FILE_ZIP = "/tmp/lept/format/file_zip.tif";
91 static const char *FILE_TIFF_JPEG = "/tmp/lept/format/file_jpeg.tif";
92 static const char *FILE_TIFF = "/tmp/lept/format/file.tif";
93 static const char *FILE_JPG = "/tmp/lept/format/file.jpg";
94 static const char *FILE_GIF = "/tmp/lept/format/file.gif";
95 static const char *FILE_WEBP = "/tmp/lept/format/file.webp";
96 static const char *FILE_JP2K = "/tmp/lept/format/file.jp2";
97 
98 static const unsigned char JP2K_CODESTREAM[4] = { 0xff, 0x4f, 0xff, 0x51 };
99 static const unsigned char JP2K_IMAGE_DATA[12] = { 0x00, 0x00, 0x00, 0x0C,
100  0x6A, 0x50, 0x20, 0x20,
101  0x0D, 0x0A, 0x87, 0x0A };
102 
103 
104 /*---------------------------------------------------------------------*
105  * Top-level functions for reading images from file *
106  *---------------------------------------------------------------------*/
123 PIXA *
124 pixaReadFiles(const char *dirname,
125  const char *substr)
126 {
127 PIXA *pixa;
128 SARRAY *sa;
129 
130  PROCNAME("pixaReadFiles");
131 
132  if (!dirname)
133  return (PIXA *)ERROR_PTR("dirname not defined", procName, NULL);
134 
135  if ((sa = getSortedPathnamesInDirectory(dirname, substr, 0, 0)) == NULL)
136  return (PIXA *)ERROR_PTR("sa not made", procName, NULL);
137 
138  pixa = pixaReadFilesSA(sa);
139  sarrayDestroy(&sa);
140  return pixa;
141 }
142 
143 
150 PIXA *
152 {
153 char *str;
154 l_int32 i, n;
155 PIX *pix;
156 PIXA *pixa;
157 
158  PROCNAME("pixaReadFilesSA");
159 
160  if (!sa)
161  return (PIXA *)ERROR_PTR("sa not defined", procName, NULL);
162 
163  n = sarrayGetCount(sa);
164  pixa = pixaCreate(n);
165  for (i = 0; i < n; i++) {
166  str = sarrayGetString(sa, i, L_NOCOPY);
167  if ((pix = pixRead(str)) == NULL) {
168  L_WARNING("pix not read from file %s\n", procName, str);
169  continue;
170  }
171  pixaAddPix(pixa, pix, L_INSERT);
172  }
173 
174  return pixa;
175 }
176 
177 
189 PIX *
190 pixRead(const char *filename)
191 {
192 FILE *fp;
193 PIX *pix;
194 
195  PROCNAME("pixRead");
196 
197  if (!filename)
198  return (PIX *)ERROR_PTR("filename not defined", procName, NULL);
199 
200  if ((fp = fopenReadStream(filename)) == NULL) {
201  L_ERROR("image file not found: %s\n", procName, filename);
202  return NULL;
203  }
204  pix = pixReadStream(fp, 0);
205  fclose(fp);
206  if (!pix)
207  return (PIX *)ERROR_PTR("pix not read", procName, NULL);
208  return pix;
209 }
210 
211 
225 PIX *
226 pixReadWithHint(const char *filename,
227  l_int32 hint)
228 {
229 FILE *fp;
230 PIX *pix;
231 
232  PROCNAME("pixReadWithHint");
233 
234  if (!filename)
235  return (PIX *)ERROR_PTR("filename not defined", procName, NULL);
236 
237  if ((fp = fopenReadStream(filename)) == NULL)
238  return (PIX *)ERROR_PTR("image file not found", procName, NULL);
239  pix = pixReadStream(fp, hint);
240  fclose(fp);
241 
242  if (!pix)
243  return (PIX *)ERROR_PTR("image not returned", procName, NULL);
244  return pix;
245 }
246 
247 
276 PIX *
278  l_int32 index)
279 {
280 char *fname;
281 l_int32 n;
282 PIX *pix;
283 
284  PROCNAME("pixReadIndexed");
285 
286  if (!sa)
287  return (PIX *)ERROR_PTR("sa not defined", procName, NULL);
288  n = sarrayGetCount(sa);
289  if (index < 0 || index >= n)
290  return (PIX *)ERROR_PTR("index out of bounds", procName, NULL);
291 
292  fname = sarrayGetString(sa, index, L_NOCOPY);
293  if (fname[0] == '\0')
294  return NULL;
295 
296  if ((pix = pixRead(fname)) == NULL) {
297  L_ERROR("pix not read from file %s\n", procName, fname);
298  return NULL;
299  }
300 
301  return pix;
302 }
303 
304 
317 PIX *
318 pixReadStream(FILE *fp,
319  l_int32 hint)
320 {
321 l_int32 format, ret, valid;
322 l_uint8 *comment;
323 PIX *pix;
324 PIXCMAP *cmap;
325 
326  PROCNAME("pixReadStream");
327 
328  if (!fp)
329  return (PIX *)ERROR_PTR("stream not defined", procName, NULL);
330  pix = NULL;
331 
332  findFileFormatStream(fp, &format);
333  switch (format)
334  {
335  case IFF_BMP:
336  if ((pix = pixReadStreamBmp(fp)) == NULL )
337  return (PIX *)ERROR_PTR( "bmp: no pix returned", procName, NULL);
338  break;
339 
340  case IFF_JFIF_JPEG:
341  if ((pix = pixReadStreamJpeg(fp, 0, 1, NULL, hint)) == NULL)
342  return (PIX *)ERROR_PTR( "jpeg: no pix returned", procName, NULL);
343  ret = fgetJpegComment(fp, &comment);
344  if (!ret && comment)
345  pixSetText(pix, (char *)comment);
346  LEPT_FREE(comment);
347  break;
348 
349  case IFF_PNG:
350  if ((pix = pixReadStreamPng(fp)) == NULL)
351  return (PIX *)ERROR_PTR("png: no pix returned", procName, NULL);
352  break;
353 
354  case IFF_TIFF:
355  case IFF_TIFF_PACKBITS:
356  case IFF_TIFF_RLE:
357  case IFF_TIFF_G3:
358  case IFF_TIFF_G4:
359  case IFF_TIFF_LZW:
360  case IFF_TIFF_ZIP:
361  case IFF_TIFF_JPEG:
362  if ((pix = pixReadStreamTiff(fp, 0)) == NULL) /* page 0 by default */
363  return (PIX *)ERROR_PTR("tiff: no pix returned", procName, NULL);
364  break;
365 
366  case IFF_PNM:
367  if ((pix = pixReadStreamPnm(fp)) == NULL)
368  return (PIX *)ERROR_PTR("pnm: no pix returned", procName, NULL);
369  break;
370 
371  case IFF_GIF:
372  if ((pix = pixReadStreamGif(fp)) == NULL)
373  return (PIX *)ERROR_PTR("gif: no pix returned", procName, NULL);
374  break;
375 
376  case IFF_JP2:
377  if ((pix = pixReadStreamJp2k(fp, 1, NULL, 0, 0)) == NULL)
378  return (PIX *)ERROR_PTR("jp2: no pix returned", procName, NULL);
379  break;
380 
381  case IFF_WEBP:
382  if ((pix = pixReadStreamWebP(fp)) == NULL)
383  return (PIX *)ERROR_PTR("webp: no pix returned", procName, NULL);
384  break;
385 
386  case IFF_PS:
387  L_ERROR("PostScript reading is not supported\n", procName);
388  return NULL;
389 
390  case IFF_LPDF:
391  L_ERROR("Pdf reading is not supported\n", procName);
392  return NULL;
393 
394  case IFF_SPIX:
395  if ((pix = pixReadStreamSpix(fp)) == NULL)
396  return (PIX *)ERROR_PTR("spix: no pix returned", procName, NULL);
397  break;
398 
399  case IFF_UNKNOWN:
400  return (PIX *)ERROR_PTR( "Unknown format: no pix returned",
401  procName, NULL);
402  break;
403  }
404 
405  if (pix) {
406  pixSetInputFormat(pix, format);
407  if ((cmap = pixGetColormap(pix))) {
408  pixcmapIsValid(cmap, &valid);
409  if (!valid) {
410  pixDestroy(&pix);
411  return (PIX *)ERROR_PTR("invalid colormap", procName, NULL);
412  }
413  }
414  }
415  return pix;
416 }
417 
418 
419 
420 /*---------------------------------------------------------------------*
421  * Read header information from file *
422  *---------------------------------------------------------------------*/
441 l_ok
442 pixReadHeader(const char *filename,
443  l_int32 *pformat,
444  l_int32 *pw,
445  l_int32 *ph,
446  l_int32 *pbps,
447  l_int32 *pspp,
448  l_int32 *piscmap)
449 {
450 l_int32 format, ret, w, h, d, bps, spp, iscmap;
451 l_int32 type; /* ignored */
452 FILE *fp;
453 PIX *pix;
454 
455  PROCNAME("pixReadHeader");
456 
457  if (pw) *pw = 0;
458  if (ph) *ph = 0;
459  if (pbps) *pbps = 0;
460  if (pspp) *pspp = 0;
461  if (piscmap) *piscmap = 0;
462  if (pformat) *pformat = 0;
463  iscmap = 0; /* init to false */
464  if (!filename)
465  return ERROR_INT("filename not defined", procName, 1);
466 
467  if ((fp = fopenReadStream(filename)) == NULL)
468  return ERROR_INT("image file not found", procName, 1);
469  findFileFormatStream(fp, &format);
470  fclose(fp);
471 
472  switch (format)
473  {
474  case IFF_BMP: /* cheating: reading the entire file */
475  if ((pix = pixRead(filename)) == NULL)
476  return ERROR_INT( "bmp: pix not read", procName, 1);
477  pixGetDimensions(pix, &w, &h, &d);
478  if (pixGetColormap(pix))
479  iscmap = 1;
480  pixDestroy(&pix);
481  bps = (d == 32) ? 8 : d;
482  spp = (d == 32) ? 3 : 1;
483  break;
484 
485  case IFF_JFIF_JPEG:
486  ret = readHeaderJpeg(filename, &w, &h, &spp, NULL, NULL);
487  bps = 8;
488  if (ret)
489  return ERROR_INT( "jpeg: no header info returned", procName, 1);
490  break;
491 
492  case IFF_PNG:
493  ret = readHeaderPng(filename, &w, &h, &bps, &spp, &iscmap);
494  if (ret)
495  return ERROR_INT( "png: no header info returned", procName, 1);
496  break;
497 
498  case IFF_TIFF:
499  case IFF_TIFF_PACKBITS:
500  case IFF_TIFF_RLE:
501  case IFF_TIFF_G3:
502  case IFF_TIFF_G4:
503  case IFF_TIFF_LZW:
504  case IFF_TIFF_ZIP:
505  case IFF_TIFF_JPEG:
506  /* Reading page 0 by default; possibly redefine format */
507  ret = readHeaderTiff(filename, 0, &w, &h, &bps, &spp, NULL, &iscmap,
508  &format);
509  if (ret)
510  return ERROR_INT( "tiff: no header info returned", procName, 1);
511  break;
512 
513  case IFF_PNM:
514  ret = readHeaderPnm(filename, &w, &h, &d, &type, &bps, &spp);
515  if (ret)
516  return ERROR_INT( "pnm: no header info returned", procName, 1);
517  break;
518 
519  case IFF_GIF: /* cheating: reading the entire file */
520  if ((pix = pixRead(filename)) == NULL)
521  return ERROR_INT( "gif: pix not read", procName, 1);
522  pixGetDimensions(pix, &w, &h, &d);
523  pixDestroy(&pix);
524  iscmap = 1; /* always colormapped; max 256 colors */
525  spp = 1;
526  bps = d;
527  break;
528 
529  case IFF_JP2:
530  ret = readHeaderJp2k(filename, &w, &h, &bps, &spp);
531  break;
532 
533  case IFF_WEBP:
534  if (readHeaderWebP(filename, &w, &h, &spp))
535  return ERROR_INT( "webp: no header info returned", procName, 1);
536  bps = 8;
537  break;
538 
539  case IFF_PS:
540  if (pformat) *pformat = format;
541  return ERROR_INT("PostScript reading is not supported\n", procName, 1);
542 
543  case IFF_LPDF:
544  if (pformat) *pformat = format;
545  return ERROR_INT("Pdf reading is not supported\n", procName, 1);
546 
547  case IFF_SPIX:
548  ret = readHeaderSpix(filename, &w, &h, &bps, &spp, &iscmap);
549  if (ret)
550  return ERROR_INT( "spix: no header info returned", procName, 1);
551  break;
552 
553  case IFF_UNKNOWN:
554  L_ERROR("unknown format in file %s\n", procName, filename);
555  return 1;
556  break;
557  }
558 
559  if (pw) *pw = w;
560  if (ph) *ph = h;
561  if (pbps) *pbps = bps;
562  if (pspp) *pspp = spp;
563  if (piscmap) *piscmap = iscmap;
564  if (pformat) *pformat = format;
565  return 0;
566 }
567 
568 
569 /*---------------------------------------------------------------------*
570  * Format finders *
571  *---------------------------------------------------------------------*/
579 l_ok
580 findFileFormat(const char *filename,
581  l_int32 *pformat)
582 {
583 l_int32 ret;
584 FILE *fp;
585 
586  PROCNAME("findFileFormat");
587 
588  if (!pformat)
589  return ERROR_INT("&format not defined", procName, 1);
590  *pformat = IFF_UNKNOWN;
591  if (!filename)
592  return ERROR_INT("filename not defined", procName, 1);
593 
594  if ((fp = fopenReadStream(filename)) == NULL)
595  return ERROR_INT("image file not found", procName, 1);
596  ret = findFileFormatStream(fp, pformat);
597  fclose(fp);
598  return ret;
599 }
600 
601 
614 l_ok
616  l_int32 *pformat)
617 {
618 l_uint8 firstbytes[12];
619 l_int32 format;
620 
621  PROCNAME("findFileFormatStream");
622 
623  if (!pformat)
624  return ERROR_INT("&format not defined", procName, 1);
625  *pformat = IFF_UNKNOWN;
626  if (!fp)
627  return ERROR_INT("stream not defined", procName, 1);
628 
629  rewind(fp);
630  if (fnbytesInFile(fp) < 12)
631  return ERROR_INT("truncated file", procName, 1);
632 
633  if (fread(&firstbytes, 1, 12, fp) != 12)
634  return ERROR_INT("failed to read first 12 bytes of file", procName, 1);
635  rewind(fp);
636 
637  findFileFormatBuffer(firstbytes, &format);
638  if (format == IFF_TIFF) {
639  findTiffCompression(fp, &format);
640  rewind(fp);
641  }
642  *pformat = format;
643  if (format == IFF_UNKNOWN)
644  return 1;
645  else
646  return 0;
647 }
648 
649 
665 l_ok
666 findFileFormatBuffer(const l_uint8 *buf,
667  l_int32 *pformat)
668 {
669 l_uint16 twobytepw;
670 
671  PROCNAME("findFileFormatBuffer");
672 
673  if (!pformat)
674  return ERROR_INT("&format not defined", procName, 1);
675  *pformat = IFF_UNKNOWN;
676  if (!buf)
677  return ERROR_INT("byte buffer not defined", procName, 0);
678 
679  /* Check the bmp and tiff 2-byte header ids */
680  ((char *)(&twobytepw))[0] = buf[0];
681  ((char *)(&twobytepw))[1] = buf[1];
682 
683  if (convertOnBigEnd16(twobytepw) == BMP_ID) {
684  *pformat = IFF_BMP;
685  return 0;
686  }
687 
688  if (twobytepw == TIFF_BIGEND_ID || twobytepw == TIFF_LITTLEEND_ID) {
689  *pformat = IFF_TIFF;
690  return 0;
691  }
692 
693  /* Check for the p*m 2-byte header ids */
694  if ((buf[0] == 'P' && buf[1] == '4') || /* newer packed */
695  (buf[0] == 'P' && buf[1] == '1')) { /* old ASCII format */
696  *pformat = IFF_PNM;
697  return 0;
698  }
699 
700  if ((buf[0] == 'P' && buf[1] == '5') || /* newer */
701  (buf[0] == 'P' && buf[1] == '2')) { /* old */
702  *pformat = IFF_PNM;
703  return 0;
704  }
705 
706  if ((buf[0] == 'P' && buf[1] == '6') || /* newer */
707  (buf[0] == 'P' && buf[1] == '3')) { /* old */
708  *pformat = IFF_PNM;
709  return 0;
710  }
711 
712  if (buf[0] == 'P' && buf[1] == '7') { /* new arbitrary (PAM) */
713  *pformat = IFF_PNM;
714  return 0;
715  }
716 
717  /* Consider the first 11 bytes of the standard JFIF JPEG header:
718  * - The first two bytes are the most important: 0xffd8.
719  * - The next two bytes are the jfif marker: 0xffe0.
720  * Not all jpeg files have this marker.
721  * - The next two bytes are the header length.
722  * - The next 5 bytes are a null-terminated string.
723  * For JFIF, the string is "JFIF", naturally. For others it
724  * can be "Exif" or just about anything else.
725  * - Because of all this variability, we only check the first
726  * two byte marker. All jpeg files are identified as
727  * IFF_JFIF_JPEG. */
728  if (buf[0] == 0xff && buf[1] == 0xd8) {
729  *pformat = IFF_JFIF_JPEG;
730  return 0;
731  }
732 
733  /* Check for the 8 byte PNG signature (png_signature in png.c):
734  * {137, 80, 78, 71, 13, 10, 26, 10} */
735  if (buf[0] == 137 && buf[1] == 80 && buf[2] == 78 && buf[3] == 71 &&
736  buf[4] == 13 && buf[5] == 10 && buf[6] == 26 && buf[7] == 10) {
737  *pformat = IFF_PNG;
738  return 0;
739  }
740 
741  /* Look for "GIF87a" or "GIF89a" */
742  if (buf[0] == 'G' && buf[1] == 'I' && buf[2] == 'F' && buf[3] == '8' &&
743  (buf[4] == '7' || buf[4] == '9') && buf[5] == 'a') {
744  *pformat = IFF_GIF;
745  return 0;
746  }
747 
748  /* Check for both types of jp2k file */
749  if (memcmp(buf, JP2K_CODESTREAM, 4) == 0 ||
750  memcmp(buf, JP2K_IMAGE_DATA, 12) == 0) {
751  *pformat = IFF_JP2;
752  return 0;
753  }
754 
755  /* Check for webp */
756  if (buf[0] == 'R' && buf[1] == 'I' && buf[2] == 'F' && buf[3] == 'F' &&
757  buf[8] == 'W' && buf[9] == 'E' && buf[10] == 'B' && buf[11] == 'P') {
758  *pformat = IFF_WEBP;
759  return 0;
760  }
761 
762  /* Check for ps */
763  if (buf[0] == '%' && buf[1] == '!' && buf[2] == 'P' && buf[3] == 'S' &&
764  buf[4] == '-' && buf[5] == 'A' && buf[6] == 'd' && buf[7] == 'o' &&
765  buf[8] == 'b' && buf[9] == 'e') {
766  *pformat = IFF_PS;
767  return 0;
768  }
769 
770  /* Check for pdf */
771  if (buf[0] == '%' && buf[1] == 'P' && buf[2] == 'D' && buf[3] == 'F' &&
772  buf[4] == '-' && buf[5] == '1') {
773  *pformat = IFF_LPDF;
774  return 0;
775  }
776 
777  /* Check for "spix" serialized pix */
778  if (buf[0] == 's' && buf[1] == 'p' && buf[2] == 'i' && buf[3] == 'x') {
779  *pformat = IFF_SPIX;
780  return 0;
781  }
782 
783  /* File format identifier not found; unknown */
784  return 1;
785 }
786 
787 
794 l_int32
796 {
797 l_int32 format;
798 
799  PROCNAME("fileFormatIsTiff");
800 
801  if (!fp)
802  return ERROR_INT("stream not defined", procName, 0);
803 
804  findFileFormatStream(fp, &format);
805  if (format == IFF_TIFF || format == IFF_TIFF_PACKBITS ||
806  format == IFF_TIFF_RLE || format == IFF_TIFF_G3 ||
807  format == IFF_TIFF_G4 || format == IFF_TIFF_LZW ||
808  format == IFF_TIFF_ZIP || format == IFF_TIFF_JPEG)
809  return 1;
810  else
811  return 0;
812 }
813 
814 
815 /*---------------------------------------------------------------------*
816  * Read from memory *
817  *---------------------------------------------------------------------*/
838 PIX *
839 pixReadMem(const l_uint8 *data,
840  size_t size)
841 {
842 l_int32 format, valid;
843 PIX *pix;
844 PIXCMAP *cmap;
845 
846  PROCNAME("pixReadMem");
847 
848  if (!data)
849  return (PIX *)ERROR_PTR("data not defined", procName, NULL);
850  if (size < 12)
851  return (PIX *)ERROR_PTR("size < 12", procName, NULL);
852  pix = NULL;
853 
854  findFileFormatBuffer(data, &format);
855  switch (format)
856  {
857  case IFF_BMP:
858  if ((pix = pixReadMemBmp(data, size)) == NULL )
859  return (PIX *)ERROR_PTR( "bmp: no pix returned", procName, NULL);
860  break;
861 
862  case IFF_JFIF_JPEG:
863  if ((pix = pixReadMemJpeg(data, size, 0, 1, NULL, 0)) == NULL)
864  return (PIX *)ERROR_PTR( "jpeg: no pix returned", procName, NULL);
865  break;
866 
867  case IFF_PNG:
868  if ((pix = pixReadMemPng(data, size)) == NULL)
869  return (PIX *)ERROR_PTR("png: no pix returned", procName, NULL);
870  break;
871 
872  case IFF_TIFF:
873  case IFF_TIFF_PACKBITS:
874  case IFF_TIFF_RLE:
875  case IFF_TIFF_G3:
876  case IFF_TIFF_G4:
877  case IFF_TIFF_LZW:
878  case IFF_TIFF_ZIP:
879  /* Reading page 0 by default */
880  if ((pix = pixReadMemTiff(data, size, 0)) == NULL)
881  return (PIX *)ERROR_PTR("tiff: no pix returned", procName, NULL);
882  break;
883 
884  case IFF_PNM:
885  if ((pix = pixReadMemPnm(data, size)) == NULL)
886  return (PIX *)ERROR_PTR("pnm: no pix returned", procName, NULL);
887  break;
888 
889  case IFF_GIF:
890  if ((pix = pixReadMemGif(data, size)) == NULL)
891  return (PIX *)ERROR_PTR("gif: no pix returned", procName, NULL);
892  break;
893 
894  case IFF_JP2:
895  if ((pix = pixReadMemJp2k(data, size, 1, NULL, 0, 0)) == NULL)
896  return (PIX *)ERROR_PTR("jp2k: no pix returned", procName, NULL);
897  break;
898 
899  case IFF_WEBP:
900  if ((pix = pixReadMemWebP(data, size)) == NULL)
901  return (PIX *)ERROR_PTR("webp: no pix returned", procName, NULL);
902  break;
903 
904  case IFF_PS:
905  L_ERROR("PostScript reading is not supported\n", procName);
906  return NULL;
907 
908  case IFF_LPDF:
909  L_ERROR("Pdf reading is not supported\n", procName);
910  return NULL;
911 
912  case IFF_SPIX:
913  if ((pix = pixReadMemSpix(data, size)) == NULL)
914  return (PIX *)ERROR_PTR("spix: no pix returned", procName, NULL);
915  break;
916 
917  case IFF_UNKNOWN:
918  return (PIX *)ERROR_PTR("Unknown format: no pix returned",
919  procName, NULL);
920  break;
921  }
922 
923  /* Set the input format. For tiff reading from memory we lose
924  * the actual input format; for 1 bpp, default to G4. Also
925  * verify that the colormap is valid. */
926  if (pix) {
927  if (format == IFF_TIFF && pixGetDepth(pix) == 1)
928  format = IFF_TIFF_G4;
929  pixSetInputFormat(pix, format);
930  if ((cmap = pixGetColormap(pix))) {
931  pixcmapIsValid(cmap, &valid);
932  if (!valid) {
933  pixDestroy(&pix);
934  return (PIX *)ERROR_PTR("invalid colormap", procName, NULL);
935  }
936  }
937  }
938  return pix;
939 }
940 
941 
967 l_ok
968 pixReadHeaderMem(const l_uint8 *data,
969  size_t size,
970  l_int32 *pformat,
971  l_int32 *pw,
972  l_int32 *ph,
973  l_int32 *pbps,
974  l_int32 *pspp,
975  l_int32 *piscmap)
976 {
977 l_int32 format, ret, w, h, d, bps, spp, iscmap;
978 l_int32 type; /* not used */
979 PIX *pix;
980 
981  PROCNAME("pixReadHeaderMem");
982 
983  if (pw) *pw = 0;
984  if (ph) *ph = 0;
985  if (pbps) *pbps = 0;
986  if (pspp) *pspp = 0;
987  if (piscmap) *piscmap = 0;
988  if (pformat) *pformat = 0;
989  iscmap = 0; /* init to false */
990  if (!data)
991  return ERROR_INT("data not defined", procName, 1);
992  if (size < 8)
993  return ERROR_INT("size < 8", procName, 1);
994 
995  findFileFormatBuffer(data, &format);
996 
997  switch (format)
998  {
999  case IFF_BMP: /* cheating: read the pix */
1000  if ((pix = pixReadMemBmp(data, size)) == NULL)
1001  return ERROR_INT( "bmp: pix not read", procName, 1);
1002  pixGetDimensions(pix, &w, &h, &d);
1003  pixDestroy(&pix);
1004  bps = (d == 32) ? 8 : d;
1005  spp = (d == 32) ? 3 : 1;
1006  break;
1007 
1008  case IFF_JFIF_JPEG:
1009  ret = readHeaderMemJpeg(data, size, &w, &h, &spp, NULL, NULL);
1010  bps = 8;
1011  if (ret)
1012  return ERROR_INT( "jpeg: no header info returned", procName, 1);
1013  break;
1014 
1015  case IFF_PNG:
1016  ret = readHeaderMemPng(data, size, &w, &h, &bps, &spp, &iscmap);
1017  if (ret)
1018  return ERROR_INT( "png: no header info returned", procName, 1);
1019  break;
1020 
1021  case IFF_TIFF:
1022  case IFF_TIFF_PACKBITS:
1023  case IFF_TIFF_RLE:
1024  case IFF_TIFF_G3:
1025  case IFF_TIFF_G4:
1026  case IFF_TIFF_LZW:
1027  case IFF_TIFF_ZIP:
1028  case IFF_TIFF_JPEG:
1029  /* Reading page 0 by default; possibly redefine format */
1030  ret = readHeaderMemTiff(data, size, 0, &w, &h, &bps, &spp,
1031  NULL, &iscmap, &format);
1032  if (ret)
1033  return ERROR_INT( "tiff: no header info returned", procName, 1);
1034  break;
1035 
1036  case IFF_PNM:
1037  ret = readHeaderMemPnm(data, size, &w, &h, &d, &type, &bps, &spp);
1038  if (ret)
1039  return ERROR_INT( "pnm: no header info returned", procName, 1);
1040  break;
1041 
1042  case IFF_GIF: /* cheating: read the pix */
1043  if ((pix = pixReadMemGif(data, size)) == NULL)
1044  return ERROR_INT( "gif: pix not read", procName, 1);
1045  pixGetDimensions(pix, &w, &h, &d);
1046  pixDestroy(&pix);
1047  iscmap = 1; /* always colormapped; max 256 colors */
1048  spp = 1;
1049  bps = d;
1050  break;
1051 
1052  case IFF_JP2:
1053  ret = readHeaderMemJp2k(data, size, &w, &h, &bps, &spp);
1054  break;
1055 
1056  case IFF_WEBP:
1057  bps = 8;
1058  ret = readHeaderMemWebP(data, size, &w, &h, &spp);
1059  break;
1060 
1061  case IFF_PS:
1062  if (pformat) *pformat = format;
1063  return ERROR_INT("PostScript reading is not supported\n", procName, 1);
1064 
1065  case IFF_LPDF:
1066  if (pformat) *pformat = format;
1067  return ERROR_INT("Pdf reading is not supported\n", procName, 1);
1068 
1069  case IFF_SPIX:
1070  ret = sreadHeaderSpix((l_uint32 *)data, &w, &h, &bps,
1071  &spp, &iscmap);
1072  if (ret)
1073  return ERROR_INT( "pnm: no header info returned", procName, 1);
1074  break;
1075 
1076  case IFF_UNKNOWN:
1077  return ERROR_INT("unknown format; no data returned", procName, 1);
1078  break;
1079  }
1080 
1081  if (pw) *pw = w;
1082  if (ph) *ph = h;
1083  if (pbps) *pbps = bps;
1084  if (pspp) *pspp = spp;
1085  if (piscmap) *piscmap = iscmap;
1086  if (pformat) *pformat = format;
1087  return 0;
1088 }
1089 
1090 
1091 /*---------------------------------------------------------------------*
1092  * Output image file information *
1093  *---------------------------------------------------------------------*/
1094 extern const char *ImageFileFormatExtensions[];
1095 
1114 l_ok
1115 writeImageFileInfo(const char *filename,
1116  FILE *fpout,
1117  l_int32 headeronly)
1118 {
1119 char *text;
1120 l_int32 w, h, d, wpl, count, npages, color;
1121 l_int32 format, bps, spp, iscmap, xres, yres, transparency;
1122 FILE *fpin;
1123 PIX *pix, *pixt;
1124 PIXCMAP *cmap;
1125 
1126  PROCNAME("writeImageFileInfo");
1127 
1128  if (!filename)
1129  return ERROR_INT("filename not defined", procName, 1);
1130  if (!fpout)
1131  return ERROR_INT("stream not defined", procName, 1);
1132 
1133  /* Read the header */
1134  if (pixReadHeader(filename, &format, &w, &h, &bps, &spp, &iscmap)) {
1135  L_ERROR("failure to read header of %s\n", procName, filename);
1136  return 1;
1137  }
1138  fprintf(fpout, "===============================================\n"
1139  "Reading the header:\n");
1140  fprintf(fpout, " input image format type: %s\n",
1141  ImageFileFormatExtensions[format]);
1142  fprintf(fpout, " w = %d, h = %d, bps = %d, spp = %d, iscmap = %d\n",
1143  w, h, bps, spp, iscmap);
1144 
1145  findFileFormat(filename, &format);
1146  if (format == IFF_JP2) {
1147  fpin = lept_fopen(filename, "rb");
1148  fgetJp2kResolution(fpin, &xres, &yres);
1149  fclose(fpin);
1150  fprintf(fpout, " xres = %d, yres = %d\n", xres, yres);
1151  } else if (format == IFF_PNG) {
1152  fpin = lept_fopen(filename, "rb");
1153  fgetPngResolution(fpin, &xres, &yres);
1154  fclose(fpin);
1155  fprintf(fpout, " xres = %d, yres = %d\n", xres, yres);
1156  if (iscmap) {
1157  fpin = lept_fopen(filename, "rb");
1158  fgetPngColormapInfo(fpin, &cmap, &transparency);
1159  fclose(fpin);
1160  if (transparency)
1161  fprintf(fpout, " colormap has transparency\n");
1162  else
1163  fprintf(fpout, " colormap does not have transparency\n");
1164  pixcmapWriteStream(fpout, cmap);
1165  pixcmapDestroy(&cmap);
1166  }
1167  } else if (format == IFF_JFIF_JPEG) {
1168  fpin = lept_fopen(filename, "rb");
1169  fgetJpegResolution(fpin, &xres, &yres);
1170  fclose(fpin);
1171  fprintf(fpout, " xres = %d, yres = %d\n", xres, yres);
1172  }
1173 
1174  if (headeronly)
1175  return 0;
1176 
1177  /* Read the full image. Note that when we read an image that
1178  * has transparency in a colormap, we convert it to RGBA. */
1179  fprintf(fpout, "===============================================\n"
1180  "Reading the full image:\n");
1181 
1182  /* Preserve 16 bpp if the format is png */
1183  if (format == IFF_PNG && bps == 16)
1185 
1186  if ((pix = pixRead(filename)) == NULL) {
1187  L_ERROR("failure to read full image of %s\n", procName, filename);
1188  return 1;
1189  }
1190 
1191  format = pixGetInputFormat(pix);
1192  pixGetDimensions(pix, &w, &h, &d);
1193  wpl = pixGetWpl(pix);
1194  spp = pixGetSpp(pix);
1195  fprintf(fpout, " input image format type: %s\n",
1196  ImageFileFormatExtensions[format]);
1197  fprintf(fpout, " w = %d, h = %d, d = %d, spp = %d, wpl = %d\n",
1198  w, h, d, spp, wpl);
1199  fprintf(fpout, " xres = %d, yres = %d\n",
1200  pixGetXRes(pix), pixGetYRes(pix));
1201 
1202  text = pixGetText(pix);
1203  if (text) /* not null */
1204  fprintf(fpout, " text: %s\n", text);
1205 
1206  cmap = pixGetColormap(pix);
1207  if (cmap) {
1208  pixcmapHasColor(cmap, &color);
1209  if (color)
1210  fprintf(fpout, " colormap exists and has color values:");
1211  else
1212  fprintf(fpout, " colormap exists and has only gray values:");
1213  pixcmapWriteStream(fpout, pixGetColormap(pix));
1214  }
1215  else
1216  fprintf(fpout, " colormap does not exist\n");
1217 
1218  if (format == IFF_TIFF || format == IFF_TIFF_G4 ||
1219  format == IFF_TIFF_G3 || format == IFF_TIFF_PACKBITS) {
1220  fprintf(fpout, " Tiff header information:\n");
1221  fpin = lept_fopen(filename, "rb");
1222  tiffGetCount(fpin, &npages);
1223  lept_fclose(fpin);
1224  if (npages == 1)
1225  fprintf(fpout, " One page in file\n");
1226  else
1227  fprintf(fpout, " %d pages in file\n", npages);
1228  fprintTiffInfo(fpout, filename);
1229  }
1230 
1231  if (d == 1) {
1232  pixCountPixels(pix, &count, NULL);
1233  pixGetDimensions(pix, &w, &h, NULL);
1234  fprintf(fpout, " 1 bpp: foreground pixel fraction ON/Total = %g\n",
1235  (l_float32)count / (l_float32)(w * h));
1236  }
1237  fprintf(fpout, "===============================================\n");
1238 
1239  /* If there is an alpha component, visualize it. Note that when
1240  * alpha == 0, the rgb layer is transparent. We visualize the
1241  * result when a white background is visible through the
1242  * transparency layer. */
1243  if (pixGetSpp(pix) == 4) {
1244  pixt = pixDisplayLayersRGBA(pix, 0xffffff00, 600.0);
1245  pixDisplay(pixt, 100, 100);
1246  pixDestroy(&pixt);
1247  }
1248 
1249  if (format == IFF_PNG && bps == 16)
1250  l_pngSetReadStrip16To8(1); /* return to default if format is png */
1251 
1252  pixDestroy(&pix);
1253  return 0;
1254 }
1255 
1256 
1257 /*---------------------------------------------------------------------*
1258  * Test function for I/O with different formats *
1259  *---------------------------------------------------------------------*/
1282 l_ok
1283 ioFormatTest(const char *filename)
1284 {
1285 l_int32 w, h, d, depth, equal, problems;
1286 l_float32 diff;
1287 BOX *box;
1288 PIX *pixs, *pixc, *pix1, *pix2;
1289 PIXCMAP *cmap;
1290 
1291  PROCNAME("ioFormatTest");
1292 
1293  if (!filename)
1294  return ERROR_INT("filename not defined", procName, 1);
1295 
1296  /* Read the input file and limit the size */
1297  if ((pix1 = pixRead(filename)) == NULL)
1298  return ERROR_INT("pix1 not made", procName, 1);
1299  pixGetDimensions(pix1, &w, &h, NULL);
1300  if (w > 250 && h > 250) { /* take the central 250 x 250 region */
1301  box = boxCreate(w / 2 - 125, h / 2 - 125, 250, 250);
1302  pixs = pixClipRectangle(pix1, box, NULL);
1303  boxDestroy(&box);
1304  } else {
1305  pixs = pixClone(pix1);
1306  }
1307  pixDestroy(&pix1);
1308 
1309  lept_mkdir("lept/format");
1310 
1311  /* Note that the reader automatically removes colormaps
1312  * from 1 bpp BMP images, but not from 8 bpp BMP images.
1313  * Therefore, if our 8 bpp image initially doesn't have a
1314  * colormap, we are going to need to remove it from any
1315  * pix read from a BMP file. */
1316  pixc = pixClone(pixs); /* laziness */
1317 
1318  /* This does not test the alpha layer pixels, because most
1319  * formats don't support it. Remove any alpha. */
1320  if (pixGetSpp(pixc) == 4)
1321  pixSetSpp(pixc, 3);
1322  cmap = pixGetColormap(pixc); /* colormap; can be NULL */
1323  d = pixGetDepth(pixc);
1324 
1325  problems = FALSE;
1326 
1327  /* ----------------------- BMP -------------------------- */
1328 
1329  /* BMP works for 1, 2, 4, 8 and 32 bpp images.
1330  * It always writes colormaps for 1 and 8 bpp, so we must
1331  * remove it after readback if the input image doesn't have
1332  * a colormap. Although we can write/read 2 bpp BMP, nobody
1333  * else can read them! */
1334  if (d == 1 || d == 8) {
1335  L_INFO("write/read bmp\n", procName);
1336  pixWrite(FILE_BMP, pixc, IFF_BMP);
1337  pix1 = pixRead(FILE_BMP);
1338  if (!cmap)
1340  else
1341  pix2 = pixClone(pix1);
1342  pixEqual(pixc, pix2, &equal);
1343  if (!equal) {
1344  L_INFO(" **** bad bmp image: d = %d ****\n", procName, d);
1345  problems = TRUE;
1346  }
1347  pixDestroy(&pix1);
1348  pixDestroy(&pix2);
1349  }
1350 
1351  if (d == 2 || d == 4 || d == 32) {
1352  L_INFO("write/read bmp\n", procName);
1353  pixWrite(FILE_BMP, pixc, IFF_BMP);
1354  pix1 = pixRead(FILE_BMP);
1355  pixEqual(pixc, pix1, &equal);
1356  if (!equal) {
1357  L_INFO(" **** bad bmp image: d = %d ****\n", procName, d);
1358  problems = TRUE;
1359  }
1360  pixDestroy(&pix1);
1361  }
1362 
1363  /* ----------------------- PNG -------------------------- */
1364 #if HAVE_LIBPNG
1365  /* PNG works for all depths, but here, because we strip
1366  * 16 --> 8 bpp on reading, we don't test png for 16 bpp. */
1367  if (d != 16) {
1368  L_INFO("write/read png\n", procName);
1369  pixWrite(FILE_PNG, pixc, IFF_PNG);
1370  pix1 = pixRead(FILE_PNG);
1371  pixEqual(pixc, pix1, &equal);
1372  if (!equal) {
1373  L_INFO(" **** bad png image: d = %d ****\n", procName, d);
1374  problems = TRUE;
1375  }
1376  pixDestroy(&pix1);
1377  }
1378 #endif /* HAVE_LIBPNG */
1379 
1380  /* ----------------------- TIFF -------------------------- */
1381 #if HAVE_LIBTIFF
1382  /* TIFF works for 1, 2, 4, 8, 16 and 32 bpp images.
1383  * Because 8 bpp tiff always writes 256 entry colormaps, the
1384  * colormap sizes may be different for 8 bpp images with
1385  * colormap; we are testing if the image content is the same.
1386  * Likewise, the 2 and 4 bpp tiff images with colormaps
1387  * have colormap sizes 4 and 16, rsp. This test should
1388  * work properly on the content, regardless of the number
1389  * of color entries in pixc. */
1390 
1391  /* tiff uncompressed works for all pixel depths */
1392  L_INFO("write/read uncompressed tiff\n", procName);
1393  pixWrite(FILE_TIFF, pixc, IFF_TIFF);
1394  pix1 = pixRead(FILE_TIFF);
1395  pixEqual(pixc, pix1, &equal);
1396  if (!equal) {
1397  L_INFO(" **** bad tiff uncompressed image: d = %d ****\n",
1398  procName, d);
1399  problems = TRUE;
1400  }
1401  pixDestroy(&pix1);
1402 
1403  /* tiff lzw works for all pixel depths */
1404  L_INFO("write/read lzw compressed tiff\n", procName);
1405  pixWrite(FILE_LZW, pixc, IFF_TIFF_LZW);
1406  pix1 = pixRead(FILE_LZW);
1407  pixEqual(pixc, pix1, &equal);
1408  if (!equal) {
1409  L_INFO(" **** bad tiff lzw compressed image: d = %d ****\n",
1410  procName, d);
1411  problems = TRUE;
1412  }
1413  pixDestroy(&pix1);
1414 
1415  /* tiff adobe deflate (zip) works for all pixel depths */
1416  L_INFO("write/read zip compressed tiff\n", procName);
1417  pixWrite(FILE_ZIP, pixc, IFF_TIFF_ZIP);
1418  pix1 = pixRead(FILE_ZIP);
1419  pixEqual(pixc, pix1, &equal);
1420  if (!equal) {
1421  L_INFO(" **** bad tiff zip compressed image: d = %d ****\n",
1422  procName, d);
1423  problems = TRUE;
1424  }
1425  pixDestroy(&pix1);
1426 
1427  /* tiff jpeg encoding works for grayscale and rgb */
1428  if (d == 8 || d == 32) {
1429  PIX *pixc1;
1430  L_INFO("write/read jpeg compressed tiff\n", procName);
1431  if (d == 8 && pixGetColormap(pixc)) {
1433  pixWrite(FILE_TIFF_JPEG, pixc1, IFF_TIFF_JPEG);
1434  if ((pix1 = pixRead(FILE_TIFF_JPEG)) == NULL) {
1435  L_INFO(" did not read FILE_TIFF_JPEG\n", procName);
1436  problems = TRUE;
1437  }
1438  pixDestroy(&pixc1);
1439  } else {
1440  pixWrite(FILE_TIFF_JPEG, pixc, IFF_TIFF_JPEG);
1441  pix1 = pixRead(FILE_TIFF_JPEG);
1442  if (d == 8) {
1443  pixCompareGray(pix1, pixc, L_COMPARE_ABS_DIFF, 0, NULL, &diff,
1444  NULL, NULL);
1445  } else {
1446  pixCompareRGB(pix1, pixc, L_COMPARE_ABS_DIFF, 0, NULL, &diff,
1447  NULL, NULL);
1448  }
1449  if (diff > 8.0) {
1450  L_INFO(" **** bad tiff jpeg compressed image: "
1451  "d = %d, diff = %5.2f ****\n", procName, d, diff);
1452  problems = TRUE;
1453  }
1454  }
1455  pixDestroy(&pix1);
1456  }
1457 
1458  /* tiff g4, g3, rle and packbits work for 1 bpp */
1459  if (d == 1) {
1460  L_INFO("write/read g4 compressed tiff\n", procName);
1461  pixWrite(FILE_G4, pixc, IFF_TIFF_G4);
1462  pix1 = pixRead(FILE_G4);
1463  pixEqual(pixc, pix1, &equal);
1464  if (!equal) {
1465  L_INFO(" **** bad tiff g4 image ****\n", procName);
1466  problems = TRUE;
1467  }
1468  pixDestroy(&pix1);
1469 
1470  L_INFO("write/read g3 compressed tiff\n", procName);
1471  pixWrite(FILE_G3, pixc, IFF_TIFF_G3);
1472  pix1 = pixRead(FILE_G3);
1473  pixEqual(pixc, pix1, &equal);
1474  if (!equal) {
1475  L_INFO(" **** bad tiff g3 image ****\n", procName);
1476  problems = TRUE;
1477  }
1478  pixDestroy(&pix1);
1479 
1480  L_INFO("write/read rle compressed tiff\n", procName);
1481  pixWrite(FILE_RLE, pixc, IFF_TIFF_RLE);
1482  pix1 = pixRead(FILE_RLE);
1483  pixEqual(pixc, pix1, &equal);
1484  if (!equal) {
1485  L_INFO(" **** bad tiff rle image: d = %d ****\n", procName, d);
1486  problems = TRUE;
1487  }
1488  pixDestroy(&pix1);
1489 
1490  L_INFO("write/read packbits compressed tiff\n", procName);
1491  pixWrite(FILE_PB, pixc, IFF_TIFF_PACKBITS);
1492  pix1 = pixRead(FILE_PB);
1493  pixEqual(pixc, pix1, &equal);
1494  if (!equal) {
1495  L_INFO(" **** bad tiff packbits image: d = %d ****\n",
1496  procName, d);
1497  problems = TRUE;
1498  }
1499  pixDestroy(&pix1);
1500  }
1501 #endif /* HAVE_LIBTIFF */
1502 
1503  /* ----------------------- PNM -------------------------- */
1504 
1505  /* pnm works for 1, 2, 4, 8, 16 and 32 bpp.
1506  * pnm doesn't have colormaps, so when we write colormapped
1507  * pix out as pnm, the colormap is removed. Thus for the test,
1508  * we must remove the colormap from pixc before testing. */
1509  L_INFO("write/read pnm\n", procName);
1510  pixWrite(FILE_PNM, pixc, IFF_PNM);
1511  pix1 = pixRead(FILE_PNM);
1512  if (cmap)
1514  else
1515  pix2 = pixClone(pixc);
1516  pixEqual(pix1, pix2, &equal);
1517  if (!equal) {
1518  L_INFO(" **** bad pnm image: d = %d ****\n", procName, d);
1519  problems = TRUE;
1520  }
1521  pixDestroy(&pix1);
1522  pixDestroy(&pix2);
1523 
1524  /* ----------------------- GIF -------------------------- */
1525 #if HAVE_LIBGIF
1526  /* GIF works for only 1 and 8 bpp, colormapped */
1527  if (d != 8 || !cmap)
1528  pix1 = pixConvertTo8(pixc, 1);
1529  else
1530  pix1 = pixClone(pixc);
1531  L_INFO("write/read gif\n", procName);
1532  pixWrite(FILE_GIF, pix1, IFF_GIF);
1533  pix2 = pixRead(FILE_GIF);
1534  pixEqual(pix1, pix2, &equal);
1535  if (!equal) {
1536  L_INFO(" **** bad gif image: d = %d ****\n", procName, d);
1537  problems = TRUE;
1538  }
1539  pixDestroy(&pix1);
1540  pixDestroy(&pix2);
1541 #endif /* HAVE_LIBGIF */
1542 
1543  /* ----------------------- JPEG ------------------------- */
1544 #if HAVE_LIBJPEG
1545  /* JPEG works for only 8 bpp gray and rgb */
1546  if (cmap || d > 8)
1547  pix1 = pixConvertTo32(pixc);
1548  else
1549  pix1 = pixConvertTo8(pixc, 0);
1550  depth = pixGetDepth(pix1);
1551  L_INFO("write/read jpeg\n", procName);
1552  pixWrite(FILE_JPG, pix1, IFF_JFIF_JPEG);
1553  pix2 = pixRead(FILE_JPG);
1554  if (depth == 8) {
1555  pixCompareGray(pix1, pix2, L_COMPARE_ABS_DIFF, 0, NULL, &diff,
1556  NULL, NULL);
1557  } else {
1558  pixCompareRGB(pix1, pix2, L_COMPARE_ABS_DIFF, 0, NULL, &diff,
1559  NULL, NULL);
1560  }
1561  if (diff > 8.0) {
1562  L_INFO(" **** bad jpeg image: d = %d, diff = %5.2f ****\n",
1563  procName, depth, diff);
1564  problems = TRUE;
1565  }
1566  pixDestroy(&pix1);
1567  pixDestroy(&pix2);
1568 #endif /* HAVE_LIBJPEG */
1569 
1570  /* ----------------------- WEBP ------------------------- */
1571 #if HAVE_LIBWEBP
1572  /* WEBP works for rgb and rgba */
1573  if (cmap || d <= 16)
1574  pix1 = pixConvertTo32(pixc);
1575  else
1576  pix1 = pixClone(pixc);
1577  depth = pixGetDepth(pix1);
1578  L_INFO("write/read webp\n", procName);
1579  pixWrite(FILE_WEBP, pix1, IFF_WEBP);
1580  pix2 = pixRead(FILE_WEBP);
1581  pixCompareRGB(pix1, pix2, L_COMPARE_ABS_DIFF, 0, NULL, &diff, NULL, NULL);
1582  if (diff > 5.0) {
1583  L_INFO(" **** bad webp image: d = %d, diff = %5.2f ****\n",
1584  procName, depth, diff);
1585  problems = TRUE;
1586  }
1587  pixDestroy(&pix1);
1588  pixDestroy(&pix2);
1589 #endif /* HAVE_LIBWEBP */
1590 
1591  /* ----------------------- JP2K ------------------------- */
1592 #if HAVE_LIBJP2K
1593  /* JP2K works for only 8 bpp gray, rgb and rgba */
1594  if (cmap || d > 8)
1595  pix1 = pixConvertTo32(pixc);
1596  else
1597  pix1 = pixConvertTo8(pixc, 0);
1598  depth = pixGetDepth(pix1);
1599  L_INFO("write/read jp2k\n", procName);
1600  pixWrite(FILE_JP2K, pix1, IFF_JP2);
1601  pix2 = pixRead(FILE_JP2K);
1602  if (depth == 8) {
1603  pixCompareGray(pix1, pix2, L_COMPARE_ABS_DIFF, 0, NULL, &diff,
1604  NULL, NULL);
1605  } else {
1606  pixCompareRGB(pix1, pix2, L_COMPARE_ABS_DIFF, 0, NULL, &diff,
1607  NULL, NULL);
1608  }
1609  fprintf(stderr, "diff = %7.3f\n", diff);
1610  if (diff > 7.0) {
1611  L_INFO(" **** bad jp2k image: d = %d, diff = %5.2f ****\n",
1612  procName, depth, diff);
1613  problems = TRUE;
1614  }
1615  pixDestroy(&pix1);
1616  pixDestroy(&pix2);
1617 #endif /* HAVE_LIBJP2K */
1618 
1619  if (problems == FALSE)
1620  L_INFO("All formats read and written OK!\n", procName);
1621 
1622  pixDestroy(&pixc);
1623  pixDestroy(&pixs);
1624  return problems;
1625 }
l_ok writeImageFileInfo(const char *filename, FILE *fpout, l_int32 headeronly)
writeImageFileInfo()
Definition: readfile.c:1115
l_ok readHeaderTiff(const char *filename, l_int32 n, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp, l_int32 *pres, l_int32 *pcmap, l_int32 *pformat)
readHeaderTiff()
Definition: tiffio.c:1672
PIX * pixRemoveColormap(PIX *pixs, l_int32 type)
pixRemoveColormap()
Definition: pixconv.c:322
PIX * pixReadStreamTiff(FILE *fp, l_int32 n)
pixReadStreamTiff()
Definition: tiffio.c:406
l_int32 lept_mkdir(const char *subdir)
lept_mkdir()
Definition: utils2.c:1944
Definition: pix.h:717
l_ok fprintTiffInfo(FILE *fpout, const char *tiffile)
fprintTiffInfo()
Definition: tiffio.c:1489
l_ok readHeaderMemPng(const l_uint8 *data, size_t size, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp, l_int32 *piscmap)
readHeaderMemPng()
Definition: pngio.c:619
l_ok readHeaderMemJp2k(const l_uint8 *data, size_t size, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp)
readHeaderMemJp2k()
Definition: jp2kheader.c:167
PIX * pixReadMemSpix(const l_uint8 *data, size_t size)
pixReadMemSpix()
Definition: spixio.c:297
PIX * pixReadStreamPng(FILE *fp)
pixReadStreamPng()
Definition: pngio.c:185
PIX * pixConvertTo32(PIX *pixs)
pixConvertTo32()
Definition: pixconv.c:3233
l_ok readHeaderPnm(const char *filename, l_int32 *pw, l_int32 *ph, l_int32 *pd, l_int32 *ptype, l_int32 *pbps, l_int32 *pspp)
readHeaderPnm()
Definition: pnmio.c:447
PIXA * pixaReadFiles(const char *dirname, const char *substr)
pixaReadFiles()
Definition: readfile.c:124
PIX * pixReadStreamBmp(FILE *fp)
pixReadStreamBmp()
Definition: bmpio.c:90
PIXA * pixaCreate(l_int32 n)
pixaCreate()
Definition: pixabasic.c:163
Definition: pix.h:716
PIX * pixReadIndexed(SARRAY *sa, l_int32 index)
pixReadIndexed()
Definition: readfile.c:277
PIX * pixConvertTo8(PIX *pixs, l_int32 cmapflag)
pixConvertTo8()
Definition: pixconv.c:3041
l_ok pixcmapWriteStream(FILE *fp, PIXCMAP *cmap)
pixcmapWriteStream()
Definition: colormap.c:1758
PIX * pixReadMemBmp(const l_uint8 *cdata, size_t size)
pixReadMemBmp()
Definition: bmpio.c:120
void pixcmapDestroy(PIXCMAP **pcmap)
pixcmapDestroy()
Definition: colormap.c:265
l_ok readHeaderMemTiff(const l_uint8 *cdata, size_t size, l_int32 n, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp, l_int32 *pres, l_int32 *pcmap, l_int32 *pformat)
readHeaderMemTiff()
Definition: tiffio.c:1800
PIX * pixReadMemPnm(const l_uint8 *data, size_t size)
pixReadMemPnm()
Definition: pnmio.c:1084
void l_pngSetReadStrip16To8(l_int32 flag)
l_pngSetReadStrip16To8()
Definition: pngio.c:1278
PIX * pixReadMem(const l_uint8 *data, size_t size)
pixReadMem()
Definition: readfile.c:839
l_ok pixCompareRGB(PIX *pix1, PIX *pix2, l_int32 comptype, l_int32 plottype, l_int32 *psame, l_float32 *pdiff, l_float32 *prmsdiff, PIX **ppixdiff)
pixCompareRGB()
Definition: compare.c:968
PIX * pixClipRectangle(PIX *pixs, BOX *box, BOX **pboxc)
pixClipRectangle()
Definition: pix5.c:1020
l_ok readHeaderMemPnm(const l_uint8 *data, size_t size, l_int32 *pw, l_int32 *ph, l_int32 *pd, l_int32 *ptype, l_int32 *pbps, l_int32 *pspp)
readHeaderMemPnm()
Definition: pnmio.c:1117
Definition: array.h:116
PIX * pixReadStreamPnm(FILE *fp)
pixReadStreamPnm()
Definition: pnmio.c:145
l_ok pixSetText(PIX *pix, const char *textstring)
pixSetText()
Definition: pix1.c:1483
PIX * pixReadMemJpeg(const l_uint8 *data, size_t size, l_int32 cmflag, l_int32 reduction, l_int32 *pnwarn, l_int32 hint)
pixReadMemJpeg()
Definition: jpegio.c:991
l_ok findFileFormatStream(FILE *fp, l_int32 *pformat)
findFileFormatStream()
Definition: readfile.c:615
l_ok readHeaderMemJpeg(const l_uint8 *data, size_t size, l_int32 *pw, l_int32 *ph, l_int32 *pspp, l_int32 *pycck, l_int32 *pcmyk)
readHeaderMemJpeg()
Definition: jpegio.c:1038
l_ok readHeaderPng(const char *filename, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp, l_int32 *piscmap)
readHeaderPng()
Definition: pngio.c:518
l_ok pixCompareGray(PIX *pix1, PIX *pix2, l_int32 comptype, l_int32 plottype, l_int32 *psame, l_float32 *pdiff, l_float32 *prmsdiff, PIX **ppixdiff)
pixCompareGray()
Definition: compare.c:859
l_ok pixaAddPix(PIXA *pixa, PIX *pix, l_int32 copyflag)
pixaAddPix()
Definition: pixabasic.c:503
PIX * pixReadStreamJpeg(FILE *fp, l_int32 cmapflag, l_int32 reduction, l_int32 *pnwarn, l_int32 hint)
pixReadStreamJpeg()
Definition: jpegio.c:270
l_ok pixCountPixels(PIX *pixs, l_int32 *pcount, l_int32 *tab8)
pixCountPixels()
Definition: pix3.c:1823
l_ok pixcmapIsValid(PIXCMAP *cmap, l_int32 *pvalid)
pixcmapIsValid()
Definition: colormap.c:293
size_t fnbytesInFile(FILE *fp)
fnbytesInFile()
Definition: utils2.c:1495
PIX * pixReadStreamSpix(FILE *fp)
pixReadStreamSpix()
Definition: spixio.c:88
l_ok findFileFormat(const char *filename, l_int32 *pformat)
findFileFormat()
Definition: readfile.c:580
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
Definition: sarray1.c:681
PIX * pixClone(PIX *pixs)
pixClone()
Definition: pix1.c:515
l_ok findFileFormatBuffer(const l_uint8 *buf, l_int32 *pformat)
findFileFormatBuffer()
Definition: readfile.c:666
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:543
l_ok pixcmapHasColor(PIXCMAP *cmap, l_int32 *pcolor)
pixcmapHasColor()
Definition: colormap.c:1002
PIX * pixReadStream(FILE *fp, l_int32 hint)
pixReadStream()
Definition: readfile.c:318
l_ok sreadHeaderSpix(const l_uint32 *data, l_int32 *pwidth, l_int32 *pheight, l_int32 *pbps, l_int32 *pspp, l_int32 *piscmap)
sreadHeaderSpix()
Definition: spixio.c:211
SARRAY * getSortedPathnamesInDirectory(const char *dirname, const char *substr, l_int32 first, l_int32 nfiles)
getSortedPathnamesInDirectory()
Definition: sarray1.c:1717
Definition: pix.h:454
PIX * pixReadMemTiff(const l_uint8 *cdata, size_t size, l_int32 n)
pixReadMemTiff()
Definition: tiffio.c:2503
PIXA * pixaReadFilesSA(SARRAY *sa)
pixaReadFilesSA()
Definition: readfile.c:151
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
Definition: pix1.c:1065
l_int32 fileFormatIsTiff(FILE *fp)
fileFormatIsTiff()
Definition: readfile.c:795
FILE * fopenReadStream(const char *filename)
fopenReadStream()
Definition: utils2.c:1657
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
Definition: sarray1.c:621
PIX * pixRead(const char *filename)
pixRead()
Definition: readfile.c:190
FILE * lept_fopen(const char *filename, const char *mode)
lept_fopen()
Definition: utils2.c:1838
char * pixGetText(PIX *pix)
pixGetText()
Definition: pix1.c:1459
PIX * pixReadWithHint(const char *filename, l_int32 hint)
pixReadWithHint()
Definition: readfile.c:226
l_ok findTiffCompression(FILE *fp, l_int32 *pcomptype)
findTiffCompression()
Definition: tiffio.c:1935
l_ok lept_fclose(FILE *fp)
lept_fclose()
Definition: utils2.c:1868
l_ok pixReadHeaderMem(const l_uint8 *data, size_t size, l_int32 *pformat, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp, l_int32 *piscmap)
pixReadHeaderMem()
Definition: readfile.c:968
PIX * pixDisplayLayersRGBA(PIX *pixs, l_uint32 val, l_int32 maxw)
pixDisplayLayersRGBA()
Definition: pix2.c:2276
Definition: pix.h:134
void boxDestroy(BOX **pbox)
boxDestroy()
Definition: boxbasic.c:278
l_ok pixEqual(PIX *pix1, PIX *pix2, l_int32 *psame)
pixEqual()
Definition: compare.c:150
PIX * pixReadMemPng(const l_uint8 *filedata, size_t filesize)
pixReadMemPng()
Definition: pngio.c:1521
l_ok readHeaderSpix(const char *filename, l_int32 *pwidth, l_int32 *pheight, l_int32 *pbps, l_int32 *pspp, l_int32 *piscmap)
readHeaderSpix()
Definition: spixio.c:126
l_ok readHeaderJp2k(const char *filename, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp)
readHeaderJp2k()
Definition: jp2kheader.c:75
Definition: pix.h:480
l_ok readHeaderJpeg(const char *filename, l_int32 *pw, l_int32 *ph, l_int32 *pspp, l_int32 *pycck, l_int32 *pcmyk)
readHeaderJpeg()
Definition: jpegio.c:507
l_ok tiffGetCount(FILE *fp, l_int32 *pn)
tiffGetCount()
Definition: tiffio.c:1522
BOX * boxCreate(l_int32 x, l_int32 y, l_int32 w, l_int32 h)
boxCreate()
Definition: boxbasic.c:165
l_ok ioFormatTest(const char *filename)
ioFormatTest()
Definition: readfile.c:1283
l_ok pixReadHeader(const char *filename, l_int32 *pformat, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp, l_int32 *piscmap)
pixReadHeader()
Definition: readfile.c:442
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
Definition: sarray1.c:355