Make clang build as silent as possible
[fvs_assignment_project.git] / image.c
blobea7616db7b13c08d203b0477884f8d818cbba15a
1 /*########################################################################
3 * Copyright(C) 2002-2007. All Rights Reserved.
5 * Authors: Shivang Patel
6 * Jaap de Haan(jdh)
8 * Changes: jdh -> Added support for ImageMagick that enables
9 * to export files to more than 40 formats.
10 * This is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2, or (at your option) any later
13 * version.
15 * This is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * for more details.
20 * You should have received a copy of the GNU General Public License with
21 * the fvs source package as the
22 * file COPYING. If not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA
24 * 02111-1307, USA.
25 ########################################################################*/
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
31 #include "image.h"
33 /*!
34 A fingerprint image structure, a fingerprint image is a 256 gray level
35 image. This should be enough to do accurate processing. This image
36 class defines a common format for all algorithms running in this
37 suite. Images may be imported from different sources such as files,
38 network data, databases,...
40 typedef struct iFvsImage_t
42 FvsByte_t *pimg; /* 8 bit image array */
43 FvsInt_t w; /* width of image */
44 FvsInt_t h; /* height of image */
45 FvsInt_t pitch; /* pitch */
46 FvsImageFlag_t flags; /* flags */
47 FvsInt_t dpi; /* dots per inch */
48 } iFvsImage_t;
51 FvsImage_t ImageCreate()
53 iFvsImage_t* p = NULL;
55 p = (FvsImage_t)malloc(sizeof(iFvsImage_t));
56 if (p!=NULL)
58 p->h = 0;
59 p->w = 0;
60 p->pitch = 0;
61 p->pimg = NULL;
62 p->flags = FvsImageGray; /* default flag */
64 return (FvsImage_t)p;
68 void ImageDestroy(FvsImage_t image)
70 iFvsImage_t* p = NULL;
71 if (image==NULL)
72 return;
73 (void)ImageSetSize(image, 0, 0);
74 p = image;
75 free(p);
79 FvsError_t ImageSetFlag(FvsImage_t img, const FvsImageFlag_t flag)
81 iFvsImage_t* image = (iFvsImage_t*)img;
82 image->flags = flag;
83 return FvsOK;
87 FvsError_t ImageSetDPI(FvsImage_t img, const FvsInt_t dpi)
89 iFvsImage_t* image = (iFvsImage_t*)img;
90 image->dpi = dpi;
91 return FvsOK;
95 FvsInt_t ImageGetDPI(const FvsImage_t img)
97 iFvsImage_t* image = (iFvsImage_t*)img;
98 return image->dpi;
102 FvsImageFlag_t ImageGetFlag(const FvsImage_t img)
104 iFvsImage_t* image = (iFvsImage_t*)img;
105 return image->flags;
109 FvsError_t ImageSetSize(FvsImage_t img, const FvsInt_t width, const FvsInt_t height)
111 iFvsImage_t* image = (iFvsImage_t*)img;
112 FvsError_t nRet = FvsOK;
113 FvsInt_t newsize = width*height;
115 /* special case for zero size */
116 if (newsize==0)
118 if (image->pimg!=NULL)
120 free(image->pimg);
121 image->pimg = NULL;
122 image->w = 0;
123 image->h = 0;
124 image->pitch = 0;
126 return FvsOK;
129 if (image->h*image->w != newsize)
131 free(image->pimg);
132 image->w = 0;
133 image->h = 0;
134 image->pitch = 0;
135 /* This allocates the amount of memory need for the image structure */
136 image->pimg = (uint8_t*)malloc((size_t)newsize);
139 if (image->pimg == NULL)
140 nRet = FvsMemory;
141 else
143 image->h = height;
144 image->w = width;
145 image->pitch = width;
147 return nRet;
151 FvsError_t ImageCopy(FvsImage_t destination, const FvsImage_t source)
153 iFvsImage_t* dest = (iFvsImage_t*)destination;
154 iFvsImage_t* src = (iFvsImage_t*)source;
155 FvsError_t nRet = FvsOK;
157 nRet = ImageSetSize(dest, src->w, src->h);
159 if (nRet==FvsOK)
160 memcpy(dest->pimg, src->pimg, (size_t)src->h*src->w);
162 /* copy the flag */
163 dest->flags = src->flags;
165 return nRet;
169 FvsError_t ImageClear(FvsImage_t img)
171 return ImageFlood(img, 0);
175 FvsError_t ImageFlood(FvsImage_t img, const FvsByte_t value)
177 FvsError_t nRet = FvsOK;
178 iFvsImage_t* image = (iFvsImage_t*)img;
179 if (image==NULL) return FvsMemory;
180 if (image->pimg!=NULL)
181 memset(image->pimg, (int)value, (size_t)(image->h*image->w));
182 return nRet;
186 /* set a pixel value in the picture */
187 void ImageSetPixel(FvsImage_t img, const FvsInt_t x, const FvsInt_t y, const FvsByte_t val)
189 iFvsImage_t* image = (iFvsImage_t*)img;
190 int address = y * image->w + x;
191 image->pimg[address] = val;
195 /* This function returns the pixel for the x and y value */
196 FvsByte_t ImageGetPixel(const FvsImage_t img, const FvsInt_t x, const FvsInt_t y)
198 iFvsImage_t* image = (iFvsImage_t*)img;
199 /* position in array */
200 int address = y * image->pitch + x;
201 return image->pimg[address];
205 /* returns a pointer to the image buffer */
206 FvsByte_t* ImageGetBuffer(FvsImage_t img)
208 iFvsImage_t* image = (iFvsImage_t*)img;
209 if (image==NULL) return NULL;
210 return image->pimg;
214 /* retrieve width and height */
215 FvsInt_t ImageGetWidth(const FvsImage_t img)
217 iFvsImage_t* image = (iFvsImage_t*)img;
218 if (image==NULL) return -1;
219 return image->w;
223 FvsInt_t ImageGetHeight(const FvsImage_t img)
225 iFvsImage_t* image = (iFvsImage_t*)img;
226 if (image==NULL) return -1;
227 return image->h;
231 FvsInt_t ImageGetSize(const FvsImage_t img)
233 iFvsImage_t* image = (iFvsImage_t*)img;
234 if (image==NULL) return 0;
235 return image->h * image->w;
238 /* get the pitch pixel(x,y) pos is at x + y * pitch */
239 FvsInt_t ImageGetPitch(const FvsImage_t img)
241 iFvsImage_t* image = (iFvsImage_t*)img;
242 if (image==NULL) return -1;
243 return image->pitch;
246 FvsBool_t ImageCompareSize(const FvsImage_t image1, const FvsImage_t image2)
248 if (ImageGetWidth(image1)!=ImageGetWidth(image2))
249 return FvsFalse;
250 if (ImageGetHeight(image1)!=ImageGetHeight(image2))
251 return FvsFalse;
252 return FvsTrue;