gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master 1856ea9: JPEG input output now in libraries


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 1856ea9: JPEG input output now in libraries
Date: Sat, 17 Mar 2018 21:52:38 -0400 (EDT)

branch: master
commit 1856ea963257eb5fb066f9456af86e7df9a726f7
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    JPEG input output now in libraries
    
    The functions to read and write JPEG files have been moved to the library
    and are now generally available to all programs. Previously, reading and
    writing JPEG files were only available within the ConvertType program.
---
 NEWS                         |  6 ++-
 bin/convertt/Makefile.am     |  4 +-
 bin/convertt/convertt.c      | 80 +++++++-----------------------------
 bin/convertt/jpeg.h          | 38 -----------------
 bin/convertt/ui.c            | 40 ++++++++----------
 doc/gnuastro.texi            | 71 ++++++++++++++++++++++++++++----
 lib/Makefile.am              | 10 ++---
 lib/array.c                  | 11 ++++-
 lib/gnuastro/jpeg.h          | 75 ++++++++++++++++++++++++++++++++++
 {bin/convertt => lib}/jpeg.c | 97 ++++++++++++++++++++++++++++----------------
 lib/tiff.c                   |  9 ++--
 11 files changed, 262 insertions(+), 179 deletions(-)

diff --git a/NEWS b/NEWS
index 9f464f6..0c2967b 100644
--- a/NEWS
+++ b/NEWS
@@ -28,10 +28,14 @@ GNU Astronomy Utilities NEWS                          -*- 
outline -*-
   Libraries:
     gal_array_read: read from file with all known formats (FITS, TIFF, etc).
     gal_array_read_to_type: similar to `gal_array_read', but to given type.
+    gal_jpeg_name_is_jpeg: Returns 1 if given filename is JPEG.
+    gal_jpeg_suffix_is_jpeg: Returns 1 if given suffix is JPEG.
+    gal_jpeg_read: Reads input JPEG image into `gal_data_t'.
+    gal_jpeg_write: Writes a `gal_data_t' into a JPEG file.
     gal_tiff_name_is_tiff: check if name contains a TIFF suffix.
     gal_tiff_suffix_is_tiff: check if suffix is a TIFF suffix.
     gal_tiff_dir_string_read: convert a string to a TIFF directory number.
-    gal_tiff_read: Read the contents of a TIFF `directory' to `gal_data_t'.
+    gal_tiff_read: Read the contents of a TIFF "directory" to `gal_data_t'.
 
 ** Removed features
 
diff --git a/bin/convertt/Makefile.am b/bin/convertt/Makefile.am
index 0b851f6..807597b 100644
--- a/bin/convertt/Makefile.am
+++ b/bin/convertt/Makefile.am
@@ -30,9 +30,9 @@ bin_PROGRAMS = astconvertt
 
 astconvertt_LDADD = -lgnuastro
 
-astconvertt_SOURCES = main.c ui.c convertt.c eps.c jpeg.c
+astconvertt_SOURCES = main.c ui.c convertt.c eps.c
 
-EXTRA_DIST = main.h authors-cite.h args.h ui.h convertt.h eps.h jpeg.h
+EXTRA_DIST = main.h authors-cite.h args.h ui.h convertt.h eps.h
 
 
 
diff --git a/bin/convertt/convertt.c b/bin/convertt/convertt.c
index 72b1d54..81e3ddd 100644
--- a/bin/convertt/convertt.c
+++ b/bin/convertt/convertt.c
@@ -31,6 +31,7 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 
 #include <gnuastro/txt.h>
 #include <gnuastro/fits.h>
+#include <gnuastro/jpeg.h>
 #include <gnuastro/arithmetic.h>
 
 #include <gnuastro-internal/timing.h>
@@ -39,7 +40,6 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #include "main.h"
 
 #include "eps.h"
-#include "jpeg.h"
 
 
 
@@ -165,57 +165,6 @@ convertt_truncate(struct converttparams *p)
 
 
 /**************************************************************/
-/**************       Save text and FITS        ***************/
-/**************************************************************/
-static void
-save_with_gnuastro_lib(struct converttparams *p)
-{
-  gal_data_t *channel;
-
-  /* Determine the type. */
-  switch(p->outformat)
-    {
-
-    /* FITS: a FITS file can have many extensions (channels). */
-    case OUT_FORMAT_FITS:
-      for(channel=p->chll; channel!=NULL; channel=channel->next)
-        gal_fits_img_write(channel, p->cp.output, NULL, PROGRAM_NAME);
-      break;
-
-    /* Plain text: only one channel is acceptable. */
-    case OUT_FORMAT_TXT:
-      gal_checkset_writable_remove(p->cp.output, 0, p->cp.dontdelete);
-      gal_txt_write(p->chll, NULL, p->cp.output);
-      break;
-
-
-    /* Not recognized. */
-    default:
-      error(EXIT_FAILURE, 0, "%s: a bug! output format code `%d' not "
-            "recognized", __func__, p->outformat);
-    }
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-/**************************************************************/
 /**************       convert to 8 bit          ***************/
 /**************************************************************/
 void
