1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
/* 
**! module Image 
**! class Image 
*/ 
 
#include "global.h" 
 
#include <math.h> 
#include <ctype.h> 
 
#include "pike_macros.h" 
#include "object.h" 
#include "interpret.h" 
#include "svalue.h" 
#include "threads.h" 
#include "pike_error.h" 
 
#include "image.h" 
 
#include <builtin_functions.h> 
 
 
#define sp Pike_sp 
 
extern struct program *image_program; 
#ifdef THIS 
#undef THIS /* Needed for NT */ 
#endif 
#define THIS ((struct image *)(Pike_fp->current_storage)) 
#define THISOBJ (Pike_fp->current_object) 
 
static const double c0=0.70710678118654752440; 
static const double my_PI=3.14159265358979323846; 
 
#if 0 
#include <sys/resource.h> 
#define CHRONO(X) chrono(X); 
 
static void chrono(char *x) 
{ 
   struct rusage r; 
   static struct rusage rold; 
   getrusage(RUSAGE_SELF,&r); 
   fprintf(stderr,"%s: %ld.%06ld - %ld.%06ld\n",x, 
           (long)r.ru_utime.tv_sec,(long)r.ru_utime.tv_usec, 
 
           (long)(((r.ru_utime.tv_usec-rold.ru_utime.tv_usec<0)?-1:0) 
                  +r.ru_utime.tv_sec-rold.ru_utime.tv_sec), 
           (long)(((r.ru_utime.tv_usec-rold.ru_utime.tv_usec<0)?1000000:0) 
                  + r.ru_utime.tv_usec-rold.ru_utime.tv_usec) 
      ); 
 
   rold=r; 
} 
#else 
#define CHRONO(X) 
#endif 
 
/* 
**! method object orient(void|array(object) something) 
**! method array(object) orient4() 
**!     Draws images describing the orientation 
**!     of the current image. 
**! 
**!     <tt>orient</tt> gives an HSV image 
**!     (run a <ref>hsv_to_rgb</ref> pass on it 
**!     to get a viewable image). 
**!     corresponding to the angle of the 
**!     orientation: 
**!     <pre>      |      /    -    \ 
**!          hue=  0     64   128  192  (=red in an hsv image) 
**!              purple cyan green red 
**!     </pre> 
**!     Red, green and blue channels are added 
**!     and not compared separately. 
**! 
**!     If you first use orient4 you can give its 
**!     output as input to this function. 
**! 
**!     The <tt>orient4</tt> function gives back 
**!     4 image objects, corresponding to the 
**!     amount of different directions, see above. 
**! 
**! returns an image or an array of the four new image objects 
**! 
**! note 
**!     experimental status; may not be exact the same 
**!     output in later versions 
*/ 
static inline int my_abs(int a) { return (a<0)?-a:a; } 
 
static void _image_orient(struct image *source, 
                          struct object *o[5], 
                          struct image *img[5]) 
{ 
   int i; 
   struct { int x,y; } or[4]={ {1,0}, {1,1}, {0,1}, {-1,1} }; 
   int x,y; 
 
   for (i=0; i<5; i++) 
   { 
      push_int(source->xsize); 
      push_int(source->ysize); 
      o[i]=clone_object(image_program,2); 
      img[i]=get_storage(o[i],image_program); 
      push_object(o[i]); 
   } 
 
THREADS_ALLOW(); 
CHRONO("start"); 
   for (i=0; i<4; i++) /* four directions */ 
   { 
      rgb_group *d=img[i]->img; 
      rgb_group *s=source->img; 
      int xz=source->xsize; 
      int yz=source->ysize; 
      int xd=or[i].x; 
      int yd=or[i].y; 
 
      for(x=1; x<xz-1; x++) 
         for(y=1; y<yz-1; y++) 
         { 
#define FOOBAR(CO) \ 
  d[x+y*xz].CO \ 
     = \ 
  (COLORTYPE) \ 
     my_abs( s[(x+xd)+(y+yd)*xz].CO - s[(x-xd)+(y-yd)*xz].CO ) 
 
            FOOBAR(r); 
            FOOBAR(g); 
            FOOBAR(b); 
 
#undef FOOBAR 
         } 
   } 
CHRONO("end"); 
THREADS_DISALLOW(); 
} 
 
