JWS C Library
C language utility library
imgblock_io.c
Go to the documentation of this file.
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 #include "jwsc/base/error.h"
00055 #include "jwsc/image/image.h"
00056 #include "jwsc/image/image_io.h"
00057 #include "jwsc/image/jiff.h"
00058 #include "jwsc/imgblock/imgblock.h"
00059 #include "jwsc/imgblock/imgblock_io.h"
00060 
00061 
00089 Error* read_imgblock_numbered_f
00090 (
00091     Imgblock_f** imgblk_out, 
00092     const char*  fname_fmt,
00093     uint32_t     num_imgs
00094 )
00095 {
00096     char fname[256] = {0};
00097 
00098     uint32_t num_rows, num_cols;
00099     uint32_t i;
00100 
00101     Image_f* img = NULL;
00102     Error*   e;
00103 
00104     num_rows = 0;
00105     num_cols = 0;
00106 
00107     for (i = 0; i < num_imgs; i++)
00108     {
00109         snprintf(fname, 255, fname_fmt, i+1);
00110 
00111         if ((e = read_image_f(&img, fname)) != NULL)
00112         {
00113             free_imgblock_f(*imgblk_out); *imgblk_out = NULL;
00114             return e;
00115         }
00116 
00117         if (num_rows == 0 && num_cols == 0)
00118         {
00119             num_rows = img->num_rows;
00120             num_cols = img->num_cols;
00121             create_imgblock_f(imgblk_out, num_imgs, num_rows, num_cols);
00122         }
00123 
00124         assert(copy_image_into_imgblock_f(imgblk_out, *imgblk_out, img, 
00125                     IMGBLOCK_IMG_IMAGE, i) == NULL);
00126     }
00127 
00128     free_image_f(img);
00129 
00130     return NULL;
00131 }
00132 
00161 Error* read_imgblock_named_f
00162 (
00163     Imgblock_f**   imgblk_out, 
00164     const char**   fnames,
00165     uint32_t       num_imgs
00166 )
00167 {
00168     uint32_t num_rows, num_cols;
00169     uint32_t i;
00170 
00171     Image_f* img = NULL;
00172     Error*   e;
00173 
00174     num_rows = 0;
00175     num_cols = 0;
00176 
00177     for (i = 0; i < num_imgs; i++)
00178     {
00179         if ((e = read_image_f(&img, fnames[ i ])) != NULL)
00180         {
00181             free_imgblock_f(*imgblk_out); *imgblk_out = NULL;
00182             return e;
00183         }
00184 
00185         if (num_rows == 0 && num_cols == 0)
00186         {
00187             num_rows = img->num_rows;
00188             num_cols = img->num_cols;
00189             create_imgblock_f(imgblk_out, num_imgs, num_rows, num_cols);
00190         }
00191 
00192         assert(copy_image_into_imgblock_f(imgblk_out, *imgblk_out, img, 
00193                     IMGBLOCK_IMG_IMAGE, i) == NULL);
00194     }
00195 
00196     free_image_f(img);
00197 
00198     return NULL;
00199 }
00200 
00226 Error* write_imgblock_numbered_f
00227 (
00228     const Imgblock_f* imgblk,
00229     const char*       fname_fmt
00230 )
00231 {
00232     char fname[256] = {0};
00233 
00234     uint32_t i;
00235 
00236     Image_f* img = NULL;
00237     Error*   e;
00238 
00239     for (i = 0; i < imgblk->num_imgs; i++)
00240     {
00241         assert(copy_image_from_imgblock_f(&img, imgblk, IMGBLOCK_IMG_IMAGE, i) 
00242                 == NULL);
00243 
00244         snprintf(fname, 255, fname_fmt, i+1);
00245 
00246         if ((e = write_image_f(img, fname)) != NULL)
00247         {
00248             free_image_f(img);
00249             return e;
00250         }
00251     }
00252 
00253     free_image_f(img);
00254 
00255     return NULL;
00256 }
00257 
00278 Error* write_imgblock_named_f
00279 (
00280     const Imgblock_f* imgblk,
00281     const char**      fnames
00282 )
00283 {
00284     uint32_t i;
00285 
00286     Image_f* img = NULL;
00287     Error*   e;
00288 
00289     for (i = 0; i < imgblk->num_imgs; i++)
00290     {
00291         assert(copy_image_from_imgblock_f(&img, imgblk, IMGBLOCK_IMG_IMAGE, i) 
00292                 == NULL);
00293 
00294         if ((e = write_image_f(img, fnames[ i ])) != NULL)
00295         {
00296             free_image_f(img);
00297             return e;
00298         }
00299     }
00300 
00301     free_image_f(img);
00302 
00303     return NULL;
00304 }
00305 
00332 Error* write_imgblock_as_numbered_jiff_f
00333 (
00334     const Imgblock_f*      imgblk,
00335     const char*            fname_fmt,
00336     const JIFF_attributes* attrs
00337 )
00338 {
00339     char fname[256] = {0};
00340 
00341     uint32_t i;
00342 
00343     Image_f* img = NULL;
00344     Error*   e;
00345 
00346     for (i = 0; i < imgblk->num_imgs; i++)
00347     {
00348         assert(copy_image_from_imgblock_f(&img, imgblk, IMGBLOCK_IMG_IMAGE, i) 
00349                 == NULL);
00350 
00351         snprintf(fname, 255, fname_fmt, i+1);
00352 
00353         if ((e = write_image_as_jiff_f(img, fname, attrs)) != NULL)
00354         {
00355             free_image_f(img);
00356             return e;
00357         }
00358     }
00359 
00360     free_image_f(img);
00361 
00362     return NULL;
00363 }
00364 
00386 Error* write_imgblock_as_named_jiff_f
00387 (
00388     const Imgblock_f*      imgblk,
00389     const char**           fnames,
00390     const JIFF_attributes* attrs
00391 )
00392 {
00393     uint32_t i;
00394 
00395     Image_f* img = NULL;
00396     Error*   e;
00397 
00398     for (i = 0; i < imgblk->num_imgs; i++)
00399     {
00400         assert(copy_image_from_imgblock_f(&img, imgblk, IMGBLOCK_IMG_IMAGE, i) 
00401                 == NULL);
00402 
00403         if ((e = write_image_as_jiff_f(img, fnames[ i ], attrs)) != NULL)
00404         {
00405             free_image_f(img);
00406             return e;
00407         }
00408     }
00409 
00410     free_image_f(img);
00411 
00412     return NULL;
00413 }
00414