JWS C Library
C language utility library
|
00001 /* 00002 * This work is licensed under a Creative Commons 00003 * Attribution-Noncommercial-Share Alike 3.0 United States License. 00004 * 00005 * http://creativecommons.org/licenses/by-nc-sa/3.0/us/ 00006 * 00007 * You are free: 00008 * 00009 * to Share - to copy, distribute, display, and perform the work 00010 * to Remix - to make derivative works 00011 * 00012 * Under the following conditions: 00013 * 00014 * Attribution. You must attribute the work in the manner specified by the 00015 * author or licensor (but not in any way that suggests that they endorse you 00016 * or your use of the work). 00017 * 00018 * Noncommercial. You may not use this work for commercial purposes. 00019 * 00020 * Share Alike. If you alter, transform, or build upon this work, you may 00021 * distribute the resulting work only under the same or similar license to 00022 * this one. 00023 * 00024 * For any reuse or distribution, you must make clear to others the license 00025 * terms of this work. The best way to do this is by including this header. 00026 * 00027 * Any of the above conditions can be waived if you get permission from the 00028 * copyright holder. 00029 * 00030 * Apart from the remix rights granted under this license, nothing in this 00031 * license impairs or restricts the author's moral rights. 00032 */ 00033 00034 00046 #include <jwsc/config.h> 00047 00048 #include <stdlib.h> 00049 #include <stdio.h> 00050 #include <inttypes.h> 00051 #include <assert.h> 00052 #include <string.h> 00053 00054 #if defined JWSC_HAVE_MAGICK_MAGICKCORE_H 00055 #include <magick/MagickCore.h> 00056 #elif defined JWSC_HAVE_MAGICK_API_H 00057 #include <magick/api.h> 00058 #endif 00059 00060 #include "jwsc/base/error.h" 00061 #include "jwsc/base/fname.h" 00062 #include "jwsc/image/image.h" 00063 #include "jwsc/image/jiff.h" 00064 #include "jwsc/image/image_io.h" 00065 00066 00088 Error* read_image_f(Image_f** img_out, const char* fname) 00089 { 00090 Error* e = NULL; 00091 00092 if (fname_has_suffix(fname, "jiff")) 00093 { 00094 e = read_image_as_jiff_f(img_out, fname); 00095 } 00096 else 00097 { 00098 #ifdef JWSC_HAVE_MAGICK 00099 e = read_image_with_magick_f(img_out, fname); 00100 #else 00101 e = JWSC_EIO("Unknown image format"); 00102 free_image_f(*img_out); *img_out = NULL; 00103 #endif 00104 } 00105 00106 return e; 00107 } 00108 00131 #ifdef JWSC_HAVE_MAGICK 00132 Error* read_image_with_magick_f(Image_f** img_out, const char* fname) 00133 { 00134 uint32_t num_rows, num_cols; 00135 00136 ExceptionInfo magick_exception; 00137 ImageInfo* magick_info = NULL; 00138 Image* magick_img = NULL; 00139 Error* e = NULL; 00140 00141 GetExceptionInfo(&magick_exception); 00142 magick_info = CloneImageInfo(NULL); 00143 strncpy(magick_info->filename, fname, MaxTextExtent); 00144 00145 if ((magick_img = ReadImage(magick_info, &magick_exception)) == NULL) 00146 { 00147 e = JWSC_EIO(magick_exception.reason); 00148 goto cleanup; 00149 } 00150 00151 num_rows = (uint32_t)magick_img->rows; 00152 num_cols = (uint32_t)magick_img->columns; 00153 00154 create_image_f(img_out, num_rows, num_cols); 00155 00156 if (ExportImagePixels(magick_img, 0, 0, num_cols, num_rows, "RGB", 00157 FloatPixel, *((*img_out)->pxls), &magick_exception) == 0) 00158 { 00159 e = JWSC_EIO(magick_exception.reason); 00160 goto cleanup; 00161 } 00162 00163 cleanup: 00164 if (magick_img) DestroyImage(magick_img); 00165 if (magick_info) DestroyImageInfo(magick_info); 00166 if (e) {free_image_f(*img_out); *img_out = NULL;} 00167 return e; 00168 } 00169 #endif 00170 00191 Error* write_image_f(const Image_f* img, const char* fname) 00192 { 00193 JIFF_attributes attrs; 00194 Error* e = NULL; 00195 00196 if (fname_has_suffix(fname, "jiff")) 00197 { 00198 init_jiff_attributes(&attrs); 00199 e = write_image_as_jiff_f(img, fname, &attrs); 00200 } 00201 else 00202 { 00203 #ifdef JWSC_HAVE_MAGICK 00204 e = write_image_with_magick_f(img, fname); 00205 #else 00206 e = JWSC_EIO("Unknown image format"); 00207 #endif 00208 } 00209 00210 return e; 00211 } 00212 00233 #ifdef JWSC_HAVE_MAGICK 00234 Error* write_image_with_magick_f(const Image_f* img, const char* fname) 00235 { 00236 ExceptionInfo magick_exception; 00237 ImageInfo* magick_info = NULL; 00238 Image* magick_img = NULL; 00239 Error* e = NULL; 00240 00241 GetExceptionInfo(&magick_exception); 00242 magick_info = CloneImageInfo(NULL); 00243 00244 if ((magick_img = ConstituteImage(img->num_cols, img->num_rows, "RGB", 00245 FloatPixel, *(img->pxls), &magick_exception)) == NULL) 00246 { 00247 e = JWSC_EIO(magick_exception.reason); 00248 goto cleanup; 00249 } 00250 00251 strncpy(magick_img->filename, fname, MaxTextExtent); 00252 00253 if (WriteImage(magick_info, magick_img) == 0) 00254 { 00255 e = JWSC_EIO(magick_img->exception.reason); 00256 goto cleanup; 00257 } 00258 00259 cleanup: 00260 if (magick_img) DestroyImage(magick_img); 00261 if (magick_info) DestroyImageInfo(magick_info); 00262 return e; 00263 } 00264 #endif 00265