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

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

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

  1 /**@(#)Image handling. (Getting values from an image).
  2  */
  3 
  4 #include <stdio.h>
  5 #include <tina/sys.h>
  6 #include <tina/sysfuncs.h>
  7 
  8 int     im_get_pix(Imrect * image, int i, int j)
  9 {
 10     Imregion *region;
 11     double     pixval;
 12 
 13     if (image == NULL || image->vtype == ptr_v)
 14         return (0);
 15 
 16     region = image->region;
 17     if (i < region->ly || i >= region->uy || j < region->lx || j >= region->ux)
 18         return (0);
 19 
 20     IM_PIX_GET(image, i, j, pixval);
 21 
 22     return ((int)pixval);
 23 }
 24 
 25 void   *im_get_ptr(Imrect * image, int i, int j)
 26 {
 27     Imregion *region;
 28 
 29     if (image == NULL || image->vtype != ptr_v)
 30         return (NULL);
 31 
 32     region = image->region;
 33     if (i < region->ly || i >= region->uy || j < region->lx || j >= region->ux)
 34         return (NULL);
 35 
 36     return (IM_PTR(image, i, j));
 37 }
 38 
 39 double  im_get_pixf(Imrect * image, int i, int j)
 40 {
 41     Imregion *region;
 42     double  pixval;
 43 
 44     if (image == NULL || image->vtype == ptr_v)
 45         return (0);
 46 
 47     region = image->region;
 48     if (i < region->ly || i >= region->uy || j < region->lx || j >= region->ux)
 49         return (0);
 50 
 51     IM_PIX_GET(image, i, j, pixval);
 52 
 53     return (pixval);
 54 }
 55 
 56 Complex im_get_pixz(Imrect * image, int i, int j)
 57 {
 58     Imregion *region;
 59     Complex pixval = {Complex_id};
 60 
 61     if (image == NULL || image->vtype == ptr_v)
 62         return (cmplx_zero());
 63 
 64     region = image->region;
 65     if (i < region->ly || i >= region->uy || j < region->lx || j >= region->ux)
 66         return (cmplx_zero());
 67 
 68     if (image->vtype == complex_v)
 69         pixval = IM_COMPLEX(image, i, j);
 70     else
 71     {
 72         pixval.y = 0.0;
 73         IM_PIX_GET(image, i, j, pixval.x);
 74     }
 75     return (pixval);
 76 }
 77 
 78 int     im_sub_pix(Imrect * image, double r, double c)
 79 {
 80     Imregion *region;
 81     int     i, j;
 82     double  x1, x2, y1, y2;
 83     double  gl, pix11, pix12, pix21, pix22;
 84     double    pixval;
 85 
 86     if (image == NULL || image->vtype == ptr_v)
 87         return (0);
 88 
 89     r -= 0.5;
 90     c -= 0.5;
 91 
 92     region = image->region;
 93     if (r < region->ly || r + 1 >= region->uy || c < region->lx || c + 1 >= region->ux)
 94         return (0);
 95 
 96     i = (int) r;
 97     y1 = r - i;
 98     y2 = 1.0 - y1;
 99     j = (int) c;
100     x1 = c - j;
101     x2 = 1.0 - x1;
102 
103     IM_PIX_GET(image, i, j, pix11);
104     IM_PIX_GET(image, i, j + 1, pix12);
105     IM_PIX_GET(image, i + 1, j, pix21);
106     IM_PIX_GET(image, i + 1, j + 1, pix22);
107 
108     gl = (pix11 * x2 + pix12 * x1) * y2 + (pix21 * x2 + pix22 * x1) * y1;
109 
110     pixval = (gl > 0) ? gl + 0.5 : gl - 0.5;
111     return ((int)pixval);
112 }
113 
114 double  im_sub_pixf(Imrect * image, double r, double c)
115 {
116     Imregion *region;
117     int     i, j;
118     double  x1, x2, y1, y2;
119     double  pix11, pix12, pix21, pix22;
120     double  pixval;
121 
122     if (image == NULL || image->vtype == ptr_v)
123         return (0);
124 
125     r -= 0.5;
126     c -= 0.5;
127 
128     region = image->region;
129     if (r < region->ly || r + 1 >= region->uy || c < region->lx || c + 1 >= region->ux)
130         return (0);
131 
132     i = (int) r;
133     y1 = r - i;
134     y2 = 1.0 - y1;
135     j = (int) c;
136     x1 = c - j;
137     x2 = 1.0 - x1;
138 
139     IM_PIX_GET(image, i, j, pix11);
140     IM_PIX_GET(image, i, j + 1, pix12);
141     IM_PIX_GET(image, i + 1, j, pix21);
142     IM_PIX_GET(image, i + 1, j + 1, pix22);
143 
144     pixval = (pix11 * x2 + pix12 * x1) * y2 + (pix21 * x2 + pix22 * x1) * y1;
145 
146     return (pixval);
147 }
148 
149 double          im_sub_pixqf(Imrect *image, double y, double x)
150 /* fits simple quadratic function to image and returns interpolated value
151                                                                 NAT 10/10/90 */
152 {
153     Imregion       *region = image->region;
154     double          pixval[3][3];
155     float           a, b, c, d, e, f;
156     double          inter;
157     short           i, j, n, m;
158     float           xs, ys;
159 
160     i = tina_int(x-1);
161     j = tina_int(y-1);
162 
163     xs = (float)( x -  i - 1.5 );
164     ys = (float)(y -  j - 1.5 );
165 
166     if (j < region->ly || j > region->uy - 3
167         || i < region->lx || i > region->ux - 3)
168         return (0);
169     for (n = 0; n < 3; n++)
170     {
171         for (m = 0; m < 3; m++)
172         {
173             IM_PIX_GET(image, j + n, i + m, pixval[n][m]);
174         }
175     }
176 
177     a = (float)pixval[1][1];
178     b = (float)((pixval[0][2] - pixval[0][0]
179         + pixval[1][2] - pixval[1][0]
180         + pixval[2][2] - pixval[2][0]) / 6.0);
181     c = (float)((pixval[2][0] - pixval[0][0]
182         + pixval[2][1] - pixval[0][1]
183         + pixval[2][2] - pixval[0][2]) / 6.0);
184     d = (float)((pixval[0][0] - 2.0 * pixval[0][1] + pixval[0][2]
185         + 3.0 * pixval[1][0] - 6.0 * pixval[1][1] + 3.0 * pixval[1][2]
186         + pixval[2][0] - 2.0 * pixval[2][1] + pixval[2][2]) / 10.0);
187     e = (float)((pixval[0][0] - pixval[2][0]
188         + pixval[2][2] - pixval[0][2]) / 4.0);
189     f = (float)((pixval[0][0] + 3.0 * pixval[0][1] + pixval[0][2]
190         - 2.0 * pixval[1][0] - 6.0 * pixval[1][1] - 2.0 * pixval[1][2]
191         + pixval[2][0] + 3.0 * pixval[2][1] + pixval[2][2]) / 10.0);
192 
193     xs *=(float)2.0;
194     ys *=(float)2.0;
195     inter = a + 0.5*(b * xs + c * ys + d * xs * xs + e * xs * ys + f * ys * ys);
196 
197     return (inter);
198 }
199 
200 Complex im_sub_pixz(Imrect * im, double y, double x)
201 {
202     int     i, j;
203     double  x1, x2, y1, y2;
204     Complex z00 = {Complex_id};
205     Complex z10 = {Complex_id};
206     Complex z01 = {Complex_id};
207     Complex z11 = {Complex_id};
208     double  re00, re01, re10, re11, re0;
209     double  im00, im01, im10, im11, im0;
210 
211     y -= 0.5;
212     i = (int) y;
213     y1 = y - i;
214     y2 = 1.0 - y1;
215     x -= 0.5;
216     j = (int) x;
217     x1 = x - j;
218     x2 = 1.0 - x1;
219 
220     z00 = im_get_pixz(im, i, j);
221     z01 = im_get_pixz(im, i, j + 1);
222     z10 = im_get_pixz(im, i + 1, j);
223     z11 = im_get_pixz(im, i + 1, j + 1);
224 
225     re00 = cmplx_re(z00);
226     re10 = cmplx_re(z10);
227     re01 = cmplx_re(z01);
228     re11 = cmplx_re(z11);
229     re0 = y2 * (x2 * re00 + x1 * re01) + y1 * (x2 * re10 + x1 * re11);
230 
231     im00 = cmplx_im(z00);
232     im10 = cmplx_im(z10);
233     im01 = cmplx_im(z01);
234     im11 = cmplx_im(z11);
235     im0 = y2 * (x2 * im00 + x1 * im01) + y1 * (x2 * im10 + x1 * im11);
236 
237     return (cmplx(re0, im0));
238 }
239 
240 void    im_get_row(int *line, Imrect * image, int i, int from, int to)
241 {
242     int     j;
243     Imregion *region;
244     double pixval;
245 
246     for (j = from; j < to; ++j)
247         line[j] = 0;
248 
249     if (image == NULL || image->vtype == ptr_v)
250         return;
251 
252     region = image->region;
253     if (i < region->ly || i >= region->uy)
254         return;
255 
256     if (from >= region->ux || to < region->lx)
257         return;
258 
259     if (to > region->ux)
260         to = region->ux;
261 
262     if (from < region->lx)
263         from = region->lx;
264 
265     for (j = from; j < to; ++j)
266     {
267         IM_PIX_GET(image, i, j, pixval);
268         line[j] = (int)pixval;
269     }
270 }
271 
272 void    im_get_col(int *line, Imrect * image, int i, int from, int to)
273 {
274     int     j;
275     Imregion *region;
276     double pixval;
277 
278     for (j = from; j < to; ++j)
279         line[j] = 0;
280 
281     if (image == NULL || image->vtype == ptr_v)
282         return;
283 
284     region = image->region;
285     if (i < region->lx || i >= region->ux)
286         return;
287 
288     if (from >= region->uy || to < region->ly)
289         return;
290 
291     if (to > region->uy)
292         to = region->uy;
293 
294     if (from < region->ly)
295         from = region->ly;
296 
297     for (j = from; j < to; ++j)
298     {
299         IM_PIX_GET(image, j, i, pixval);
300         line[j] = (int) pixval;
301     }
302 }
303 
304 void    im_get_rowf(float *line, Imrect * image, int i, int from, int to)
305 {
306     int     j;
307     Imregion *region;
308     double pixval;
309 
310     for (j = from; j < to; ++j)
311         line[j] = (float) 0.0;
312 
313     if (image == NULL || image->vtype == ptr_v)
314         return;
315 
316     region = image->region;
317     if (i < region->ly || i >= region->uy)
318         return;
319 
320     if (from >= region->ux || to < region->lx)
321         return;
322 
323     if (to > region->ux)
324         to = region->ux;
325 
326     if (from < region->lx)
327         from = region->lx;
328 
329     for (j = from; j < to; ++j)
330     {
331         IM_PIX_GET(image, i, j, pixval);
332         line[j] = (float) pixval;
333     }
334 }
335 
336 void    im_get_colf(float *line, Imrect * image, int i, int from, int to)
337 {
338     int     j;
339     Imregion *region;
340     double pixval;
341 
342     for (j = from; j < to; ++j)
343         line[j] = (float) 0.0;
344 
345     if (image == NULL || image->vtype == ptr_v)
346         return;
347 
348     region = image->region;
349     if (i < region->lx || i >= region->ux)
350         return;
351 
352     if (from >= region->uy || to < region->ly)
353         return;
354 
355     if (to > region->uy)
356         to = region->uy;
357 
358     if (from < region->ly)
359         from = region->ly;
360 
361     for (j = from; j < to; ++j)
362     {
363         IM_PIX_GET(image, j, i, pixval);
364         line[j] = (float) pixval;
365     }
366 }
367 
368 void    im_get_rowz(Complex * line, Imrect * image, int i, int from, int to)
369 {
370     int     j;
371     Imregion *region;
372 
373     for (j = from; j < to; ++j)
374         line[j] = cmplx_zero();
375 
376     if (image == NULL || image->vtype == ptr_v)
377         return;
378 
379     region = image->region;
380     if (i < region->ly || i >= region->uy)
381         return;
382 
383     if (from >= region->ux || to < region->lx)
384         return;
385 
386     if (to > region->ux)
387         to = region->ux;
388 
389     if (from < region->lx)
390         from = region->lx;
391 
392     if (image->vtype == complex_v)
393     {
394         for (j = from; j < to; ++j)
395             line[j] = IM_COMPLEX(image, i, j);
396     } else
397     {
398         for (j = from; j < to; ++j)
399             IM_PIX_GET(image, i, j, line[j].x);
400     }
401 }
402 
403 void    im_get_colz(Complex * line, Imrect * image, int i, int from, int to)
404 {
405     int     j;
406     Imregion *region;
407 
408     for (j = from; j < to; ++j)
409         line[j] = cmplx_zero();
410 
411     if (image == NULL || image->vtype == ptr_v)
412         return;
413 
414     region = image->region;
415     if (i < region->lx || i >= region->ux)
416         return;
417 
418     if (from >= region->uy || to < region->ly)
419         return;
420 
421     if (to > region->uy)
422         to = region->uy;
423 
424     if (from < region->ly)
425         from = region->ly;
426 
427     if (image->vtype == complex_v)
428     {
429         for (j = from; j < to; ++j)
430             line[j] = IM_COMPLEX(image, j, i);
431     } else
432     {
433         for (j = from; j < to; ++j)
434             IM_PIX_GET(image, j, i, line[j].x);
435     }
436 }
437 
438 void    im_get_pos_diag(int *line, Imrect * image, int x, int y, int len)
439 {
440     int     j;
441     int     minoff;
442     Imregion *region;
443 
444     for (j = 0; j < len; ++j)
445         line[j] = 0;
446 
447     if (image == NULL)
448         return;
449 
450     region = image->region;
451     if (x >= region->ux || (x + len) < region->lx)
452         return;
453 
454     if (y >= region->uy || (y + len) < region->ly)
455         return;
456 
457     minoff = MIN(x - region->lx, y - region->ly);
458     if (minoff < 0)
459     {
460         x -= minoff;
461         y -= minoff;
462         len += minoff;
463     }
464     minoff = MIN(region->ux - x - len, region->uy - y - len);
465     if (minoff < 0)
466         len += minoff;
467 
468     switch (image->vtype)
469     {
470     case char_v:
471         {
472             char  **array = (char **) image->data;
473 
474             for (j = 0; j < len; j++)
475                 line[j] = array[y + j][x + j];
476         }
477         break;
478     case uchar_v:
479         {
480             unsigned char **array = (unsigned char **) image->data;
481 
482             for (j = 0; j < len; j++)
483                 line[j] = array[y + j][x + j];
484         }
485         break;
486     case short_v:
487         {
488             short **array = (short **) image->data;
489 
490             for (j = 0; j < len; j++)
491                 line[j] = array[y + j][x + j];
492         }
493         break;
494     case ushort_v:
495         {
496             unsigned short **array = (unsigned short **) image->data;
497 
498             for (j = 0; j < len; j++)
499                 line[j] = array[y + j][x + j];
500         }
501         break;
502     case int_v:
503         {
504             int   **array = (int **) image->data;
505 
506             for (j = 0; j < len; j++)
507                 line[j] = array[y + j][x + j];
508         }
509         break;
510     case uint_v:
511         {
512             unsigned int **array = (unsigned int **) image->data;
513 
514             for (j = 0; j < len; j++)
515                 line[j] = array[y + j][x + j];
516         }
517         break;
518     case float_v:
519         {
520             float **array = (float **) image->data;
521 
522             for (j = 0; j < len; j++)
523             {
524                 float   gl = array[y + j][x + j];
525 
526                 line[j] = (int) ((gl > 0) ? gl + 0.5 : gl - 0.5);
527             }
528         }
529         break;
530     case double_v:
531         {
532             double **array = (double **) image->data;
533 
534             for (j = 0; j < len; j++)
535             {
536                 double  gl = array[y + j][x + j];
537 
538                 line[j] = (int)((gl > 0) ? gl + 0.5 : gl - 0.5);
539             }
540         }
541         break;
542     }
543 }
544 
545 void    im_get_neg_diag(int *line, Imrect * image, int x, int y, int len)
546 {
547     int     j;
548     int     minoff;
549     Imregion *region;
550 
551     for (j = 0; j < len; ++j)
552         line[j] = 0;
553 
554     if (image == NULL)
555         return;
556 
557     region = image->region;
558     if (x < region->lx || (x - len) >= region->ux)
559         return;
560 
561     if (y >= region->uy || (y + len) < region->ly)
562         return;
563 
564     minoff = MIN(region->ux - x, y - region->ly);
565     if (minoff < 0)
566     {
567         x += minoff;
568         y -= minoff;
569         len += minoff;
570     }
571     minoff = MIN(x - len - region->lx, region->uy - y - len);
572     if (minoff < 0)
573         len += minoff;
574 
575 
576     switch (image->vtype)
577     {
578     case char_v:
579         {
580             char  **array = (char **) image->data;
581 
582             for (j = 0; j < len; j++)
583                 line[j] = array[y + j][x - j];
584         }
585         break;
586     case uchar_v:
587         {
588             unsigned char **array = (unsigned char **) image->data;
589 
590             for (j = 0; j < len; j++)
591                 line[j] = array[y + j][x - j];
592         }
593         break;
594     case short_v:
595         {
596             short **array = (short **) image->data;
597 
598             for (j = 0; j < len; j++)
599                 line[j] = array[y + j][x - j];
600         }
601         break;
602     case ushort_v:
603         {
604             unsigned short **array = (unsigned short **) image->data;
605 
606             for (j = 0; j < len; j++)
607                 line[j] = array[y + j][x - j];
608         }
609         break;
610     case int_v:
611         {
612             int   **array = (int **) image->data;
613 
614             for (j = 0; j < len; j++)
615                 line[j] = array[y + j][x - j];
616         }
617         break;
618     case uint_v:
619         {
620             unsigned int **array = (unsigned int **) image->data;
621 
622             for (j = 0; j < len; j++)
623                 line[j] = array[y + j][x - j];
624         }
625         break;
626     case float_v:
627         {
628             float **array = (float **) image->data;
629 
630             for (j = 0; j < len; j++)
631             {
632                 float   gl = array[y + j][x - j];
633 
634                 line[j] = (int)((gl > 0) ? gl + 0.5 : gl - 0.5);
635             }
636         }
637         break;
638     case double_v:
639         {
640             double **array = (double **) image->data;
641 
642             for (j = 0; j < len; j++)
643             {
644                 double  gl = array[y + j][x - j];
645 
646                 line[j] = (int)((gl > 0) ? gl + 0.5 : gl - 0.5);
647             }
648         }
649         break;
650     }
651 }
652 
653 void    im_get_pos_diagf(float *line, Imrect * image, int x, int y, int len)
654 {
655     int     j;
656     int     minoff;
657     Imregion *region;
658 
659     for (j = 0; j < len; ++j)
660         line[j] = (float) 0.0;
661 
662     if (image == NULL)
663         return;
664 
665     region = image->region;
666     if (x >= region->ux || (x + len) < region->lx)
667         return;
668 
669     if (y >= region->uy || (y + len) < region->ly)
670         return;
671 
672     minoff = MIN(x - region->lx, y - region->ly);
673     if (minoff < 0)
674     {
675         x -= minoff;
676         y -= minoff;
677         len += minoff;
678     }
679     minoff = MIN(region->ux - x - len, region->uy - y - len);
680     if (minoff < 0)
681         len += minoff;
682 
683     switch (image->vtype)
684     {
685     case char_v:
686         {
687             char  **array = (char **) image->data;
688 
689             for (j = 0; j < len; j++)
690                 line[j] = array[y + j][x + j];
691         }
692         break;
693     case uchar_v:
694         {
695             unsigned char **array = (unsigned char **) image->data;
696 
697             for (j = 0; j < len; j++)
698                 line[j] = array[y + j][x + j];
699         }
700         break;
701     case short_v:
702         {
703             short **array = (short **) image->data;
704 
705             for (j = 0; j < len; j++)
706                 line[j] = array[y + j][x + j];
707         }
708         break;
709     case ushort_v:
710         {
711             unsigned short **array = (unsigned short **) image->data;
712 
713             for (j = 0; j < len; j++)
714                 line[j] = array[y + j][x + j];
715         }
716         break;
717     case int_v:
718         {
719             int   **array = (int **) image->data;
720 
721             for (j = 0; j < len; j++)
722                 line[j] = (float) array[y + j][x + j];
723         }
724         break;
725     case uint_v:
726         {
727             unsigned int **array = (unsigned int **) image->data;
728 
729             for (j = 0; j < len; j++)
730                 line[j] = (float) array[y + j][x + j];
731         }
732         break;
733     case float_v:
734         {
735             float **array = (float **) image->data;
736 
737             for (j = 0; j < len; j++)
738             {
739                 float   gl = array[y + j][x + j];
740 
741                 line[j] = (float)((gl > 0) ? gl + 0.5 : gl - 0.5);
742             }
743         }
744         break;
745     case double_v:
746         {
747             double **array = (double **) image->data;
748 
749             for (j = 0; j < len; j++)
750             {
751                 double  gl = array[y + j][x + j];
752 
753                 line[j] = (float)((gl > 0) ? gl + 0.5 : gl - 0.5);
754             }
755         }
756         break;
757     }
758 }
759 
760 void    im_get_neg_diagf(float *line, Imrect * image, int x, int y, int len)
761 {
762     int     j;
763     int     minoff;
764     Imregion *region;
765 
766     for (j = 0; j < len; ++j)
767         line[j] = (float) 0.0;
768 
769     if (image == NULL)
770         return;
771 
772     region = image->region;
773     if (x < region->lx || (x - len) >= region->ux)
774         return;
775 
776     if (y >= region->uy || (y + len) < region->ly)
777         return;
778 
779     minoff = MIN(region->ux - x, y - region->ly);
780     if (minoff < 0)
781     {
782         x += minoff;
783         y -= minoff;
784         len += minoff;
785     }
786     minoff = MIN(x - len - region->lx, region->uy - y - len);
787     if (minoff < 0)
788         len += minoff;
789 
790     switch (image->vtype)
791     {
792     case char_v:
793         {
794             char  **array = (char **) image->data;
795 
796             for (j = 0; j < len; j++)
797                 line[j] = array[y + j][x - j];
798         }
799         break;
800     case uchar_v:
801         {
802             unsigned char **array = (unsigned char **) image->data;
803 
804             for (j = 0; j < len; j++)
805                 line[j] = array[y + j][x - j];
806         }
807         break;
808     case short_v:
809         {
810             short **array = (short **) image->data;
811 
812             for (j = 0; j < len; j++)
813                 line[j] = array[y + j][x - j];
814         }
815         break;
816     case ushort_v:
817         {
818             unsigned short **array = (unsigned short **) image->data;
819 
820             for (j = 0; j < len; j++)
821                 line[j] = array[y + j][x - j];
822         }
823         break;
824     case int_v:
825         {
826             int   **array = (int **) image->data;
827 
828             for (j = 0; j < len; j++)
829                 line[j] = (float) array[y + j][x - j];
830         }
831         break;
832     case uint_v:
833         {
834             unsigned int **array = (unsigned int **) image->data;
835 
836             for (j = 0; j < len; j++)
837                 line[j] = (float) array[y + j][x - j];
838         }
839         break;
840     case float_v:
841         {
842             float **array = (float **) image->data;
843 
844             for (j = 0; j < len; j++)
845             {
846                 float   gl = array[y + j][x - j];
847 
848                 line[j] = (float)((gl > 0) ? gl + 0.5 : gl - 0.5);
849             }
850         }
851         break;
852     case double_v:
853         {
854             double **array = (double **) image->data;
855 
856             for (j = 0; j < len; j++)
857             {
858                 double  gl = array[y + j][x - j];
859 
860                 line[j] = (float)((gl > 0) ? gl + 0.5 : gl - 0.5);
861             }
862         }
863         break;
864     }
865 }
866 

~ [ 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.