Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals  

src/main/array.c

Go to the documentation of this file.
00001 /*
00002  * "$Id: array.c,v 1.14 2004/05/07 19:20:28 rleigh Exp $"
00003  *
00004  *   Array data type.  This type is designed to be derived from by
00005  *   the curve and dither matrix types.
00006  *
00007  *   Copyright 2002-2003 Robert Krawitz (rlk@alum.mit.edu)
00008  *   Copyright 2003      Roger Leigh (rleigh@debian.org)
00009  *
00010  *   This program is free software; you can redistribute it and/or modify it
00011  *   under the terms of the GNU General Public License as published by the Free
00012  *   Software Foundation; either version 2 of the License, or (at your option)
00013  *   any later version.
00014  *
00015  *   This program is distributed in the hope that it will be useful, but
00016  *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00017  *   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00018  *   for more details.
00019  *
00020  *   You should have received a copy of the GNU General Public License
00021  *   along with this program; if not, write to the Free Software
00022  *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00023  */
00024 
00025 #ifdef HAVE_CONFIG_H
00026 #include <config.h>
00027 #endif
00028 #include <gimp-print/gimp-print.h>
00029 #include "gimp-print-internal.h"
00030 #include <gimp-print/gimp-print-intl-internal.h>
00031 #include <math.h>
00032 #include <string.h>
00033 #include <stdlib.h>
00034 #include <limits.h>
00035 
00036 
00037 struct stp_array
00038 {
00039   stp_sequence_t *data; /* First member, to allow typecasting to sequence. */
00040   int x_size;
00041   int y_size;
00042 };
00043 
00044 /*
00045  * We could do more sanity checks here if we want.
00046  */
00047 static inline void
00048 check_array(const stp_array_t *array)
00049 {
00050   if (array == NULL)
00051     {
00052       stp_erprintf("Null stp_array_t! Please report this bug.\n");
00053       stp_abort();
00054     }
00055 }
00056 
00057 
00058 static void array_ctor(stp_array_t *array)
00059 {
00060   array->data = stp_sequence_create();
00061   stp_sequence_set_size(array->data, array->x_size * array->y_size);
00062 }
00063 
00064 stp_array_t *
00065 stp_array_create(int x_size, int y_size)
00066 {
00067   stp_array_t *ret;
00068   ret = stp_zalloc(sizeof(stp_array_t));
00069   ret->x_size = x_size;
00070   ret->y_size = y_size;
00071   ret->data = NULL;
00072   array_ctor(ret);
00073   return ret;
00074 }
00075 
00076 
00077 static void
00078 array_dtor(stp_array_t *array)
00079 {
00080   if (array->data)
00081     stp_sequence_destroy(array->data);
00082   memset(array, 0, sizeof(stp_array_t));
00083 }
00084 
00085 void
00086 stp_array_destroy(stp_array_t *array)
00087 {
00088   check_array(array);
00089   array_dtor(array);
00090   stp_free(array);
00091 }
00092 
00093 void stp_array_copy(stp_array_t *dest, const stp_array_t *source)
00094 {
00095   check_array(dest);
00096   check_array(source);
00097 
00098   dest->x_size = source->x_size;
00099   dest->y_size = source->y_size;
00100   if (dest->data)
00101     stp_sequence_destroy(dest->data);
00102   dest->data = stp_sequence_create_copy(source->data);
00103 }
00104 
00105 stp_array_t *
00106 stp_array_create_copy(const stp_array_t *array)
00107 {
00108   stp_array_t *ret;
00109   check_array(array);
00110   ret = stp_array_create(0, 0); /* gets freed next */
00111   stp_array_copy(ret, array);
00112   return ret;
00113 }
00114 
00115 
00116 void
00117 stp_array_set_size(stp_array_t *array, int x_size, int y_size)
00118 {
00119   check_array(array);
00120   if (array->data) /* Free old data */
00121     stp_sequence_destroy(array->data);
00122   array->x_size = x_size;
00123   array->y_size = y_size;
00124   array->data = stp_sequence_create();
00125   stp_sequence_set_size(array->data, array->x_size * array->y_size);
00126 }
00127 
00128 void
00129 stp_array_get_size(const stp_array_t *array, int *x_size, int *y_size)
00130 {
00131   check_array(array);
00132   *x_size = array->x_size;
00133   *y_size = array->y_size;
00134   return;
00135 }
00136 
00137 void
00138 stp_array_set_data(stp_array_t *array, const double *data)
00139 {
00140   check_array(array);
00141   stp_sequence_set_data(array->data, array->x_size * array->y_size,
00142                         data);
00143 }
00144 
00145 void
00146 stp_array_get_data(const stp_array_t *array, size_t *size, const double **data)
00147 {
00148   check_array(array);
00149   stp_sequence_get_data(array->data, size, data);
00150 }
00151 
00152 int
00153 stp_array_set_point(stp_array_t *array, int x, int y, double data)
00154 {
00155   check_array(array);
00156 
00157   if (((array->x_size * x) + y) >= (array->x_size * array->y_size))
00158     return 0;
00159 
00160   return stp_sequence_set_point(array->data, (array->x_size * x) + y, data);}
00161 
00162 int
00163 stp_array_get_point(const stp_array_t *array, int x, int y, double *data)
00164 {
00165   check_array(array);
00166 
00167   if (((array->x_size * x) + y) >= array->x_size * array->y_size)
00168     return 0;
00169   return stp_sequence_get_point(array->data,
00170                                 (array->x_size * x) + y, data);
00171 }
00172 
00173 const stp_sequence_t *
00174 stp_array_get_sequence(const stp_array_t *array)
00175 {
00176   check_array(array);
00177 
00178   return array->data;
00179 }
00180 
00181 stp_array_t *
00182 stp_array_create_from_xmltree(stp_mxml_node_t *array)  /* The array node */
00183 {
00184   const char *stmp;                          /* Temporary string */
00185   stp_mxml_node_t *child;                       /* Child sequence node */
00186   int x_size, y_size;
00187   size_t count;
00188   stp_sequence_t *seq = NULL;
00189   stp_array_t *ret = NULL;
00190 
00191   stmp = stp_mxmlElementGetAttr(array, "x-size");
00192   if (stmp)
00193     {
00194       x_size = (int) strtoul(stmp, NULL, 0);
00195     }
00196   else
00197     {
00198       stp_erprintf("stp_array_create_from_xmltree: \"x-size\" missing\n");
00199       goto error;
00200     }
00201   /* Get y-size */
00202   stmp = stp_mxmlElementGetAttr(array, "y-size");
00203   if (stmp)
00204     {
00205       y_size = (int) strtoul(stmp, NULL, 0);
00206     }
00207   else
00208     {
00209       stp_erprintf("stp_array_create_from_xmltree: \"y-size\" missing\n");
00210       goto error;
00211     }
00212 
00213   /* Get the sequence data */
00214 
00215   child = stp_mxmlFindElement(array, array, "sequence", NULL, NULL, STP_MXML_DESCEND);
00216   if (child)
00217     seq = stp_sequence_create_from_xmltree(child);
00218 
00219   if (seq == NULL)
00220     goto error;
00221 
00222   ret = stp_array_create(x_size, y_size);
00223   if (ret->data)
00224     stp_sequence_destroy(ret->data);
00225   ret->data = seq;
00226 
00227   count = stp_sequence_get_size(seq);
00228   if (count != (x_size * y_size))
00229     {
00230       stp_erprintf("stp_array_create_from_xmltree: size mismatch between array and sequence\n");
00231       goto error;
00232     }
00233 
00234   return ret;
00235 
00236  error:
00237   stp_erprintf("stp_array_create_from_xmltree: error during array read\n");
00238   if (ret)
00239     stp_array_destroy(ret);
00240   return NULL;
00241 }

Generated on Wed May 12 20:21:27 2004 for libgimpprint API Reference by doxygen1.2.17