@@ -361,6 +310,7 @@ convertt_scale_to_uchar(struct converttparams *p)
 void
 convertt(struct converttparams *p)
 {
+  gal_data_t *channel;
 
   /* Make any of the desired changes to the data. */
   if(p->changeaftertrunc)
@@ -374,35 +324,35 @@ convertt(struct converttparams *p)
       convertt_truncate(p);
     }
 
-
-
   /* Save the outputs: */
   switch(p->outformat)
     {
-    case OUT_FORMAT_TXT:
+    /* FITS: a FITS file can have many extensions (channels). */
     case OUT_FORMAT_FITS:
-      save_with_gnuastro_lib(p);
+      for(channel=p->chll; channel!=NULL; channel=channel->next)
+        gal_fits_img_write(channel, p->cp.output, NULL, PROGRAM_NAME);
       break;
 
+    /* Plain text: only one channel is acceptable. */
+    case OUT_FORMAT_TXT:
+      gal_checkset_writable_remove(p->cp.output, 0, p->cp.dontdelete);
+      gal_txt_write(p->chll, NULL, p->cp.output);
+      break;
+
+    /* JPEG: */
     case OUT_FORMAT_JPEG:
-#ifdef HAVE_LIBJPEG
       convertt_scale_to_uchar(p);
-      jpeg_write(p);
-#else
-      error(EXIT_FAILURE, 0, "you have asked for a JPEG output, however, "
-            "when %s was configured libjpeg was not available. To write "
-            "to JPEG files, libjpeg is required. Please install it and "
-            "configure, make and install %s again", PACKAGE_STRING,
-            PACKAGE_STRING);
-#endif
+      gal_jpeg_write(p->chll, p->cp.output, p->quality, p->widthincm);
       break;
 
+    /* EPS/PDF. */
     case OUT_FORMAT_EPS:
     case OUT_FORMAT_PDF:
       convertt_scale_to_uchar(p);
       eps_write_eps_or_pdf(p);
       break;
 
+    /* Not recognized. */
     default:
       error(EXIT_FAILURE, 0, "%s: a bug! Please contact us so we can find "
             "the problem and fix it The internal type of the output is "
diff --git a/bin/convertt/jpeg.h b/bin/convertt/jpeg.h
deleted file mode 100644
index 14a9310..0000000
--- a/bin/convertt/jpeg.h
+++ /dev/null
@@ -1,38 +0,0 @@
-/*********************************************************************
-ConvertType - Convert between various types of files.
-ConvertType is part of GNU Astronomy Utilities (Gnuastro) package.
-
-Original author:
-     Mohammad Akhlaghi <address@hidden>
-Contributing author(s):
-Copyright (C) 2015-2018, Free Software Foundation, Inc.
-
-Gnuastro is free software: you can redistribute it and/or modify it
-under the terms of the GNU General Public License as published by the
-Free Software Foundation, either version 3 of the License, or (at your
-option) any later version.
-
-Gnuastro is distributed in the hope that it will be useful, but
-WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
-**********************************************************************/
-#ifndef JPEG_H
-#define JPEG_H
-
-int
-nameisjpeg(char *name);
-
-int
-nameisjpegsuffix(char *name);
-
-size_t
-jpeg_read_to_ll(char *filename, gal_data_t **list, size_t minmapsize);
-
-void
-jpeg_write(struct converttparams *p);
-
-#endif
diff --git a/bin/convertt/ui.c b/bin/convertt/ui.c
index d90fbf3..94cd70d 100644
--- a/bin/convertt/ui.c
+++ b/bin/convertt/ui.c
@@ -32,6 +32,7 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #include <gnuastro/wcs.h>
 #include <gnuastro/list.h>
 #include <gnuastro/fits.h>
+#include <gnuastro/jpeg.h>
 #include <gnuastro/tiff.h>
 #include <gnuastro/table.h>
 #include <gnuastro/blank.h>
@@ -46,7 +47,6 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 
 #include "ui.h"
 #include "eps.h"
-#include "jpeg.h"
 #include "authors-cite.h"
 
 
@@ -453,17 +453,11 @@ ui_make_channels_ll(struct converttparams *p)
 
 
       /* JPEG: */
-      else if ( nameisjpeg(name->v) )
+      else if ( gal_jpeg_name_is_jpeg(name->v) )
         {
-#ifndef HAVE_LIBJPEG
-          error(EXIT_FAILURE, 0, "you are giving a JPEG input, however, "
-                "when %s was configured, libjpeg was not available. To read "
-                "from (and write to) JPEG files, libjpeg is required. "
-                "Please install it and configure, make and install %s "
-                "again", PACKAGE_STRING, PACKAGE_STRING);
-#else
-          p->numch += jpeg_read_to_ll(name->v, &p->chll, p->cp.minmapsize);
-#endif
+          data=gal_jpeg_read(name->v, p->cp.minmapsize);
+          p->numch += gal_list_data_number(data);
+          gal_list_data_add(&p->chll, data);
         }
 
 
@@ -656,22 +650,17 @@ ui_set_output(struct converttparams *p)
 {
   struct gal_options_common_params *cp=&p->cp;
 
-  /* Determine the type */
+  /* FITS */
   if(gal_fits_name_is_fits(cp->output))
     {
       p->outformat=OUT_FORMAT_FITS;
       if( gal_fits_suffix_is_fits(cp->output) )
         ui_add_dot_use_automatic_output(p);
     }
-  else if(nameisjpeg(cp->output))
+
+  /* JPEG */
+  else if(gal_jpeg_name_is_jpeg(cp->output))
     {
-#ifndef HAVE_LIBJPEG
-      error(EXIT_FAILURE, 0, "you have asked for a JPEG output, "
-            "however, when %s was configured libjpeg was not "
-            "available. To write to JPEG files, libjpeg is required. "
-            "Please install it and configure, make and install %s "
-            "again", PACKAGE_STRING, PACKAGE_STRING);
-#else
       /* Small sanity checks. */
       if(p->quality == GAL_BLANK_UINT8)
         error(EXIT_FAILURE, 0, "the `--quality' (`-u') option is necessary "
@@ -683,26 +672,33 @@ ui_set_output(struct converttparams *p)
 
       /* Preparations. */
       p->outformat=OUT_FORMAT_JPEG;
-      if( nameisjpegsuffix(cp->output) )
+      if( gal_jpeg_suffix_is_jpeg(cp->output) )
         ui_add_dot_use_automatic_output(p);
-#endif
     }
+
+  /* TIFF */
   else if( gal_tiff_name_is_tiff(cp->output) )
       error(EXIT_FAILURE, 0, "writing TIFF files is not yet supported, "
             "please get in touch with us at %s so we implement it",
             PACKAGE_BUGREPORT);
+
+  /* EPS */
   else if(nameiseps(cp->output))
     {
       p->outformat=OUT_FORMAT_EPS;
       if( nameisepssuffix(cp->output) )
         ui_add_dot_use_automatic_output(p);
     }
+
+  /* PDF */
   else if(nameispdf(cp->output))
     {
       p->outformat=OUT_FORMAT_PDF;
       if( nameispdfsuffix(cp->output) )
         ui_add_dot_use_automatic_output(p);
     }
+
+  /* Default: plain text. */
   else
     {
       p->outformat=OUT_FORMAT_TXT;
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index e553a12..2927437 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -557,8 +557,9 @@ Gnuastro library
 * Array input output::          Reading and writing images or cubes.
 * Table input output::          Reading and writing table columns.
 * FITS files::                  Working with FITS data.
-* TIFF files::                  Functions for using TIFF files.
-* Text files::                  Functions to work on Text files.
+* TIFF files::                  Reading and writing TIFF files.
+* JPEG files::                  Reading and writing JPEG files.
+* Text files::                  Reading and writing Text files.
 * World Coordinate System::     Dealing with the world coordinate system.
 * Arithmetic on datasets::      Arithmetic operations on a dataset.
 * Tessellation library::        Functions for working on tiles.
@@ -19103,8 +19104,9 @@ documentation will correspond to your installed version.
 * Array input output::          Reading and writing images or cubes.
 * Table input output::          Reading and writing table columns.
 * FITS files::                  Working with FITS data.
-* TIFF files::                  Functions for using TIFF files.
-* Text files::                  Functions to work on Text files.
+* TIFF files::                  Reading and writing TIFF files.
+* JPEG files::                  Reading and writing JPEG files.
+* Text files::                  Reading and writing Text files.
 * World Coordinate System::     Dealing with the world coordinate system.
 * Arithmetic on datasets::      Arithmetic operations on a dataset.
 * Tessellation library::        Functions for working on tiles.
@@ -22061,9 +22063,10 @@ formats, see @ref{Table input output}.
 
 
 
address@hidden TIFF files, Text files, FITS files, Gnuastro library
address@hidden TIFF files
address@hidden TIFF files, JPEG files, FITS files, Gnuastro library
address@hidden TIFF files (@file{tiff.h})
 
address@hidden TIFF format
 Outside of astronomy, the TIFF standard is arguably the most commonly used
 format to store high-precision data/images. Unlike FITS however, the TIFF
 standard only supports images (not tables), but like FITS, it has support
@@ -22111,7 +22114,61 @@ directory's image contains multiple channels, the 
output will be a list
 
 
 
address@hidden Text files, World Coordinate System, TIFF files, Gnuastro library
address@hidden JPEG files, Text files, TIFF files, Gnuastro library
address@hidden JPEG files (@file{jpeg.h})
+
address@hidden JPEG format
+The JPEG file format is one of the most common formats for storing and
+transferring images, recognized by almost all image rendering and
+processing programs. In particular, because of its lossy compression
+algorithm, JPEG files can have low volumes, making it used heavily on the
+internet. For more on this file format, and a comparison with others,
+please see @ref{Recognized file formats}.
+
+For scientific purposes, the lossy compression and very limited dymanic
+range (8-bit integers) make JPEG very un-attractive for storing of valuable
+data. However, because of its commonality, it will inevitably be needed in
+some situations. The functions here can be used to read and write JPEG
+images into Gnuastro's @ref{Generic data container}. If the JPEG file has
+more than one color channel, each channel is treated as a separate node in
+a list of datasets (see @ref{List of gal_data_t}).
+
address@hidden {int} gal_jpeg_name_is_jpeg (char @code{*name})
+Return @code{1} if @code{name} has a JPEG suffix. This can be used to make
+sure that a given input file is JPEG. See @code{gal_jpeg_suffix_is_jpeg}
+for a list of recognized suffixes.
address@hidden deftypefun
+
address@hidden {int} gal_jpeg_suffix_is_jpeg (char @code{*name})
+Return @code{1} if @code{suffix} is a recognized JPEG suffix. The
+recognized suffixes are @code{.jpg}, @code{.JPG}, @code{.jpeg},
address@hidden, @code{.jpe}, @code{.jif}, @code{.jfif} and @code{.jfi}.
address@hidden deftypefun
+
address@hidden {gal_data_t *} gal_jpeg_read (char @code{*filename}, size_t 
@code{dir}, size_t @code{minmapsize})
+Read the JPEG file @code{filename} and return the contents as
address@hidden If the directory's image contains multiple channels, the
+output will be a list (see @ref{List of gal_data_t}).
address@hidden deftypefun
+
address@hidden JPEG compression quality
address@hidden {gal_data_t *} gal_jpeg_write (gal_data_t @code{*in}, char 
@code{*filename}, uint8_t @code{quality}, float @code{widthincm})
+Write the given dataset (@code{in}) into @file{filename} (a JPEG file). If
address@hidden is a list, then each node in the list will be a color channel,
+therefore there can only be 1, 3 or 4 nodes in the list. If the number of
+nodes is different, then this function will abort the program with a
+message describing the cause. The lossy JPEG compression level can be set
+through @code{quality} which is a value between 0 and 100 (inclusive, 100
+being the best quality). The display width of the JPEG file in units of
+centimeters (to suggest to viewers/users, only a meta-data) can be set
+through @code{widthincm}.
address@hidden deftypefun
+
+
+
+
+
address@hidden Text files, World Coordinate System, JPEG files, Gnuastro library
 @subsection Text files (@file{txt.h})
 
 FITS files are the primary data container in astronomy. FITS indeed as many
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 9917a3a..8c14596 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -58,9 +58,9 @@ libgnuastro_la_SOURCES = arithmetic.c arithmetic-and.c 
arithmetic-bitand.c \
   arithmetic-modulo.c arithmetic-multiply.c arithmetic-ne.c                \
   arithmetic-or.c arithmetic-plus.c array.c binary.c blank.c box.c         \
   checkset.c convolve.c cosmology.c data.c fits.c git.c interpolate.c      \
-  list.c match.c options.c permutation.c polygon.c qsort.c dimension.c     \
-  statistics.c table.c tableintern.c threads.c tiff.c tile.c timing.c      \
-  txt.c type.c wcs.c
+  jpeg.c list.c match.c options.c permutation.c polygon.c qsort.c          \
+  dimension.c statistics.c table.c tableintern.c threads.c tiff.c tile.c   \
+  timing.c txt.c type.c wcs.c
 
 
 
@@ -74,8 +74,8 @@ pkginclude_HEADERS = gnuastro/config.h 
$(headersdir)/arithmetic.h          \
   $(headersdir)/array.h $(headersdir)/binary.h $(headersdir)/blank.h       \
   $(headersdir)/box.h $(headersdir)/convolve.h $(headersdir)/cosmology.h   \
   $(headersdir)/data.h $(headersdir)/dimension.h $(headersdir)/fits.h      \
-  $(headersdir)/git.h $(headersdir)/interpolate.h $(headersdir)/list.h     \
-  $(headersdir)/match.h $(headersdir)/permutation.h                        \
+  $(headersdir)/git.h $(headersdir)/jpeg.h $(headersdir)/interpolate.h     \
+  $(headersdir)/list.h $(headersdir)/match.h $(headersdir)/permutation.h   \
   $(headersdir)/polygon.h $(headersdir)/qsort.h $(headersdir)/statistics.h \
   $(headersdir)/table.h $(headersdir)/threads.h $(headersdir)/tiff.h       \
   $(headersdir)/tile.h $(headersdir)/txt.h $(headersdir)/type.h            \
diff --git a/lib/array.c b/lib/array.c
index 502c7fa..57effe9 100644
--- a/lib/array.c
+++ b/lib/array.c
@@ -29,6 +29,7 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 
 #include <gnuastro/txt.h>
 #include <gnuastro/fits.h>
+#include <gnuastro/jpeg.h>
 #include <gnuastro/tiff.h>
 #include <gnuastro/array.h>
 
@@ -51,14 +52,22 @@ gal_array_read(char *filename, char *extension, size_t 
minmapsize)
 {
   size_t ext;
 
-  /* Based on the filename,  */
+  /* FITS  */
   if( gal_fits_name_is_fits(filename) )
     return gal_fits_img_read(filename, extension, minmapsize);
+
+  /* TIFF */
   else if ( gal_tiff_name_is_tiff(filename) )
     {
       ext=gal_tiff_dir_string_read(extension);
       return gal_tiff_read(filename, ext, minmapsize);
     }
+
+  /* JPEG */
+  else if ( gal_jpeg_name_is_jpeg(filename) )
+    return gal_jpeg_read(filename, minmapsize);
+
+  /* Default: plain text. */
   else
     return gal_txt_image_read(filename, minmapsize);
 
diff --git a/lib/gnuastro/jpeg.h b/lib/gnuastro/jpeg.h
new file mode 100644
index 0000000..52c1cc7
--- /dev/null
+++ b/lib/gnuastro/jpeg.h
@@ -0,0 +1,75 @@
+/*********************************************************************
+jpeg -- functions to read and write JPEG files.
+This is part of GNU Astronomy Utilities (Gnuastro) package.
+
+Original author:
+     Mohammad Akhlaghi <address@hidden>
+Contributing author(s):
+Copyright (C) 2015-2018, Free Software Foundation, Inc.
+
+Gnuastro is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation, either version 3 of the License, or (at your
+option) any later version.
+
+Gnuastro is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
+**********************************************************************/
+#ifndef __GAL_JPEG_H__
+#define __GAL_JPEG_H__
+
+
+/* Include other headers if necessary here. Note that other header files
+   must be included before the C++ preparations below */
+#include <gnuastro/data.h>
+
+
+
+/* C++ Preparations */
+#undef __BEGIN_C_DECLS
+#undef __END_C_DECLS
+#ifdef __cplusplus
+# define __BEGIN_C_DECLS extern "C" {
+# define __END_C_DECLS }
+#else
+# define __BEGIN_C_DECLS                /* empty */
+# define __END_C_DECLS                  /* empty */
+#endif
+/* End of C++ preparations */
+
+
+
+
+
+/* Actual header contants (the above were for the Pre-processor). */
+__BEGIN_C_DECLS  /* From C++ preparations */
+
+
+
+
+
+/* Functions */
+int
+gal_jpeg_name_is_jpeg(char *name);
+
+int
+gal_jpeg_suffix_is_jpeg(char *name);
+
+gal_data_t *
+gal_jpeg_read(char *filename, size_t minmapsize);
+
+void
+gal_jpeg_write(gal_data_t *in, char *filename, uint8_t quality,
+               float widthincm);
+
+
+
+
+__END_C_DECLS    /* From C++ preparations */
+
+#endif           /* __GAL_TIFF_H__ */
diff --git a/bin/convertt/jpeg.c b/lib/jpeg.c
similarity index 78%
rename from bin/convertt/jpeg.c
rename to lib/jpeg.c
index dd8989b..f89c6d0 100644
--- a/bin/convertt/jpeg.c
+++ b/lib/jpeg.c
@@ -1,6 +1,6 @@
 /*********************************************************************
-ConvertType - Convert between various types of files.
-ConvertType is part of GNU Astronomy Utilities (Gnuastro) package.
+jpeg -- functions to read and write JPEG files.
+This is part of GNU Astronomy Utilities (Gnuastro) package.
 
 Original author:
      Mohammad Akhlaghi <address@hidden>
@@ -33,10 +33,7 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #endif
 
 #include <gnuastro/list.h>
-#include <gnuastro/fits.h>
-
-#include "main.h"
-#include "jpeg.h"
+#include <gnuastro/jpeg.h>
 
 
 
@@ -48,7 +45,7 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
  **************      Acceptable JPEG names      **************
  *************************************************************/
 int
-nameisjpeg(char *name)
+gal_jpeg_name_is_jpeg(char *name)
 {
   size_t len;
   len=strlen(name);
@@ -70,7 +67,7 @@ nameisjpeg(char *name)
 
 
 int
-nameisjpegsuffix(char *name)
+gal_jpeg_suffix_is_jpeg(char *name)
 {
   if (strcmp(name, "jpg") == 0   || strcmp(name, ".jpg") == 0
       || strcmp(name, "JPG") == 0 || strcmp(name, ".JPG") == 0
@@ -102,10 +99,12 @@ nameisjpegsuffix(char *name)
 
 
 
-#ifdef HAVE_LIBJPEG
+
+
 /*************************************************************
  **************        Read a JPEG image        **************
  *************************************************************/
+#ifdef HAVE_LIBJPEG
 /* Read the example.c in libjpeg's source code to understand the
    details of what is going on here.  */
 struct my_error_mgr
@@ -135,7 +134,7 @@ jpeg_error_exit(j_common_ptr cinfo)
 
 
 
-void
+static void
 makejsample(JSAMPLE **a, size_t size)
 {
   JSAMPLE *jsarr;
@@ -159,7 +158,7 @@ makejsample(JSAMPLE **a, size_t size)
 
 
 
-unsigned char **
+static unsigned char **
 readjpg(char *inname, size_t *outs0, size_t *outs1, size_t *numcolors)
 {
   FILE * infile;
@@ -238,6 +237,7 @@ readjpg(char *inname, size_t *outs0, size_t *outs1, size_t 
*numcolors)
 
   return all;
 }
+#endif  /* HAVE_LIBJPEG */
 
 
 
@@ -245,10 +245,12 @@ readjpg(char *inname, size_t *outs0, size_t *outs1, 
size_t *numcolors)
 
 /* Read each color channel of a JPEG image as a separate array and put them
    in a linked list of data-structures. */
-size_t
-jpeg_read_to_ll(char *filename, gal_data_t **list, size_t minmapsize)
+gal_data_t *
+gal_jpeg_read(char *filename, size_t minmapsize)
 {
+#ifdef HAVE_LIBJPEG
   char *name;
+  gal_data_t *out=NULL;
   size_t ndim=2, dsize[2];
   unsigned char **allcolors;
   size_t i, s0, s1, numcolors;
@@ -263,7 +265,7 @@ jpeg_read_to_ll(char *filename, gal_data_t **list, size_t 
minmapsize)
       dsize[1]=s1;
       if( asprintf(&name, "JPEG_CH_%zu", i+1)<0 )
         error(EXIT_FAILURE, 0, "%s: asprintf allocation", __func__);
-      gal_list_data_add_alloc(list, allcolors[i], GAL_TYPE_UINT8, ndim,
+      gal_list_data_add_alloc(&out, allcolors[i], GAL_TYPE_UINT8, ndim,
                               dsize, NULL, 0, minmapsize, name, NULL, NULL);
       free(name);
     }
@@ -275,7 +277,14 @@ jpeg_read_to_ll(char *filename, gal_data_t **list, size_t 
minmapsize)
   free(allcolors);
 
   /* Return the number of color channels. */
-  return numcolors;
+  return out;
+#else
+  error(EXIT_FAILURE, 0, "%s: libjpeg was not found during the "
+        "configuration of %s on this system. To read from JPEG files, "
+        "libjpeg is required. Please install libjpeg and configure, make "
+        "and install %s again", __func__, PACKAGE_STRING, PACKAGE_STRING);
+  return NULL;
+#endif
 }
 
 
@@ -300,28 +309,36 @@ jpeg_read_to_ll(char *filename, gal_data_t **list, size_t 
minmapsize)
 /*************************************************************
  **************       Write a JPEG image        **************
  *************************************************************/
+#ifdef HAVE_LIBJPEG
 static void
-jpeg_write_array(JSAMPLE *jsr, struct converttparams *p)
+jpeg_write_array(JSAMPLE *jsr, gal_data_t *in, char *filename,
+                 uint8_t quality, float widthincm)
 {
   JSAMPROW r[1];
   FILE * outfile;
   int row_stride=0, c;
+  size_t *dsize=in->dsize;
   struct jpeg_error_mgr jerr;
-  size_t *dsize=p->chll->dsize;
   struct jpeg_compress_struct cinfo;
+  size_t numch=gal_list_data_number(in);
+
+  /* A small sanity check. */
+  if(quality > 100)
+    error(EXIT_FAILURE, 0, "%s: quality value %u not acceptable. It must be "
+          "a value between zero and 100 (inclusive)", __func__, quality);
 
   /* Begin the JPEG writing, following libjpeg's example.c  */
   cinfo.err = jpeg_std_error(&jerr);
   jpeg_create_compress(&cinfo);
 
   errno=0;
-  if ((outfile = fopen(p->cp.output, "wb")) == NULL)
-    error(EXIT_FAILURE, errno, "%s", p->cp.output);
+  if ((outfile = fopen(filename, "wb")) == NULL)
+    error(EXIT_FAILURE, errno, "%s", filename);
   jpeg_stdio_dest(&cinfo, outfile);
 
   cinfo.image_width  = dsize[1];
   cinfo.image_height = dsize[0];
-  switch(p->numch)
+  switch(numch)
     {
     case 1:
       row_stride=dsize[1];
@@ -341,13 +358,13 @@ jpeg_write_array(JSAMPLE *jsr, struct converttparams *p)
     default:
       error(EXIT_FAILURE, 0, "%s: a bug! The number of channels is not 1, 3 "
             "or 4, but %zu. This should not happen. Please contact us so we "
-            "can fix the problem", __func__, p->numch);
+            "can fix the problem", __func__, numch);
     }
 
   jpeg_set_defaults(&cinfo);
-  jpeg_set_quality(&cinfo, p->quality, TRUE);
+  jpeg_set_quality(&cinfo, quality, TRUE);
   cinfo.density_unit=1;
-  cinfo.Y_density=cinfo.X_density=dsize[1]/(p->widthincm/2.54);
+  cinfo.Y_density=cinfo.X_density=dsize[1]/(widthincm/2.54);
   jpeg_start_compress(&cinfo, TRUE);
 
   /* cinfo.next_scanline is 'unsigned int' */
@@ -362,41 +379,48 @@ jpeg_write_array(JSAMPLE *jsr, struct converttparams *p)
   fclose(outfile);
   jpeg_destroy_compress(&cinfo);
 }
+#endif  /* HAVE_LIBJPEG */
 
 
 
 
 
 void
-jpeg_write(struct converttparams *p)
+gal_jpeg_write(gal_data_t *in, char *filename, uint8_t quality,
+               float widthincm)
 {
+#ifdef HAVE_LIBJPEG
   JSAMPLE *jsr;
   gal_data_t *channel;
   unsigned char *colors[4];
-  size_t i, pixel, color, numch=p->numch;
+  size_t i, pixel, color;
+  size_t numch=gal_list_data_number(in);
 
-  /* A small sanity check */
-  if(p->numch==2 || p->numch>4)
+  /* Small sanity checks. */
+  if(numch==2 || numch>4)
     error(EXIT_FAILURE, 0, "%s: only 1, 3, and 4 color channels are "
-          "acceptable", __func__);
+          "acceptable, input is a list of %zu data sets", __func__, numch);
+  if(in->type!=GAL_TYPE_UINT8)
+    error(EXIT_FAILURE, 0, "%s: input has a `%s' type, but JPEG images can "
+          "only have a `uint8' type", __func__, gal_type_name(in->type, 1));
 
   /* Make sure the JSAMPLE is 8bits, then allocate the necessary space
      based on the number of channels. */
   if(sizeof *jsr!=1)
     error(EXIT_FAILURE, 0, "%s: JSAMPLE has to be 8bit", __func__);
   errno=0;
-  jsr=malloc(numch * p->chll->size * sizeof *jsr);
+  jsr=malloc(numch * in->size * sizeof *jsr);
   if(jsr==NULL)
     error(EXIT_FAILURE, errno, "%s: allocating %zu bytes for jsr",
-          __func__, numch * p->chll->size * sizeof *jsr );
+          __func__, numch * in->size * sizeof *jsr );
 
   /* Set the pointers to each color. */
   i=0;
-  for(channel=p->chll; channel!=NULL; channel=channel->next)
+  for(channel=in; channel!=NULL; channel=channel->next)
     colors[i++]=channel->array;
 
   /* Write the different colors into jsr. */
-  for(pixel=0; pixel<p->chll->size; ++pixel)
+  for(pixel=0; pixel<in->size; ++pixel)
     for(color=0;color<numch;++color)
       {
         jsr[pixel*numch+color] = colors[color][pixel];
@@ -407,7 +431,12 @@ jpeg_write(struct converttparams *p)
       }
 
   /* Write jsr to a JPEG image and clean up. */
-  jpeg_write_array(jsr, p);
+  jpeg_write_array(jsr, in, filename, quality, widthincm);
   free(jsr);
-}
+#else
+  error(EXIT_FAILURE, 0, "%s: libjpeg was not found during the "
+        "configuration of %s on this system. To write JPEG files, libjpeg "
+        "is required. Please install libjpeg, then configure, make and "
+        "install %s again", __func__, PACKAGE_STRING, PACKAGE_STRING);
 #endif  /* HAVE_LIBJPEG */
+}
diff --git a/lib/tiff.c b/lib/tiff.c
index 023c223..418d2c0 100644
--- a/lib/tiff.c
+++ b/lib/tiff.c
@@ -598,8 +598,9 @@ gal_tiff_read(char *filename, size_t dir, size_t minmapsize)
   return out;
 #else
   error(EXIT_FAILURE, 0, "%s: libtiff was not found during the "
-    "configuration of %s. To read from (and write to) TIFF files, "
-    "libtiff is required. Please install libtiff and configure, make "
-    "and install %s again", __func__, PACKAGE_STRING, PACKAGE_STRING);
-#endif
+        "configuration of %s on this system. To read from TIFF files, "
+        "libtiff is required. Please install libtiff, then configure, make "
+        "and install %s again", __func__, PACKAGE_STRING, PACKAGE_STRING);
+  return NULL;
+#endif  /* HAVE_LIBTIFF */
 }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]