void image_orient(INT32 args) 
{ 
  struct object *o[5]; 
  struct image *img[5],*this,*img1; 
  int n; 
  rgb_group *d,*s1,*s2,*s3,*s0; 
  double mag; 
  int i, w, h; 
 
  CHECK_INIT(); 
 
  this=THIS; 
 
  if (args) 
  { 
    if (TYPEOF(sp[-args]) == T_INT) 
      mag=sp[-args].u.integer; 
    else if (TYPEOF(sp[-args]) == T_FLOAT) 
      mag=sp[-args].u.float_number; 
    else { 
      SIMPLE_ARG_TYPE_ERROR("orient",1,"int|float"); 
      UNREACHABLE(mag=0.0); 
    } 
  } 
  else mag=1.0; 
 
  if (args==1) 
    pop_n_elems(args); 
 
  if (args>1) 
  { 
    if (TYPEOF(sp[1-args]) != T_ARRAY) 
      SIMPLE_ARG_TYPE_ERROR("orient",2,"array"); 
    if (sp[1-args].u.array->size!=4) 
      Pike_error("The array given as argument 2 to orient do not have size 4\n"); 
    for(i=0; i<4; i++) 
      if ((TYPEOF(sp[1-args].u.array->item[i]) != T_OBJECT) || 
          (!(sp[1-args].u.array->item[i].u.object)) || 
          (sp[1-args].u.array->item[i].u.object->prog!=image_program)) 
        Pike_error("The array given as argument 2 to orient do not contain images\n"); 
    img1=(struct image*)sp[1-args].u.array->item[0].u.object->storage; 
 
    w=this->xsize; 
    h=this->ysize; 
 
    for(i=0; i<4; i++) 
    { 
      img1=(struct image*)sp[1-args].u.array->item[i].u.object->storage; 
      if ((img1->xsize!=w)|| 
          (img1->ysize!=h)) 
        Pike_error("The images in the array given as argument 2 to orient have different sizes\n"); 
    } 
    for(i=0; i<4; i++) 
        img[i]=get_storage(sp[1-args].u.array->item[i].u.object,image_program); 
    pop_n_elems(args); 
    push_int(this->xsize); 
    push_int(this->ysize); 
    o[4]=clone_object(image_program,2); 
    img[4]=get_storage(o[4],image_program); 
    push_object(o[4]); 
    w=1; 
  } 
  else 
  { 
    _image_orient(this,o,img); 
    w=0; 
  } 
  s0=img[0]->img; 
  s1=img[1]->img; 
  s2=img[2]->img; 
  s3=img[3]->img; 
  d=img[4]->img; 
 
THREADS_ALLOW(); 
CHRONO("begin hsv..."); 
  n=this->xsize*this->ysize; 
  while (n--) 
  { 
     /* Första färg, sista mörkhet */ 
     double j=(s0->r+s0->g+s0->b-s2->r-s2->g-s2->b)/3.0; 
                /* riktning - - riktning | */ 
 
     double h=(s1->r+s1->g+s1->b-s3->r-s3->g-s3->b)/3.0; 
                /* riktning \ - riktning / */ 
 
     int z,w; 
 
     if (my_abs((int)h) > my_abs((int)j)) 
        if (h) { 
          z = -(int)(32*(j/h)+(h>0)*128+64); 
          w = my_abs((int)h); 
        } 
        else z=0,w=0; 
     else { 
        if (j) { 
          z = -(int)(-32*(h/j)+(j>0)*128+128); 
          w = my_abs((int)j); 
        } 
        else z=0,w=0; 
     } 
 
     d->r=(COLORTYPE)z; 
     d->g=255; 
     d->b = MINIMUM((COLORTYPE)(w*mag), 255); 
 
     d++; 
     s0++; 
     s1++; 
     s2++; 
     s3++; 
  } 
CHRONO("end hsv..."); 
THREADS_DISALLOW(); 
 
  if (!w) 
  { 
    add_ref(o[4]); 
    pop_n_elems(5); 
    push_object(o[4]); 
  } 
} 
 
 
void image_orient4(INT32 args) 
{ 
  struct object *o[5]; 
  struct image *img[5]; 
 
  CHECK_INIT(); 
 
  pop_n_elems(args); 
  _image_orient(THIS,o,img); 
 
  pop_n_elems(1); 
  f_aggregate(4); 
}