~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/sys/image/image.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**@(#)Imrect handling. (Imrect is Tina's generic image structure)
  2  * @(#)Imrect is:
  3  * @(#){ Vartype vtype; int imtype; int width, height; Imregion *region; List *props; }
  4  */
  5 
  6 #include <stdio.h>
  7 #include <tina/sys.h>
  8 #include <tina/sysfuncs.h>
  9 #include <tina/image.h>
 10 
 11 /* Image allocation.  Allocate memory for Imrect data structure and for
 12  * region of interest (roi).  Ie Imrect (image rectangle) and imregion.
 13  * Set roi co-ordinates (top-left bottom-right). Allocate memory for
 14  * image data. */
 15 Imrect *im_alloc(int height, int width, Imregion * region, Vartype vtype)
 16 {
 17     Imrect *image;
 18     Imregion *roi_alloc();
 19     Imregion *roi_copy();
 20     void  **narray_alloc();
 21 
 22     int     lx, ly, ux, uy;
 23 
 24     if (region == NULL)         /* alloc whole image */
 25         region = roi_alloc(0, 0, width, height);
 26     else                        /* use copy of given region */
 27         region = roi_copy(region);
 28 
 29     if (region == NULL)
 30         return (NULL);
 31 
 32     lx = region->lx;
 33     ly = region->ly;
 34     ux = region->ux;
 35     uy = region->uy;
 36 
 37     image = ts_ralloc(Imrect);
 38 
 39     image->height = height;
 40     image->width = width;
 41     image->vtype = vtype;
 42     image->region = region;
 43     image->props = NULL;
 44     if (vtype == complex_v)
 45     {
 46         image->data = (void *) ts_narray_alloc(ly, lx, uy, ux, Complex);
 47     } else
 48     {
 49         image->data = (void *) narray_alloc(ly, lx, uy, ux, var_size(vtype));
 50     }
 51 
 52     return image;
 53 }
 54 
 55 /* Wrap an imrect data structure around the contiguous block of memory
 56  * specified by the region and vtype arguments. */
 57 Imrect *im_wrap_contig(void *mem_ptr, int height, int width, Imregion * region, Vartype vtype)
 58 {
 59     Imrect *image;
 60     Imregion *roi_alloc();
 61     Imregion *roi_copy();
 62     int     i;
 63     int     lx, ly, ux, uy;
 64     int     roi_width;
 65     unsigned int vsize;
 66     void   *nvector_alloc();
 67     void  **data;
 68 
 69     if (region == NULL)         /* alloc whole image */
 70         region = roi_alloc(0, 0, width, height);
 71     else                        /* use copy of given region */
 72         region = roi_copy(region);
 73 
 74     if (region == NULL)
 75         return (NULL);
 76 
 77     lx = region->lx;
 78     ly = region->ly;
 79     ux = region->ux;
 80     uy = region->uy;
 81     roi_width = ux - lx;
 82 
 83     image = ts_ralloc(Imrect);
 84 
 85     image->height = height;
 86     image->width = width;
 87     image->vtype = vtype;
 88     image->region = region;
 89     image->props = NULL;
 90 
 91     data = (void **) nvector_alloc(ly, uy, sizeof(void *));
 92     image->data = (void *) data;
 93 
 94     vsize = var_size(vtype);
 95 
 96     for (i = ly; i < uy; ++i)
 97         data[i] = (void *) ((int) mem_ptr + (roi_width * (i - ly) - lx) * vsize);
 98 
 99     return image;
100 }
101 
102 /* Copy specified roi of image1 to image2 throgh a double Storage must
103  * already be allocated for image2. */
104 static void write_thro_double(Imrect * image1, Imrect * image2, Imregion * roi)
105 {
106     int     lx, ly, ux, uy;
107     int     i, j;
108     double  gl;
109 
110 
111     lx = roi->lx;
112     ly = roi->ly;
113     ux = roi->ux;
114     uy = roi->uy;
115 
116     for (i = ly; i < uy; ++i)
117         for (j = lx; j < ux; ++j)
118         {
119             IM_PIX_GET(image1, i, j, gl);
120             IM_PIX_SET(image2, i, j, gl);
121         }
122 }
123 
124 /* Copy specified roi of image1 to image2 through type int (ok for
125  * ptr).  Storage must already be allocated for image2. */
126 static void write_thro_int(Imrect * image1, Imrect * image2, Imregion * roi)
127 {
128     int     lx, ly, ux, uy;
129     int     i, j;
130     double     gl;
131 
132     lx = roi->lx;
133     ly = roi->ly;
134     ux = roi->ux;
135     uy = roi->uy;
136 
137     for (i = ly; i < uy; ++i)
138         for (j = lx; j < ux; ++j)
139         {
140             IM_PIX_GET(image1, i, j, gl);
141             IM_PIX_SET(image2, i, j, (int)gl);
142         }
143 }
144 
145 /* Copy specified roi of image1 to image2 through type complex. Storage
146  * must already be allocated for image2. */
147 static void write_thro_complex(Imrect * image1, Imrect * image2, Imregion * roi)
148 {
149     int     lx, ly, ux, uy;
150     int     i, j;
151     Complex gl =
152     {Complex_id};
153 
154     lx = roi->lx;
155     ly = roi->ly;
156     ux = roi->ux;
157     uy = roi->uy;
158 
159     for (i = ly; i < uy; ++i)
160         for (j = lx; j < ux; ++j)
161         {
162             IM_PIX_GETZ(image1, i, j, gl);
163             IM_PIX_SETZ(image2, i, j, gl);
164         }
165 }
166 
167 /* Overwrite region of image2 by region of image1. The region used is
168  * the intersection of the roi's of image1 and image2.  Storage must
169  * already be allocated for image2. */
170 void    im_copy_inplace(Imrect * image2, Imrect * image1)
171 {
172     Imregion *roi;
173     Imregion *roi_inter();
174     Vartype vtype;
175     void * memcpy();
176 
177     if (image1 == NULL || image2 == NULL)
178         return;
179 
180     roi = roi_inter(image1->region, image2->region);
181     if(!roi) 
182       {
183         errorf(warning,"im_copy_inplace() regions do not overlap\n");
184         return;
185       }
186     vtype = (Vartype) MAX((int) image1->vtype, (int) image2->vtype);
187 
188     if (image1->vtype != image2->vtype || (int) image1->vtype > (int) complex_v)
189     {
190         switch (vtype)
191         {
192         case char_v:
193         case uchar_v:
194         case short_v:
195         case ushort_v:
196         case int_v:
197         case uint_v:
198         case ptr_v:
199             write_thro_int(image1, image2, roi);
200             break;
201         case float_v:
202         case double_v:
203             write_thro_double(image1, image2, roi);
204             break;
205         case complex_v:
206             write_thro_complex(image1, image2, roi);
207             break;
208           case vram0_v:
209           case vram1_v:
210           case vram2_v:
211           case vram3_v:
212             write_thro_int(image1, image2, roi);
213             break;
214           default:
215             error("Cannot copy this image type",fatal);
216             break;
217         }
218     } else
219     {
220         /* same type */
221         int     lx, ly, ux, uy;
222         int     nchars;
223         unsigned int vsize;
224         int     i;
225         char  **array1 = (char **) image1->data;
226         char  **array2 = (char **) image2->data;
227 
228         vsize = var_size(image1->vtype);
229 
230         lx = roi->lx;
231         ly = roi->ly;
232         ux = roi->ux;
233         uy = roi->uy;
234         lx *= vsize;
235         ux *= vsize;
236         nchars = ux - lx;
237 
238         for (i = ly; i < uy; ++i)
239             (void) memcpy(array2[i] + lx, array1[i] + lx, nchars);
240     }
241 
242     rfree((void *) (char *) roi);
243 }
244 
245 /* Make new copy of specified roi of image.  Allocate storage for new
246  * copy.  Copy specified roi of image into it. Return pointer to it. */
247 Imrect *im_subim(Imrect * image, Imregion * region)
248 {
249     Imregion *subregion;
250     Imregion *roi_inter();
251     Imrect *subimage;
252 
253     if (image == NULL)
254         return (NULL);
255 
256     subregion = roi_inter(region, image->region);
257     if (subregion == NULL)
258         return (NULL);
259 
260     subimage = im_alloc(image->height, image->width, subregion, image->vtype);
261     rfree((void *) (char *) subregion);
262     im_copy_inplace(subimage, image);
263     return (subimage);
264 }
265 
266 /* Make new copy of image of given type.  Allocate storage for new
267  * copy. Copy whole image into it. Return pointer to it. */
268 Imrect *im_cast(Imrect * image, Vartype vtype)
269 {
270     Imrect *copy;
271 
272     if (image == NULL)
273         return (NULL);
274 
275     copy = im_alloc(image->height, image->width, image->region, vtype);
276     im_copy_inplace(copy, image);
277 
278     return (copy);
279 }
280 
281 /* Make new copy of image.  Allocate storage for new copy. Copy whole
282  * image into it. Return pointer to it. */
283 Imrect *im_copy(Imrect * image)
284 {
285     Imrect *copy;
286 
287     if (image == NULL)
288         return (NULL);
289 
290     copy = im_alloc(image->height, image->width, image->region, image->vtype);
291     im_copy_inplace(copy, image);
292 
293     return (copy);
294 }
295 
296 /* Destroy image.  Free an image's storage. */
297 void    im_free(Imrect * image)
298 {
299     int     lx, ly, ux, uy;
300     unsigned int vsize;
301 
302     if (image == NULL)
303         return;
304 
305     proplist_freelist(image->props);
306 
307     lx = image->region->lx;
308     ly = image->region->ly;
309     ux = image->region->ux;
310     uy = image->region->uy;
311 
312     vsize = var_size(image->vtype);
313     narray_free((char **) image->data, ly, lx, uy, ux, vsize);
314     rfree((void *) image->region);
315     rfree((void *) image);
316 }
317 
318 /**
319 Shifts data and roi of an image by
320  dy rows
321  dx cols
322 cheaply, in place
323 **/
324 
325 void    im_shift(Imrect * im, int dy, int dx)
326 {
327     Imregion *roi;
328     int     y, ly, uy;
329     unsigned int vsize;
330     void   *row;
331 
332     if (im == NULL)
333         return;
334 
335     roi = im->region;
336     ly = roi->ly;
337     uy = roi->uy;
338     vsize = var_size(im->vtype);
339 
340     for (y = ly; y < uy; y++)
341     {
342         IM_ROW_GET(im, y, row);
343 /*
344         row = (void *) ((int) row - vsize * dx);
345 */
346         row = nvector_shift(row, roi->lx, roi->lx + dx, vsize);
347         IM_ROW_SET(im, y, row);
348     }
349 /*
350     im->data = (void **) im->data - dy;
351 */
352     im->data =  nvector_shift((void **) im->data, roi->ly, roi->ly+dy,
353                               var_size(ptr_v)); 
354 
355     roi->lx += dx;
356     roi->ux += dx;
357     roi->ly += dy;
358     roi->uy += dy;
359 }
360 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.