Leptonica  1.73
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 = "/tmp/lept/format/file.tif";
92 static const char *FILE_JPG = "/tmp/lept/format/file.jpg";
93 static const char *FILE_GIF = "/tmp/lept/format/file.gif";
94 static const char *FILE_WEBP = "/tmp/lept/format/file.webp";
95 static const char *FILE_JP2K = "/tmp/lept/format/file.jp2";
96 
97 static const unsigned char JP2K_CODESTREAM[4] = { 0xff, 0x4f, 0xff, 0x51 };
98 static const unsigned char JP2K_IMAGE_DATA[12] = { 0x00, 0x00, 0x00, 0x0C,
99  0x6A, 0x50, 0x20, 0x20,
100  0x0D, 0x0A, 0x87, 0x0A };
101 
102 
103 /*---------------------------------------------------------------------*
104  * Top-level functions for reading images from file *
105  *---------------------------------------------------------------------*/
122 PIXA *
123 pixaReadFiles(const char *dirname,
124  const char *substr)
125 {
126 PIXA *pixa;
127 SARRAY *sa;
128 
129  PROCNAME("pixaReadFiles");
130 
131  if (!dirname)
132  return (PIXA *)ERROR_PTR("dirname not defined", procName, NULL);
133 
134  if ((sa = getSortedPathnamesInDirectory(dirname, substr, 0, 0)) == NULL)
135  return (PIXA *)ERROR_PTR("sa not made", procName, NULL);
136 
137  pixa = pixaReadFilesSA(sa);
138  sarrayDestroy(&sa);
139  return pixa;
140 }
141 
142 
149 PIXA *
151 {
152 char *str;
153 l_int32 i, n;
154 PIX *pix;
155 PIXA *pixa;
156 
157  PROCNAME("pixaReadFilesSA");
158 
159  if (!sa)
160  return (PIXA *)ERROR_PTR("sa not defined", procName, NULL);
161 
162  n = sarrayGetCount(sa);
163  pixa = pixaCreate(n);
164  for (i = 0; i < n; i++) {
165  str = sarrayGetString(sa, i, L_NOCOPY);
166  if ((pix = pixRead(str)) == NULL) {
167  L_WARNING("pix not read from file %s\n", procName, str);
168  continue;
169  }
170  pixaAddPix(pixa, pix, L_INSERT);
171  }
172 
173  return pixa;
174 }
175 
176 
188 PIX *
189 pixRead(const char *filename)
190 {
191 FILE *fp;
192 PIX *pix;
193 
194  PROCNAME("pixRead");
195 
196  if (!filename)
197  return (PIX *)ERROR_PTR("filename not defined", procName, NULL);
198 
199  if ((fp = fopenReadStream(filename)) == NULL) {
200  L_ERROR("image file not found: %s\n", procName, filename);
201  return NULL;
202  }
203  pix = pixReadStream(fp, 0);
204  fclose(fp);
205  if (!pix)
206  return (PIX *)ERROR_PTR("pix not read", procName, NULL);
207  return pix;
208 }
209 
210 
224 PIX *
225 pixReadWithHint(const char *filename,
226  l_int32 hint)
227 {
228 FILE *fp;
229 PIX *pix;
230 
231  PROCNAME("pixReadWithHint");
232 
233  if (!filename)
234  return (PIX *)ERROR_PTR("filename not defined", procName, NULL);
235 
236  if ((fp = fopenReadStream(filename)) == NULL)
237  return (PIX *)ERROR_PTR("image file not found", procName, NULL);
238  pix = pixReadStream(fp, hint);
239  fclose(fp);
240 
241  if (!pix)
242  return (PIX *)ERROR_PTR("image not returned", procName, NULL);
243  return pix;
244 }
245 
246 
275 PIX *
277  l_int32 index)
278 {
279 char *fname;
280 l_int32 n;
281 PIX *pix;
282 
283  PROCNAME("pixReadIndexed");
284 
285  if (!sa)
286  return (PIX *)ERROR_PTR("sa not defined", procName, NULL);
287  n = sarrayGetCount(sa);
288  if (index < 0 || index >= n)
289  return (PIX *)ERROR_PTR("index out of bounds", procName, NULL);
290 
291  fname = sarrayGetString(sa, index, L_NOCOPY);
292  if (fname[0] == '\0')
293  return NULL;
294 
295  if ((pix = pixRead(fname)) == NULL) {
296  L_ERROR("pix not read from file %s\n", procName, fname);
297  return NULL;
298  }
299 
300  return pix;
301 }
302 
303 
316 PIX *
317 pixReadStream(FILE *fp,
318  l_int32 hint)
319 {
320 l_int32 format, ret;
321 l_uint8 *comment;
322 PIX *pix;
323 
324  PROCNAME("pixReadStream");
325 
326  if (!fp)
327  return (PIX *)ERROR_PTR("stream not defined", procName, NULL);
328  pix = NULL;
329 
330  findFileFormatStream(fp, &format);
331  switch (format)
332  {
333  case IFF_BMP:
334  if ((pix = pixReadStreamBmp(fp)) == NULL )
335  return (PIX *)ERROR_PTR( "bmp: no pix returned", procName, NULL);
336  break;
337 
338  case IFF_JFIF_JPEG:
339  if ((pix = pixReadStreamJpeg(fp, 0, 1, NULL, hint)) == NULL)
340  return (PIX *)ERROR_PTR( "jpeg: no pix returned", procName, NULL);
341  ret = fgetJpegComment(fp, &comment);
342  if (!ret && comment)
343  pixSetText(pix, (char *)comment);
344  LEPT_FREE(comment);
345  break;
346 
347  case IFF_PNG:
348  if ((pix = pixReadStreamPng(fp)) == NULL)
349  return (PIX *)ERROR_PTR("png: no pix returned", procName, NULL);
350  break;
351 
352  case IFF_TIFF:
353  case IFF_TIFF_PACKBITS:
354  case IFF_TIFF_RLE:
355  case IFF_TIFF_G3:
356  case IFF_TIFF_G4:
357  case IFF_TIFF_LZW:
358  case IFF_TIFF_ZIP:
359  if ((pix = pixReadStreamTiff(fp, 0)) == NULL) /* page 0 by default */
360  return (PIX *)ERROR_PTR("tiff: no pix returned", procName, NULL);
361  break;
362 
363  case IFF_PNM:
364  if ((pix = pixReadStreamPnm(fp)) == NULL)
365  return (PIX *)ERROR_PTR("pnm: no pix returned", procName, NULL);
366  break;
367 
368  case IFF_GIF:
369  if ((pix = pixReadStreamGif(fp)) == NULL)
370  return (PIX *)ERROR_PTR("gif: no pix returned", procName, NULL);
371  break;
372 
373  case IFF_JP2:
374  if ((pix = pixReadStreamJp2k(fp, 1, NULL, 0, 0)) == NULL)
375  return (PIX *)ERROR_PTR("jp2: no pix returned", procName, NULL);
376  break;
377 
378  case IFF_WEBP:
379  if ((pix = pixReadStreamWebP(fp)) == NULL)
380  return (PIX *)ERROR_PTR("webp: no pix returned", procName, NULL);
381  break;
382 
383  case IFF_PS:
384  L_ERROR("PostScript reading is not supported\n", procName);
385  return NULL;
386 
387  case IFF_LPDF:
388  L_ERROR("Pdf reading is not supported\n", procName);
389  return NULL;
390 
391  case IFF_SPIX:
392  if ((pix = pixReadStreamSpix(fp)) == NULL)
393  return (PIX *)ERROR_PTR("spix: no pix returned", procName, NULL);
394  break;
395 
396  case IFF_UNKNOWN:
397  return (PIX *)ERROR_PTR( "Unknown format: no pix returned",
398  procName, NULL);
399  break;
400  }
401 
402  if (pix)
403  pixSetInputFormat(pix, format);
404  return pix;
405 }
406 
407 
408 
409 /*---------------------------------------------------------------------*
410  * Read header information from file *
411  *---------------------------------------------------------------------*/
430 l_int32
431 pixReadHeader(const char *filename,
432  l_int32 *pformat,
433  l_int32 *pw,
434  l_int32 *ph,
435  l_int32 *pbps,
436  l_int32 *pspp,
437  l_int32 *piscmap)
438 {
439 l_int32 format, ret, w, h, d, bps, spp, iscmap;
440 l_int32 type; /* ignored */
441 FILE *fp;
442 PIX *pix;
443 
444  PROCNAME("pixReadHeader");
445 
446  if (pw) *pw = 0;
447  if (ph) *ph = 0;
448  if (pbps) *pbps = 0;
449  if (pspp) *pspp = 0;
450  if (piscmap) *piscmap = 0;
451  if (pformat) *pformat = 0;
452  iscmap = 0; /* init to false */
453  if (!filename)
454  return ERROR_INT("filename not defined", procName, 1);
455 
456  if ((fp = fopenReadStream(filename)) == NULL)
457  return ERROR_INT("image file not found", procName, 1);
458  findFileFormatStream(fp, &format);
459  fclose(fp);
460 
461  switch (format)
462  {
463  case IFF_BMP: /* cheating: reading the entire file */
464  if ((pix = pixRead(filename)) == NULL)
465  return ERROR_INT( "bmp: pix not read", procName, 1);
466  pixGetDimensions(pix, &w, &h, &d);
467  if (pixGetColormap(pix))
468  iscmap = 1;
469  pixDestroy(&pix);
470  bps = (d == 32) ? 8 : d;
471  spp = (d == 32) ? 3 : 1;
472  break;
473 
474  case IFF_JFIF_JPEG:
475  ret = readHeaderJpeg(filename, &w, &h, &spp, NULL, NULL);
476  bps = 8;
477  if (ret)
478  return ERROR_INT( "jpeg: no header info returned", procName, 1);
479  break;
480 
481  case IFF_PNG:
482  ret = readHeaderPng(filename, &w, &h, &bps, &spp, &iscmap);
483  if (ret)
484  return ERROR_INT( "png: no header info returned", procName, 1);
485  break;
486 
487  case IFF_TIFF:
488  case IFF_TIFF_PACKBITS:
489  case IFF_TIFF_RLE:
490  case IFF_TIFF_G3:
491  case IFF_TIFF_G4:
492  case IFF_TIFF_LZW:
493  case IFF_TIFF_ZIP:
494  /* Reading page 0 by default; possibly redefine format */
495  ret = readHeaderTiff(filename, 0, &w, &h, &bps, &spp, NULL, &iscmap,
496  &format);
497  if (ret)
498  return ERROR_INT( "tiff: no header info returned", procName, 1);
499  break;
500 
501  case IFF_PNM:
502  ret = readHeaderPnm(filename, &w, &h, &d, &type, &bps, &spp);
503  if (ret)
504  return ERROR_INT( "pnm: no header info returned", procName, 1);
505  break;
506 
507  case IFF_GIF: /* cheating: reading the entire file */
508  if ((pix = pixRead(filename)) == NULL)
509  return ERROR_INT( "gif: pix not read", procName, 1);
510  pixGetDimensions(pix, &w, &h, &d);
511  pixDestroy(&pix);
512  iscmap = 1; /* always colormapped; max 256 colors */
513  spp = 1;
514  bps = d;
515  break;
516 
517  case IFF_JP2:
518  ret = readHeaderJp2k(filename, &w, &h, &bps, &spp);
519  break;
520 
521  case IFF_WEBP:
522  if (readHeaderWebP(filename, &w, &h, &spp))
523  return ERROR_INT( "webp: no header info returned", procName, 1);
524  bps = 8;
525  break;
526 
527  case IFF_PS:
528  if (pformat) *pformat = format;
529  return ERROR_INT("PostScript reading is not supported\n", procName, 1);
530 
531  case IFF_LPDF:
532  if (pformat) *pformat = format;
533  return ERROR_INT("Pdf reading is not supported\n", procName, 1);
534 
535  case IFF_SPIX:
536  ret = readHeaderSpix(filename, &w, &h, &bps, &spp, &iscmap);
537  if (ret)
538  return ERROR_INT( "spix: no header info returned", procName, 1);
539  break;
540 
541  case IFF_UNKNOWN:
542  L_ERROR("unknown format in file %s\n", procName, filename);
543  return 1;
544  break;
545  }
546 
547  if (pw) *pw = w;
548  if (ph) *ph = h;
549  if (pbps) *pbps = bps;
550  if (pspp) *pspp = spp;
551  if (piscmap) *piscmap = iscmap;
552  if (pformat) *pformat = format;
553  return 0;
554 }
555 
556 
557 /*---------------------------------------------------------------------*
558  * Format finders *
559  *---------------------------------------------------------------------*/
567 l_int32
568 findFileFormat(const char *filename,
569  l_int32 *pformat)
570 {
571 l_int32 ret;
572 FILE *fp;
573 
574  PROCNAME("findFileFormat");
575 
576  if (!pformat)
577  return ERROR_INT("&format not defined", procName, 1);
578  *pformat = IFF_UNKNOWN;
579  if (!filename)
580  return ERROR_INT("filename not defined", procName, 1);
581 
582  if ((fp = fopenReadStream(filename)) == NULL)
583  return ERROR_INT("image file not found", procName, 1);
584  ret = findFileFormatStream(fp, pformat);
585  fclose(fp);
586  return ret;
587 }
588 
589 
602 l_int32
604  l_int32 *pformat)
605 {
606 l_uint8 firstbytes[12];
607 l_int32 format;
608 
609  PROCNAME("findFileFormatStream");
610 
611  if (!pformat)
612  return ERROR_INT("&format not defined", procName, 1);
613  *pformat = IFF_UNKNOWN;
614  if (!fp)
615  return ERROR_INT("stream not defined", procName, 1);
616 
617  rewind(fp);
618  if (fnbytesInFile(fp) < 12)
619  return ERROR_INT("truncated file", procName, 1);
620 
621  if (fread((char *)&firstbytes, 1, 12, fp) != 12)
622  return ERROR_INT("failed to read first 12 bytes of file", procName, 1);
623  rewind(fp);
624 
625  findFileFormatBuffer(firstbytes, &format);
626  if (format == IFF_TIFF) {
627  findTiffCompression(fp, &format);
628  rewind(fp);
629  }
630  *pformat = format;
631  if (format == IFF_UNKNOWN)
632  return 1;
633  else
634  return 0;
635 }
636 
637 
653 l_int32
654 findFileFormatBuffer(const l_uint8 *buf,
655  l_int32 *pformat)
656 {
657 l_uint16 twobytepw;
658 
659  PROCNAME("findFileFormatBuffer");
660 
661  if (!pformat)
662  return ERROR_INT("&format not defined", procName, 1);
663  *pformat = IFF_UNKNOWN;
664  if (!buf)
665  return ERROR_INT("byte buffer not defined", procName, 0);
666 
667  /* Check the bmp and tiff 2-byte header ids */
668  ((char *)(&twobytepw))[0] = buf[0];
669  ((char *)(&twobytepw))[1] = buf[1];
670 
671  if (convertOnBigEnd16(twobytepw) == BMP_ID) {
672  *pformat = IFF_BMP;
673  return 0;
674  }
675 
676  if (twobytepw == TIFF_BIGEND_ID || twobytepw == TIFF_LITTLEEND_ID) {
677  *pformat = IFF_TIFF;
678  return 0;
679  }
680 
681  /* Check for the p*m 2-byte header ids */
682  if ((buf[0] == 'P' && buf[1] == '4') || /* newer packed */
683  (buf[0] == 'P' && buf[1] == '1')) { /* old ASCII format */
684  *pformat = IFF_PNM;
685  return 0;
686  }
687 
688  if ((buf[0] == 'P' && buf[1] == '5') || /* newer */
689  (buf[0] == 'P' && buf[1] == '2')) { /* old */
690  *pformat = IFF_PNM;
691  return 0;
692  }
693 
694  if ((buf[0] == 'P' && buf[1] == '6') || /* newer */
695  (buf[0] == 'P' && buf[1] == '3')) { /* old */
696  *pformat = IFF_PNM;
697  return 0;
698  }
699 
700  if (buf[0] == 'P' && buf[1] == '7') { /* new arbitrary (PAM) */
701  *pformat = IFF_PNM;
702  return 0;
703  }
704 
705  /* Consider the first 11 bytes of the standard JFIF JPEG header:
706  * - The first two bytes are the most important: 0xffd8.
707  * - The next two bytes are the jfif marker: 0xffe0.
708  * Not all jpeg files have this marker.
709  * - The next two bytes are the header length.
710  * - The next 5 bytes are a null-terminated string.
711  * For JFIF, the string is "JFIF", naturally. For others it
712  * can be "Exif" or just about anything else.
713  * - Because of all this variability, we only check the first
714  * two byte marker. All jpeg files are identified as
715  * IFF_JFIF_JPEG. */
716  if (buf[0] == 0xff && buf[1] == 0xd8) {
717  *pformat = IFF_JFIF_JPEG;
718  return 0;
719  }
720 
721  /* Check for the 8 byte PNG signature (png_signature in png.c):
722  * {137, 80, 78, 71, 13, 10, 26, 10} */
723  if (buf[0] == 137 && buf[1] == 80 && buf[2] == 78 && buf[3] == 71 &&
724  buf[4] == 13 && buf[5] == 10 && buf[6] == 26 && buf[7] == 10) {
725  *pformat = IFF_PNG;
726  return 0;
727  }
728 
729  /* Look for "GIF87a" or "GIF89a" */
730  if (buf[0] == 'G' && buf[1] == 'I' && buf[2] == 'F' && buf[3] == '8' &&
731  (buf[4] == '7' || buf[4] == '9') && buf[5] == 'a') {
732  *pformat = IFF_GIF;
733  return 0;
734  }
735 
736  /* Check for both types of jp2k file */
737  if (strncmp((const char *)buf, (char *)JP2K_CODESTREAM, 4) == 0 ||
738  strncmp((const char *)buf, (char *)JP2K_IMAGE_DATA, 12) == 0) {
739  *pformat = IFF_JP2;
740  return 0;
741  }
742 
743  /* Check for webp */
744  if (buf[0] == 'R' && buf[1] == 'I' && buf[2] == 'F' && buf[3] == 'F' &&
745  buf[8] == 'W' && buf[9] == 'E' && buf[10] == 'B' && buf[11] == 'P') {
746  *pformat = IFF_WEBP;
747  return 0;
748  }
749 
750  /* Check for ps */
751  if (buf[0] == '%' && buf[1] == '!' && buf[2] == 'P' && buf[3] == 'S' &&
752  buf[4] == '-' && buf[5] == 'A' && buf[6] == 'd' && buf[7] == 'o' &&
753  buf[8] == 'b' && buf[9] == 'e') {
754  *pformat = IFF_PS;
755  return 0;
756  }
757 
758  /* Check for pdf */
759  if (buf[0] == '%' && buf[1] == 'P' && buf[2] == 'D' && buf[3] == 'F' &&
760  buf[4] == '-' && buf[5] == '1') {
761  *pformat = IFF_LPDF;
762  return 0;
763  }
764 
765  /* Check for "spix" serialized pix */
766  if (buf[0] == 's' && buf[1] == 'p' && buf[2] == 'i' && buf[3] == 'x') {
767  *pformat = IFF_SPIX;
768  return 0;
769  }
770 
771  /* File format identifier not found; unknown */
772  return 1;
773 }
774 
775 
782 l_int32
784 {
785 l_int32 format;
786 
787  PROCNAME("fileFormatIsTiff");
788 
789  if (!fp)
790  return ERROR_INT("stream not defined", procName, 0);
791 
792  findFileFormatStream(fp, &format);
793  if (format == IFF_TIFF || format == IFF_TIFF_PACKBITS ||
794  format == IFF_TIFF_RLE || format == IFF_TIFF_G3 ||
795  format == IFF_TIFF_G4 || format == IFF_TIFF_LZW ||
796  format == IFF_TIFF_ZIP)
797  return 1;
798  else
799  return 0;
800 }
801 
802 
803 /*---------------------------------------------------------------------*
804  * Read from memory *
805  *---------------------------------------------------------------------*/
826 PIX *
827 pixReadMem(const l_uint8 *data,
828  size_t size)
829 {
830 l_int32 format;
831 PIX *pix;
832 
833  PROCNAME("pixReadMem");
834 
835  if (!data)
836  return (PIX *)ERROR_PTR("data not defined", procName, NULL);
837  if (size < 12)
838  return (PIX *)ERROR_PTR("size < 12", procName, NULL);
839  pix = NULL;
840 
841  findFileFormatBuffer(data, &format);
842  switch (format)
843  {
844  case IFF_BMP:
845  if ((pix = pixReadMemBmp(data, size)) == NULL )
846  return (PIX *)ERROR_PTR( "bmp: no pix returned", procName, NULL);
847  break;
848 
849  case IFF_JFIF_JPEG:
850  if ((pix = pixReadMemJpeg(data, size, 0, 1, NULL, 0)) == NULL)
851  return (PIX *)ERROR_PTR( "jpeg: no pix returned", procName, NULL);
852  break;
853 
854  case IFF_PNG:
855  if ((pix = pixReadMemPng(data, size)) == NULL)
856  return (PIX *)ERROR_PTR("png: no pix returned", procName, NULL);
857  break;
858 
859  case IFF_TIFF:
860  case IFF_TIFF_PACKBITS:
861  case IFF_TIFF_RLE:
862  case IFF_TIFF_G3:
863  case IFF_TIFF_G4:
864  case IFF_TIFF_LZW:
865  case IFF_TIFF_ZIP:
866  /* Reading page 0 by default */
867  if ((pix = pixReadMemTiff(data, size, 0)) == NULL)
868  return (PIX *)ERROR_PTR("tiff: no pix returned", procName, NULL);
869  break;
870 
871  case IFF_PNM:
872  if ((pix = pixReadMemPnm(data, size)) == NULL)
873  return (PIX *)ERROR_PTR("pnm: no pix returned", procName, NULL);
874  break;
875 
876  case IFF_GIF:
877  if ((pix = pixReadMemGif(data, size)) == NULL)
878  return (PIX *)ERROR_PTR("gif: no pix returned", procName, NULL);
879  break;
880 
881  case IFF_JP2:
882  if ((pix = pixReadMemJp2k(data, size, 1, NULL, 0, 0)) == NULL)
883  return (PIX *)ERROR_PTR("jp2k: no pix returned", procName, NULL);
884  break;
885 
886  case IFF_WEBP:
887  if ((pix = pixReadMemWebP(data, size)) == NULL)
888  return (PIX *)ERROR_PTR("webp: no pix returned", procName, NULL);
889  break;
890 
891  case IFF_PS:
892  L_ERROR("PostScript reading is not supported\n", procName);
893  return NULL;
894 
895  case IFF_LPDF:
896  L_ERROR("Pdf reading is not supported\n", procName);
897  return NULL;
898 
899  case IFF_SPIX:
900  if ((pix = pixReadMemSpix(data, size)) == NULL)
901  return (PIX *)ERROR_PTR("spix: no pix returned", procName, NULL);
902  break;
903 
904  case IFF_UNKNOWN:
905  return (PIX *)ERROR_PTR("Unknown format: no pix returned",
906  procName, NULL);
907  break;
908  }
909 
910  /* Set the input format. For tiff reading from memory we lose
911  * the actual input format; for 1 bpp, default to G4. */
912  if (pix) {
913  if (format == IFF_TIFF && pixGetDepth(pix) == 1)
914  format = IFF_TIFF_G4;
915  pixSetInputFormat(pix, format);
916  }
917 
918  return pix;
919 }
920 
921 
947 l_int32
948 pixReadHeaderMem(const l_uint8 *data,
949  size_t size,
950  l_int32 *pformat,
951  l_int32 *pw,
952  l_int32 *ph,
953  l_int32 *pbps,
954  l_int32 *pspp,
955  l_int32 *piscmap)
956 {
957 l_int32 format, ret, w, h, d, bps, spp, iscmap;
958 l_int32 type; /* not used */
959 PIX *pix;
960 
961  PROCNAME("pixReadHeaderMem");
962 
963  if (pw) *pw = 0;
964  if (ph) *ph = 0;
965  if (pbps) *pbps = 0;
966  if (pspp) *pspp = 0;
967  if (piscmap) *piscmap = 0;
968  if (pformat) *pformat = 0;
969  iscmap = 0; /* init to false */
970  if (!data)
971  return ERROR_INT("data not defined", procName, 1);
972  if (size < 8)
973  return ERROR_INT("size < 8", procName, 1);
974 
975  findFileFormatBuffer(data, &format);
976 
977  switch (format)
978  {
979  case IFF_BMP: /* cheating: read the pix */
980  if ((pix = pixReadMemBmp(data, size)) == NULL)
981  return ERROR_INT( "bmp: pix not read", procName, 1);
982  pixGetDimensions(pix, &w, &h, &d);
983  pixDestroy(&pix);
984  bps = (d == 32) ? 8 : d;
985  spp = (d == 32) ? 3 : 1;
986  break;
987 
988  case IFF_JFIF_JPEG:
989  ret = readHeaderMemJpeg(data, size, &w, &h, &spp, NULL, NULL);
990  bps = 8;
991  if (ret)
992  return ERROR_INT( "jpeg: no header info returned", procName, 1);
993  break;
994 
995  case IFF_PNG:
996  ret = readHeaderMemPng(data, size, &w, &h, &bps, &spp, &iscmap);
997  if (ret)
998  return ERROR_INT( "png: no header info returned", procName, 1);
999  break;
1000 
1001  case IFF_TIFF:
1002  case IFF_TIFF_PACKBITS:
1003  case IFF_TIFF_RLE:
1004  case IFF_TIFF_G3:
1005  case IFF_TIFF_G4:
1006  case IFF_TIFF_LZW:
1007  case IFF_TIFF_ZIP:
1008  /* Reading page 0 by default; possibly redefine format */
1009  ret = readHeaderMemTiff(data, size, 0, &w, &h, &bps, &spp,
1010  NULL, &iscmap, &format);
1011  if (ret)
1012  return ERROR_INT( "tiff: no header info returned", procName, 1);
1013  break;
1014 
1015  case IFF_PNM:
1016  ret = readHeaderMemPnm(data, size, &w, &h, &d, &type, &bps, &spp);
1017  if (ret)
1018  return ERROR_INT( "pnm: no header info returned", procName, 1);
1019  break;
1020 
1021  case IFF_GIF: /* cheating: read the pix */
1022  if ((pix = pixReadMemGif(data, size)) == NULL)
1023  return ERROR_INT( "gif: pix not read", procName, 1);
1024  pixGetDimensions(pix, &w, &h, &d);
1025  pixDestroy(&pix);
1026  iscmap = 1; /* always colormapped; max 256 colors */
1027  spp = 1;
1028  bps = d;
1029  break;
1030 
1031  case IFF_JP2:
1032  ret = readHeaderMemJp2k(data, size, &w, &h, &bps, &spp);
1033  break;
1034 
1035  case IFF_WEBP:
1036  bps = 8;
1037  ret = readHeaderMemWebP(data, size, &w, &h, &spp);
1038  break;
1039 
1040  case IFF_PS:
1041  if (pformat) *pformat = format;
1042  return ERROR_INT("PostScript reading is not supported\n", procName, 1);
1043 
1044  case IFF_LPDF:
1045  if (pformat) *pformat = format;
1046  return ERROR_INT("Pdf reading is not supported\n", procName, 1);
1047 
1048  case IFF_SPIX:
1049  ret = sreadHeaderSpix((l_uint32 *)data, &w, &h, &bps,
1050  &spp, &iscmap);
1051  if (ret)
1052  return ERROR_INT( "pnm: no header info returned", procName, 1);
1053  break;
1054 
1055  case IFF_UNKNOWN:
1056  return ERROR_INT("unknown format; no data returned", procName, 1);
1057  break;
1058  }
1059 
1060  if (pw) *pw = w;
1061  if (ph) *ph = h;
1062  if (pbps) *pbps = bps;
1063  if (pspp) *pspp = spp;
1064  if (piscmap) *piscmap = iscmap;
1065  if (pformat) *pformat = format;
1066  return 0;
1067 }
1068 
1069 
1070 /*---------------------------------------------------------------------*
1071  * Output image file information *
1072  *---------------------------------------------------------------------*/
1073 extern const char *ImageFileFormatExtensions[];
1074 
1093 l_int32
1094 writeImageFileInfo(const char *filename,
1095  FILE *fpout,
1096  l_int32 headeronly)
1097 {
1098 char *text;
1099 l_int32 w, h, d, wpl, count, npages, color;
1100 l_int32 format, bps, spp, iscmap, xres, yres, transparency;
1101 FILE *fpin;
1102 PIX *pix, *pixt;
1103 PIXCMAP *cmap;
1104 
1105  PROCNAME("writeImageFileInfo");
1106 
1107  if (!filename)
1108  return ERROR_INT("filename not defined", procName, 1);
1109  if (!fpout)
1110  return ERROR_INT("stream not defined", procName, 1);
1111 
1112  /* Read the header */
1113  if (pixReadHeader(filename, &format, &w, &h, &bps, &spp, &iscmap)) {
1114  L_ERROR("failure to read header of %s\n", procName, filename);
1115  return 1;
1116  }
1117  fprintf(fpout, "===============================================\n"
1118  "Reading the header:\n");
1119  fprintf(fpout, " input image format type: %s\n",
1120  ImageFileFormatExtensions[format]);
1121  fprintf(fpout, " w = %d, h = %d, bps = %d, spp = %d, iscmap = %d\n",
1122  w, h, bps, spp, iscmap);
1123 
1124  findFileFormat(filename, &format);
1125  if (format == IFF_JP2) {
1126  fpin = lept_fopen(filename, "rb");
1127  fgetJp2kResolution(fpin, &xres, &yres);
1128  fclose(fpin);
1129  fprintf(fpout, " xres = %d, yres = %d\n", xres, yres);
1130  } else if (format == IFF_PNG) {
1131  fpin = lept_fopen(filename, "rb");
1132  fgetPngResolution(fpin, &xres, &yres);
1133  fclose(fpin);
1134  fprintf(fpout, " xres = %d, yres = %d\n", xres, yres);
1135  if (iscmap) {
1136  fpin = lept_fopen(filename, "rb");
1137  fgetPngColormapInfo(fpin, &cmap, &transparency);
1138  fclose(fpin);
1139  if (transparency)
1140  fprintf(fpout, " colormap has transparency\n");
1141  else
1142  fprintf(fpout, " colormap does not have transparency\n");
1143  pixcmapWriteStream(fpout, cmap);
1144  pixcmapDestroy(&cmap);
1145  }
1146  } else if (format == IFF_JFIF_JPEG) {
1147  fpin = lept_fopen(filename, "rb");
1148  fgetJpegResolution(fpin, &xres, &yres);
1149  fclose(fpin);
1150  fprintf(fpout, " xres = %d, yres = %d\n", xres, yres);
1151  }
1152 
1153  if (headeronly)
1154  return 0;
1155 
1156  /* Read the full image. Note that when we read an image that
1157  * has transparency in a colormap, we convert it to RGBA. */
1158  fprintf(fpout, "===============================================\n"
1159  "Reading the full image:\n");
1160 
1161  /* Preserve 16 bpp if the format is png */
1162  if (format == IFF_PNG && bps == 16)
1164 
1165  if ((pix = pixRead(filename)) == NULL) {
1166  L_ERROR("failure to read full image of %s\n", procName, filename);
1167  return 1;
1168  }
1169 
1170  format = pixGetInputFormat(pix);
1171  pixGetDimensions(pix, &w, &h, &d);
1172  wpl = pixGetWpl(pix);
1173  spp = pixGetSpp(pix);
1174  fprintf(fpout, " input image format type: %s\n",
1175  ImageFileFormatExtensions[format]);
1176  fprintf(fpout, " w = %d, h = %d, d = %d, spp = %d, wpl = %d\n",
1177  w, h, d, spp, wpl);
1178  fprintf(fpout, " xres = %d, yres = %d\n",
1179  pixGetXRes(pix), pixGetYRes(pix));
1180 
1181  text = pixGetText(pix);
1182  if (text) /* not null */
1183  fprintf(fpout, " text: %s\n", text);
1184 
1185  cmap = pixGetColormap(pix);
1186  if (cmap) {
1187  pixcmapHasColor(cmap, &color);
1188  if (color)
1189  fprintf(fpout, " colormap exists and has color values:");
1190  else
1191  fprintf(fpout, " colormap exists and has only gray values:");
1192  pixcmapWriteStream(fpout, pixGetColormap(pix));
1193  }
1194  else
1195  fprintf(fpout, " colormap does not exist\n");
1196 
1197  if (format == IFF_TIFF || format == IFF_TIFF_G4 ||
1198  format == IFF_TIFF_G3 || format == IFF_TIFF_PACKBITS) {
1199  fprintf(fpout, " Tiff header information:\n");
1200  fpin = lept_fopen(filename, "rb");
1201  tiffGetCount(fpin, &npages);
1202  lept_fclose(fpin);
1203  if (npages == 1)
1204  fprintf(fpout, " One page in file\n");
1205  else
1206  fprintf(fpout, " %d pages in file\n", npages);
1207  fprintTiffInfo(fpout, filename);
1208  }
1209 
1210  if (d == 1) {
1211  pixCountPixels(pix, &count, NULL);
1212  pixGetDimensions(pix, &w, &h, NULL);
1213  fprintf(fpout, " 1 bpp: foreground pixel fraction ON/Total = %g\n",
1214  (l_float32)count / (l_float32)(w * h));
1215  }
1216  fprintf(fpout, "===============================================\n");
1217 
1218  /* If there is an alpha component, visualize it. Note that when
1219  * alpha == 0, the rgb layer is transparent. We visualize the
1220  * result when a white background is visible through the
1221  * transparency layer. */
1222  if (pixGetSpp(pix) == 4) {
1223  pixt = pixDisplayLayersRGBA(pix, 0xffffff00, 600.0);
1224  pixDisplay(pixt, 100, 100);
1225  pixDestroy(&pixt);
1226  }
1227 
1228  if (format == IFF_PNG && bps == 16)
1229  l_pngSetReadStrip16To8(1); /* return to default if format is png */
1230 
1231  pixDestroy(&pix);
1232  return 0;
1233 }
1234 
1235 
1236 /*---------------------------------------------------------------------*
1237  * Test function for I/O with different formats *
1238  *---------------------------------------------------------------------*/
1261 l_int32
1262 ioFormatTest(const char *filename)
1263 {
1264 l_int32 w, h, d, depth, equal, problems;
1265 l_float32 diff;
1266 BOX *box;
1267 PIX *pixs, *pixc, *pix1, *pix2;
1268 PIXCMAP *cmap;
1269 
1270  PROCNAME("ioFormatTest");
1271 
1272  if (!filename)
1273  return ERROR_INT("filename not defined", procName, 1);
1274 
1275  /* Read the input file and limit the size */
1276  if ((pix1 = pixRead(filename)) == NULL)
1277  return ERROR_INT("pix1 not made", procName, 1);
1278  pixGetDimensions(pix1, &w, &h, NULL);
1279  if (w > 250 && h > 250) { /* take the central 250 x 250 region */
1280  box = boxCreate(w / 2 - 125, h / 2 - 125, 250, 250);
1281  pixs = pixClipRectangle(pix1, box, NULL);
1282  boxDestroy(&box);
1283  } else {
1284  pixs = pixClone(pix1);
1285  }
1286  pixDestroy(&pix1);
1287 
1288  lept_mkdir("lept/format");
1289 
1290  /* Note that the reader automatically removes colormaps
1291  * from 1 bpp BMP images, but not from 8 bpp BMP images.
1292  * Therefore, if our 8 bpp image initially doesn't have a
1293  * colormap, we are going to need to remove it from any
1294  * pix read from a BMP file. */
1295  pixc = pixClone(pixs); /* laziness */
1296 
1297  /* This does not test the alpha layer pixels, because most
1298  * formats don't support it. Remove any alpha. */
1299  if (pixGetSpp(pixc) == 4)
1300  pixSetSpp(pixc, 3);
1301  cmap = pixGetColormap(pixc); /* colormap; can be NULL */
1302  d = pixGetDepth(pixc);
1303 
1304  problems = FALSE;
1305 
1306  /* ----------------------- BMP -------------------------- */
1307 
1308  /* BMP works for 1, 2, 4, 8 and 32 bpp images.
1309  * It always writes colormaps for 1 and 8 bpp, so we must
1310  * remove it after readback if the input image doesn't have
1311  * a colormap. Although we can write/read 2 bpp BMP, nobody
1312  * else can read them! */
1313  if (d == 1 || d == 8) {
1314  L_INFO("write/read bmp\n", procName);
1315  pixWrite(FILE_BMP, pixc, IFF_BMP);
1316  pix1 = pixRead(FILE_BMP);
1317  if (!cmap)
1319  else
1320  pix2 = pixClone(pix1);
1321  pixEqual(pixc, pix2, &equal);
1322  if (!equal) {
1323  L_INFO(" **** bad bmp image: d = %d ****\n", procName, d);
1324  problems = TRUE;
1325  }
1326  pixDestroy(&pix1);
1327  pixDestroy(&pix2);
1328  }
1329 
1330  if (d == 2 || d == 4 || d == 32) {
1331  L_INFO("write/read bmp\n", procName);
1332  pixWrite(FILE_BMP, pixc, IFF_BMP);
1333  pix1 = pixRead(FILE_BMP);
1334  pixEqual(pixc, pix1, &equal);
1335  if (!equal) {
1336  L_INFO(" **** bad bmp image: d = %d ****\n", procName, d);
1337  problems = TRUE;
1338  }
1339  pixDestroy(&pix1);
1340  }
1341 
1342  /* ----------------------- PNG -------------------------- */
1343 #if HAVE_LIBPNG
1344  /* PNG works for all depths, but here, because we strip
1345  * 16 --> 8 bpp on reading, we don't test png for 16 bpp. */
1346  if (d != 16) {
1347  L_INFO("write/read png\n", procName);
1348  pixWrite(FILE_PNG, pixc, IFF_PNG);
1349  pix1 = pixRead(FILE_PNG);
1350  pixEqual(pixc, pix1, &equal);
1351  if (!equal) {
1352  L_INFO(" **** bad png image: d = %d ****\n", procName, d);
1353  problems = TRUE;
1354  }
1355  pixDestroy(&pix1);
1356  }
1357 #endif /* HAVE_LIBPNG */
1358 
1359  /* ----------------------- TIFF -------------------------- */
1360 #if HAVE_LIBTIFF
1361  /* TIFF works for 1, 2, 4, 8, 16 and 32 bpp images.
1362  * Because 8 bpp tiff always writes 256 entry colormaps, the
1363  * colormap sizes may be different for 8 bpp images with
1364  * colormap; we are testing if the image content is the same.
1365  * Likewise, the 2 and 4 bpp tiff images with colormaps
1366  * have colormap sizes 4 and 16, rsp. This test should
1367  * work properly on the content, regardless of the number
1368  * of color entries in pixc. */
1369 
1370  /* tiff uncompressed works for all pixel depths */
1371  L_INFO("write/read uncompressed tiff\n", procName);
1372  pixWrite(FILE_TIFF, pixc, IFF_TIFF);
1373  pix1 = pixRead(FILE_TIFF);
1374  pixEqual(pixc, pix1, &equal);
1375  if (!equal) {
1376  L_INFO(" **** bad tiff uncompressed image: d = %d ****\n",
1377  procName, d);
1378  problems = TRUE;
1379  }
1380  pixDestroy(&pix1);
1381 
1382  /* tiff lzw works for all pixel depths */
1383  L_INFO("write/read lzw compressed tiff\n", procName);
1384  pixWrite(FILE_LZW, pixc, IFF_TIFF_LZW);
1385  pix1 = pixRead(FILE_LZW);
1386  pixEqual(pixc, pix1, &equal);
1387  if (!equal) {
1388  L_INFO(" **** bad tiff lzw compressed image: d = %d ****\n",
1389  procName, d);
1390  problems = TRUE;
1391  }
1392  pixDestroy(&pix1);
1393 
1394  /* tiff adobe deflate (zip) works for all pixel depths */
1395  L_INFO("write/read zip compressed tiff\n", procName);
1396  pixWrite(FILE_ZIP, pixc, IFF_TIFF_ZIP);
1397  pix1 = pixRead(FILE_ZIP);
1398  pixEqual(pixc, pix1, &equal);
1399  if (!equal) {
1400  L_INFO(" **** bad tiff zip compressed image: d = %d ****\n",
1401  procName, d);
1402  problems = TRUE;
1403  }
1404  pixDestroy(&pix1);
1405 
1406  /* tiff g4, g3, rle and packbits work for 1 bpp */
1407  if (d == 1) {
1408  L_INFO("write/read g4 compressed tiff\n", procName);
1409  pixWrite(FILE_G4, pixc, IFF_TIFF_G4);
1410  pix1 = pixRead(FILE_G4);
1411  pixEqual(pixc, pix1, &equal);
1412  if (!equal) {
1413  L_INFO(" **** bad tiff g4 image ****\n", procName);
1414  problems = TRUE;
1415  }
1416  pixDestroy(&pix1);
1417 
1418  L_INFO("write/read g3 compressed tiff\n", procName);
1419  pixWrite(FILE_G3, pixc, IFF_TIFF_G3);
1420  pix1 = pixRead(FILE_G3);
1421  pixEqual(pixc, pix1, &equal);
1422  if (!equal) {
1423  L_INFO(" **** bad tiff g3 image ****\n", procName);
1424  problems = TRUE;
1425  }
1426  pixDestroy(&pix1);
1427 
1428  L_INFO("write/read rle compressed tiff\n", procName);
1429  pixWrite(FILE_RLE, pixc, IFF_TIFF_RLE);
1430  pix1 = pixRead(FILE_RLE);
1431  pixEqual(pixc, pix1, &equal);
1432  if (!equal) {
1433  L_INFO(" **** bad tiff rle image: d = %d ****\n", procName, d);
1434  problems = TRUE;
1435  }
1436  pixDestroy(&pix1);
1437 
1438  L_INFO("write/read packbits compressed tiff\n", procName);
1439  pixWrite(FILE_PB, pixc, IFF_TIFF_PACKBITS);
1440  pix1 = pixRead(FILE_PB);
1441  pixEqual(pixc, pix1, &equal);
1442  if (!equal) {
1443  L_INFO(" **** bad tiff packbits image: d = %d ****\n",
1444  procName, d);
1445  problems = TRUE;
1446  }
1447  pixDestroy(&pix1);
1448  }
1449 #endif /* HAVE_LIBTIFF */
1450 
1451  /* ----------------------- PNM -------------------------- */
1452 
1453  /* pnm works for 1, 2, 4, 8, 16 and 32 bpp.
1454  * pnm doesn't have colormaps, so when we write colormapped
1455  * pix out as pnm, the colormap is removed. Thus for the test,
1456  * we must remove the colormap from pixc before testing. */
1457  L_INFO("write/read pnm\n", procName);
1458  pixWrite(FILE_PNM, pixc, IFF_PNM);
1459  pix1 = pixRead(FILE_PNM);
1460  if (cmap)
1462  else
1463  pix2 = pixClone(pixc);
1464  pixEqual(pix1, pix2, &equal);
1465  if (!equal) {
1466  L_INFO(" **** bad pnm image: d = %d ****\n", procName, d);
1467  problems = TRUE;
1468  }
1469  pixDestroy(&pix1);
1470  pixDestroy(&pix2);
1471 
1472  /* ----------------------- GIF -------------------------- */
1473 #if HAVE_LIBGIF
1474  /* GIF works for only 1 and 8 bpp, colormapped */
1475  if (d != 8 || !cmap)
1476  pix1 = pixConvertTo8(pixc, 1);
1477  else
1478  pix1 = pixClone(pixc);
1479  L_INFO("write/read gif\n", procName);
1480  pixWrite(FILE_GIF, pix1, IFF_GIF);
1481  pix2 = pixRead(FILE_GIF);
1482  pixEqual(pix1, pix2, &equal);
1483  if (!equal) {
1484  L_INFO(" **** bad gif image: d = %d ****\n", procName, d);
1485  problems = TRUE;
1486  }
1487  pixDestroy(&pix1);
1488  pixDestroy(&pix2);
1489 #endif /* HAVE_LIBGIF */
1490 
1491  /* ----------------------- JPEG ------------------------- */
1492 #if HAVE_LIBJPEG
1493  /* JPEG works for only 8 bpp gray and rgb */
1494  if (cmap || d > 8)
1495  pix1 = pixConvertTo32(pixc);
1496  else
1497  pix1 = pixConvertTo8(pixc, 0);
1498  depth = pixGetDepth(pix1);
1499  L_INFO("write/read jpeg\n", procName);
1500  pixWrite(FILE_JPG, pix1, IFF_JFIF_JPEG);
1501  pix2 = pixRead(FILE_JPG);
1502  if (depth == 8) {
1503  pixCompareGray(pix1, pix2, L_COMPARE_ABS_DIFF, 0, NULL, &diff,
1504  NULL, NULL);
1505  } else {
1506  pixCompareRGB(pix1, pix2, L_COMPARE_ABS_DIFF, 0, NULL, &diff,
1507  NULL, NULL);
1508  }
1509  if (diff > 8.0) {
1510  L_INFO(" **** bad jpeg image: d = %d, diff = %5.2f ****\n",
1511  procName, depth, diff);
1512  problems = TRUE;
1513  }
1514  pixDestroy(&pix1);
1515  pixDestroy(&pix2);
1516 #endif /* HAVE_LIBJPEG */
1517 
1518  /* ----------------------- WEBP ------------------------- */
1519 #if HAVE_LIBWEBP
1520  /* WEBP works for rgb and rgba */
1521  if (cmap || d <= 16)
1522  pix1 = pixConvertTo32(pixc);
1523  else
1524  pix1 = pixClone(pixc);
1525  depth = pixGetDepth(pix1);
1526  L_INFO("write/read webp\n", procName);
1527  pixWrite(FILE_WEBP, pix1, IFF_WEBP);
1528  pix2 = pixRead(FILE_WEBP);
1529  pixCompareRGB(pix1, pix2, L_COMPARE_ABS_DIFF, 0, NULL, &diff, NULL, NULL);
1530  if (diff > 5.0) {
1531  L_INFO(" **** bad webp image: d = %d, diff = %5.2f ****\n",
1532  procName, depth, diff);
1533  problems = TRUE;
1534  }
1535  pixDestroy(&pix1);
1536  pixDestroy(&pix2);
1537 #endif /* HAVE_LIBWEBP */
1538 
1539  /* ----------------------- JP2K ------------------------- */
1540 #if HAVE_LIBJP2K
1541  /* JP2K works for only 8 bpp gray, rgb and rgba */
1542  if (cmap || d > 8)
1543  pix1 = pixConvertTo32(pixc);
1544  else
1545  pix1 = pixConvertTo8(pixc, 0);
1546  depth = pixGetDepth(pix1);
1547  L_INFO("write/read jp2k\n", procName);
1548  pixWrite(FILE_JP2K, pix1, IFF_JP2);
1549  pix2 = pixRead(FILE_JP2K);
1550  if (depth == 8) {
1551  pixCompareGray(pix1, pix2, L_COMPARE_ABS_DIFF, 0, NULL, &diff,
1552  NULL, NULL);
1553  } else {
1554  pixCompareRGB(pix1, pix2, L_COMPARE_ABS_DIFF, 0, NULL, &diff,
1555  NULL, NULL);
1556  }
1557  fprintf(stderr, "diff = %7.3f\n", diff);
1558  if (diff > 7.0) {
1559  L_INFO(" **** bad jp2k image: d = %d, diff = %5.2f ****\n",
1560  procName, depth, diff);
1561  problems = TRUE;
1562  }
1563  pixDestroy(&pix1);
1564  pixDestroy(&pix2);
1565 #endif /* HAVE_LIBJP2K */
1566 
1567  if (problems == FALSE)
1568  L_INFO("All formats read and written OK!\n", procName);
1569 
1570  pixDestroy(&pixc);
1571  pixDestroy(&pixs);
1572  return problems;
1573 }
l_int32 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 * pixRemoveColormap(PIX *pixs, l_int32 type)
pixRemoveColormap()
Definition: pixconv.c:322
PIX * pixReadStreamTiff(FILE *fp, l_int32 n)
pixReadStreamTiff()
Definition: tiffio.c:408
l_int32 lept_mkdir(const char *subdir)
lept_mkdir()
Definition: utils2.c:1880
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_int32 findFileFormatStream(FILE *fp, l_int32 *pformat)
findFileFormatStream()
Definition: readfile.c:603
l_int32 readHeaderPng(const char *filename, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp, l_int32 *piscmap)
readHeaderPng()
Definition: pngio.c:517
l_int32 writeImageFileInfo(const char *filename, FILE *fpout, l_int32 headeronly)
writeImageFileInfo()
Definition: readfile.c:1094
PIXA * pixaReadFiles(const char *dirname, const char *substr)
pixaReadFiles()
Definition: readfile.c:123
PIX * pixReadStreamBmp(FILE *fp)
pixReadStreamBmp()
Definition: bmpio.c:89
PIXA * pixaCreate(l_int32 n)
pixaCreate()
Definition: pixabasic.c:161
Definition: pix.h:704
PIX * pixReadIndexed(SARRAY *sa, l_int32 index)
pixReadIndexed()
Definition: readfile.c:276
PIX * pixConvertTo8(PIX *pixs, l_int32 cmapflag)
pixConvertTo8()
Definition: pixconv.c:3041
PIX * pixReadMemBmp(const l_uint8 *cdata, size_t size)
pixReadMemBmp()
Definition: bmpio.c:119
l_int32 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:948
void pixcmapDestroy(PIXCMAP **pcmap)
pixcmapDestroy()
Definition: colormap.c:263
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:1277
l_int32 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
PIX * pixReadMem(const l_uint8 *data, size_t size)
pixReadMem()
Definition: readfile.c:827
PIX * pixClipRectangle(PIX *pixs, BOX *box, BOX **pboxc)
pixClipRectangle()
Definition: pix5.c:1013
l_int32 findFileFormat(const char *filename, l_int32 *pformat)
findFileFormat()
Definition: readfile.c:568
l_int32 readHeaderTiff(const char *filename, l_int32 n, l_int32 *pwidth, l_int32 *pheight, l_int32 *pbps, l_int32 *pspp, l_int32 *pres, l_int32 *pcmap, l_int32 *pformat)
readHeaderTiff()
Definition: tiffio.c:1621
Definition: array.h:116
l_int32 lept_fclose(FILE *fp)
lept_fclose()
Definition: utils2.c:1804
PIX * pixReadStreamPnm(FILE *fp)
pixReadStreamPnm()
Definition: pnmio.c:145
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:983
l_int32 fprintTiffInfo(FILE *fpout, const char *tiffile)
fprintTiffInfo()
Definition: tiffio.c:1443
l_int32 pixcmapWriteStream(FILE *fp, PIXCMAP *cmap)
pixcmapWriteStream()
Definition: colormap.c:1715
static const l_int32 L_INSERT
Definition: pix.h:710
l_int32 tiffGetCount(FILE *fp, l_int32 *pn)
tiffGetCount()
Definition: tiffio.c:1476
l_int32 pixaAddPix(PIXA *pixa, PIX *pix, l_int32 copyflag)
pixaAddPix()
Definition: pixabasic.c:493
l_int32 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:431
PIX * pixReadStreamJpeg(FILE *fp, l_int32 cmapflag, l_int32 reduction, l_int32 *pnwarn, l_int32 hint)
pixReadStreamJpeg()
Definition: jpegio.c:269
l_int32 readHeaderJpeg(const char *filename, l_int32 *pw, l_int32 *ph, l_int32 *pspp, l_int32 *pycck, l_int32 *pcmyk)
readHeaderJpeg()
Definition: jpegio.c:506
size_t fnbytesInFile(FILE *fp)
fnbytesInFile()
Definition: utils2.c:1433
PIX * pixReadStreamSpix(FILE *fp)
pixReadStreamSpix()
Definition: spixio.c:88
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
Definition: sarray1.c:675
l_int32 ioFormatTest(const char *filename)
ioFormatTest()
Definition: readfile.c:1262
PIX * pixClone(PIX *pixs)
pixClone()
Definition: pix1.c:517
l_int32 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
l_int32 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
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:545
PIX * pixReadStream(FILE *fp, l_int32 hint)
pixReadStream()
Definition: readfile.c:317
SARRAY * getSortedPathnamesInDirectory(const char *dirname, const char *substr, l_int32 first, l_int32 nfiles)
getSortedPathnamesInDirectory()
Definition: sarray1.c:1710
Definition: pix.h:454
PIX * pixReadMemTiff(const l_uint8 *cdata, size_t size, l_int32 n)
pixReadMemTiff()
Definition: tiffio.c:2429
PIXA * pixaReadFilesSA(SARRAY *sa)
pixaReadFilesSA()
Definition: readfile.c:150
l_int32 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_int32 fileFormatIsTiff(FILE *fp)
fileFormatIsTiff()
Definition: readfile.c:783
l_int32 findTiffCompression(FILE *fp, l_int32 *pcomptype)
findTiffCompression()
Definition: tiffio.c:1871
FILE * fopenReadStream(const char *filename)
fopenReadStream()
Definition: utils2.c:1593
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
Definition: sarray1.c:615
PIX * pixRead(const char *filename)
pixRead()
Definition: readfile.c:189
FILE * lept_fopen(const char *filename, const char *mode)
lept_fopen()
Definition: utils2.c:1774
char * pixGetText(PIX *pix)
pixGetText()
Definition: pix1.c:1442
l_int32 pixEqual(PIX *pix1, PIX *pix2, l_int32 *psame)
pixEqual()
Definition: compare.c:150
l_int32 readHeaderJp2k(const char *filename, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp)
readHeaderJp2k()
Definition: jp2kheader.c:75
PIX * pixReadWithHint(const char *filename, l_int32 hint)
pixReadWithHint()
Definition: readfile.c:225
PIX * pixDisplayLayersRGBA(PIX *pixs, l_uint32 val, l_int32 maxw)
pixDisplayLayersRGBA()
Definition: pix2.c:2264
l_int32 findFileFormatBuffer(const l_uint8 *buf, l_int32 *pformat)
findFileFormatBuffer()
Definition: readfile.c:654
Definition: pix.h:134
l_int32 readHeaderMemTiff(const l_uint8 *cdata, size_t size, l_int32 n, l_int32 *pwidth, l_int32 *pheight, l_int32 *pbps, l_int32 *pspp, l_int32 *pres, l_int32 *pcmap, l_int32 *pformat)
readHeaderMemTiff()
Definition: tiffio.c:1743
l_int32 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
void boxDestroy(BOX **pbox)
boxDestroy()
Definition: boxbasic.c:277
PIX * pixReadMemPng(const l_uint8 *filedata, size_t filesize)
pixReadMemPng()
Definition: pngio.c:1517
l_int32 pixGetDimensions(PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
Definition: pix1.c:1052
l_int32 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:618
l_int32 pixCountPixels(PIX *pixs, l_int32 *pcount, l_int32 *tab8)
pixCountPixels()
Definition: pix3.c:1824
Definition: pix.h:480
BOX * boxCreate(l_int32 x, l_int32 y, l_int32 w, l_int32 h)
boxCreate()
Definition: boxbasic.c:164
l_int32 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_int32 pixSetText(PIX *pix, const char *textstring)
pixSetText()
Definition: pix1.c:1466
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
Definition: sarray1.c:349
l_int32 pixcmapHasColor(PIXCMAP *cmap, l_int32 *pcolor)
pixcmapHasColor()
Definition: colormap.c:960
l_int32 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:1030