pikl 0.2.7-x86-mswin32 → 0.2.8-x86-mswin32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,266 @@
1
+ #include "pikl_effect3.h"
2
+
3
+ static unsigned char lighting(unsigned char color, double dx, double dy, double dz,
4
+ double mil, double env, double *light);
5
+
6
+ //=============================================================================
7
+ // pkl_illust
8
+ //=============================================================================
9
+ PKLExport int pkl_illust(PKLImage pkl, int edge, int gammaint)
10
+ {
11
+ int GAP=10;
12
+ int sobel1[9] = { 1, 0, -1, 2, 0, -2, 1, 0, -1 }; //sobel filter
13
+ int sobel2[9] = { 1, 2, 1, 0, 0, 0, -1, -2, -1 }; //sobel filter
14
+ int i, j, k, s, t, px, py, rp, r1, r2, rc, gt;
15
+ int wx[PKL_CHANNEL], wy[PKL_CHANNEL], rate[PKL_CHANNEL];
16
+ unsigned char *wrk;
17
+ double gamma;
18
+ int th1, th2, th3;
19
+
20
+ wrk = malloc(pkl->width*pkl->height*pkl->channel);
21
+ if(!wrk) return(1);
22
+ memset(wrk, 0, pkl->width*pkl->height*pkl->channel);
23
+
24
+ gamma = 1.0/((double)gammaint/100.0);
25
+ th1 = (int)(pow(128.0/255.0, gamma)*255.0);
26
+ th2 = (int)(pow( 96.0/255.0, gamma)*255.0);
27
+ th3 = (int)(pow( 64.0/255.0, gamma)*255.0);
28
+
29
+ for(i=0; i<pkl->height; i++){
30
+ for(j=0; j<pkl->width; j++){
31
+
32
+ memset(wx, 0, sizeof(wx));
33
+ memset(wy, 0, sizeof(wy));
34
+
35
+ for(s=0; s<3; s++){
36
+ for(t=0; t<3; t++){
37
+ px = (j+t-1)<0 ? 0 : (j+t-1)>pkl->width ? pkl->width-1 : (j+t-1);
38
+ py = (i+s-1)<0 ? 0 : (i+s-1)>pkl->height ? pkl->height-1 : (i+s-1);
39
+ for(k=0; k<pkl->channel; k++){
40
+ wx[k] += pkl->image[(py*pkl->width+px)*pkl->channel+k] * sobel1[s*3+t];
41
+ wy[k] += pkl->image[(py*pkl->width+px)*pkl->channel+k] * sobel2[s*3+t];
42
+ }
43
+ }
44
+ }
45
+
46
+ for(k=0; k<pkl->channel; k++)
47
+ rate[k] = (int)(sqrt((double)(wx[k]*wx[k]+wy[k]*wy[k]))/8.0);
48
+ rp = 0;
49
+ for(k=0; k<pkl->channel; k++)
50
+ if(rate[k]>rp) rp=rate[k];
51
+ r1 = (rp<edge) ? 255 : (rp<edge+GAP) ? 255-255*(rp-edge)/GAP : 0;
52
+
53
+ gt = 0;
54
+ for(k=0; k<pkl->channel; k++)
55
+ gt += pkl->image[(i*pkl->width+j)*pkl->channel+k];
56
+ gt /= pkl->channel;
57
+ r2 = (gt>th1) ? 255 : (gt>th2) ? 128 : (gt>th3) ? 64 : 0;
58
+
59
+ rc = PKL_COLOR_CLIP(r1*r2/256);
60
+ for(k=0; k<pkl->channel; k++)
61
+ wrk[(i*pkl->width+j)*pkl->channel+k] = rc;
62
+ }
63
+ }
64
+
65
+ free(pkl->image);
66
+ pkl->image = wrk;
67
+ return(0);
68
+ }
69
+
70
+ //=============================================================================
71
+ // pkl_color_emboss
72
+ //=============================================================================
73
+ PKLExport int pkl_color_emboss(PKLImage pkl, double mil, double env)
74
+ {
75
+ int sobel1[9] = { 1, 0, -1, 2, 0, -2, 1, 0, -1 }; //sobel filter
76
+ int sobel2[9] = { 1, 2, 1, 0, 0, 0, -1, -2, -1 }; //sobel filter
77
+ int i, j, k, s, t, wx, wy, gt, px, py;
78
+ unsigned char *wrk, color[PKL_CHANNEL];
79
+ double dx, dy, dz, light[3], abl;
80
+
81
+ wrk = malloc(pkl->width*pkl->height*pkl->channel);
82
+ if(!wrk) return(1);
83
+ memset(wrk, 0, pkl->width*pkl->height*pkl->channel);
84
+
85
+ //�����x�N�g����P�ʃx�N�g���ɕ␳����
86
+ abl = sqrt(-1.0*-1.0 + -1.0*-1.0 + 1.0*1.0);
87
+ light[0] = -1.0/abl;
88
+ light[1] = -1.0/abl;
89
+ light[2] = 1.0/abl;
90
+
91
+ for(i=0; i<pkl->height; i++){
92
+ for(j=0; j<pkl->width; j++){
93
+ wx=wy=0;
94
+ for(s=0; s<3; s++){
95
+ for(t=0; t<3; t++){
96
+ px = (j+t-1)<0 ? 0 : (j+t-1)>pkl->width ? pkl->width-1 : (j+t-1);
97
+ py = (i+s-1)<0 ? 0 : (i+s-1)>pkl->height ? pkl->height-1 : (i+s-1);
98
+
99
+ gt=0;
100
+ for(k=0; k<pkl->channel; k++)
101
+ gt += pkl->image[(py*pkl->width+px)*pkl->channel+k];
102
+ gt /= pkl->channel;
103
+ wx += gt*sobel1[s*3+t];
104
+ wy += gt*sobel2[s*3+t];
105
+ }
106
+ }
107
+ dx = (double)wx;
108
+ dy = (double)wy;
109
+ dz = 16.0;
110
+ for(k=0; k<pkl->channel; k++)
111
+ color[k] = lighting(pkl->image[(py*pkl->width+px)*pkl->channel+k], dx, dy, dz, mil, env, light);
112
+ memcpy(&wrk[(i*pkl->width+j)*pkl->channel], color, pkl->channel);
113
+ }
114
+ }
115
+
116
+ free(pkl->image);
117
+ pkl->image = wrk;
118
+ return(0);
119
+ }
120
+
121
+ //=============================================================================
122
+ // �A�e�t��
123
+ // (dx,dy,dz)��@���x�N�g���Ƃ���i�P�ʃx�N�g���łȂ��Ă��悢�j
124
+ //=============================================================================
125
+ static unsigned char lighting(unsigned char color, double dx, double dy, double dz,
126
+ double mil, double env, double *light)
127
+ {
128
+ double abl, rz, env2, cosT, cosT2;
129
+ int res;
130
+
131
+ //�@���x�N�g����P�ʃx�N�g���ɕ␳
132
+ abl = sqrt(dx*dx + dy*dy + dz*dz);
133
+ cosT = (dx/abl)*light[0] + (dy/abl)*light[1] + (dz/abl)*light[2];
134
+ rz = 2.0*cosT*(dz/abl) - light[2];
135
+ cosT2 = (rz>0.0) ? pow(rz, 12.0) : 0.0;
136
+ cosT = (cosT<0.0) ? 0.0 : cosT;
137
+
138
+ env2=env*255.0/100.0;
139
+ res=(int)((cosT*255.0+env2)*(double)color/255.0);
140
+
141
+ if(res>color) res=color;
142
+ res+=(mil*cosT2);
143
+
144
+ return(PKL_COLOR_CLIP(res));
145
+ }
146
+
147
+ //=============================================================================
148
+ // pkl_mosaic
149
+ //=============================================================================
150
+ PKLExport int pkl_mosaic(PKLImage pkl, int msx, int msy)
151
+ {
152
+ unsigned char *wrk;
153
+ int mpx, mpy; //���U�C�N�̑傫��
154
+ int mlx, mly; //�摜�̕�����
155
+ int i, j, k, sx, sy, ex, ey, px, py, mm;
156
+ long color[PKL_CHANNEL];
157
+
158
+ wrk = malloc(pkl->width*pkl->height*pkl->channel);
159
+ if(!wrk) return(1);
160
+ memset(wrk, 0, pkl->width*pkl->height*pkl->channel);
161
+
162
+ mpx = (msx<1) ? 1 : (msx>=pkl->width) ? pkl->width-1 : msx;
163
+ mpy = (msy<1) ? 1 : (msy>=pkl->height) ? pkl->height-1 : msy;
164
+ mlx = (pkl->width+mpx-1)/mpx;
165
+ mly = (pkl->height+mpy-1)/mpy;
166
+
167
+ for(i=0; i<=mly; i++){
168
+ for(j=0; j<=mlx; j++){
169
+ sx = j*mpx;
170
+ sy = i*mpy;
171
+ ex = sx+mpx>=pkl->width ? pkl->width : sx+mpx;
172
+ ey = sy+mpy>=pkl->height ? pkl->height : sy+mpy;
173
+
174
+ //1�Z���̑��a�Z�o
175
+ mm=0;
176
+ memset(color, 0, sizeof(color));
177
+ for(py=sy; py<ey; py++){
178
+ for(px=sx; px<ex; px++){
179
+ mm++;
180
+ for(k=0; k<pkl->channel; k++)
181
+ color[k] += pkl->image[(py*pkl->width+px)*pkl->channel+k];
182
+ }
183
+ }
184
+ if(!mm) continue;
185
+
186
+ //�Z���̕��ϐF���Z�o���āA�Z�����̃s�N�Z���ɓK�p
187
+ for(k=0; k<pkl->channel; k++)
188
+ color[k] = PKL_COLOR_CLIP(color[k]/mm);
189
+ for(py=sy; py<ey; py++){
190
+ for(px=sx; px<ex; px++){
191
+ for(k=0; k<pkl->channel; k++)
192
+ wrk[(py*pkl->width+px)*pkl->channel+k] = color[k];
193
+ }
194
+ }
195
+ }
196
+ }
197
+
198
+ free(pkl->image);
199
+ pkl->image = wrk;
200
+ return(0);
201
+ }
202
+
203
+ //=============================================================================
204
+ // pkl_noise
205
+ //=============================================================================
206
+ PKLExport int pkl_noise(PKLImage pkl, int noise)
207
+ {
208
+ unsigned char *wrk;
209
+ int i, j, k, sx, sy, nw, nh;
210
+
211
+ wrk = malloc(pkl->width*pkl->height*pkl->channel);
212
+ if(!wrk) return(1);
213
+ memset(wrk, 0, pkl->width*pkl->height*pkl->channel);
214
+
215
+ srand48(1);
216
+ for(i=0; i<pkl->height; i++){
217
+ for(j=0; j<pkl->width; j++){
218
+ //nw = rand()%(noise*2+1)-noise;
219
+ nw = lrand48()%(noise*2+1)-noise;
220
+ nh = lrand48()%(noise*2+1)-noise;
221
+ sx = j+nw<0 ? 0 : j+nw>=pkl->width ? pkl->width-1 : j+nw;
222
+ sy = i+nh<0 ? 0 : i+nh>=pkl->height ? pkl->height-1 : i+nh;
223
+ for(k=0; k<pkl->channel; k++)
224
+ wrk[(i*pkl->width+j)*pkl->channel+k] = pkl->image[(sy*pkl->width+sx)*pkl->channel+k];
225
+ }
226
+ }
227
+
228
+ free(pkl->image);
229
+ pkl->image = wrk;
230
+ return(0);
231
+ }
232
+
233
+ //=============================================================================
234
+ // pkl_vtr
235
+ //=============================================================================
236
+ PKLExport int pkl_vtr(PKLImage pkl, int colspace, int gst, int cst)
237
+ {
238
+ unsigned char *wrk;
239
+ int i, j, k, t;
240
+ int h=0, l_det=0;
241
+
242
+ wrk = malloc(pkl->width*pkl->height*pkl->channel);
243
+ if(!wrk) return(1);
244
+ memset(wrk, 0, pkl->width*pkl->height*pkl->channel);
245
+
246
+ for(i=0; i<pkl->height; i++){
247
+ if(l_det >= colspace){
248
+ h = !h;
249
+ l_det=0;
250
+ }
251
+ l_det++;
252
+
253
+ for(j=0; j<pkl->width; j++){
254
+ t = j+gst<0 ? 0 : j+gst>=pkl->width ? pkl->width-1 : j+gst;
255
+ for(k=0; k<pkl->channel; k++)
256
+ wrk[(i*pkl->width+j)*pkl->channel+k] =
257
+ PKL_COLOR_CLIP((pkl->image[(i*pkl->width+j)*pkl->channel+k] +
258
+ pkl->image[(i*pkl->width+t)*pkl->channel+k])/2 + (cst-127)*h);
259
+ }
260
+ }
261
+
262
+ free(pkl->image);
263
+ pkl->image = wrk;
264
+ return(0);
265
+ }
266
+
@@ -0,0 +1,12 @@
1
+ #ifndef _LIB_PIKL_EFFECT4_
2
+ #define _LIB_PIKL_EFFECT4_
3
+
4
+ #include <stdio.h>
5
+ #include <stdlib.h>
6
+ #include <string.h>
7
+ #include <math.h>
8
+
9
+ #include "pikl.h"
10
+ #include "pikl_private.h"
11
+
12
+ #endif
@@ -0,0 +1,495 @@
1
+ #include "pikl_effect4.h"
2
+
3
+ //=============================================================================
4
+ // pkl_edgeposter
5
+ //=============================================================================
6
+ PKLExport int pkl_edgeposter(PKLImage pkl, int level, int up)
7
+ {
8
+ if(pkl_posterize(pkl, level)) return(1);
9
+ if(pkl_noisecut(pkl)) return(1);
10
+ if(pkl_brightness(pkl, up)) return(1);
11
+ if(pkl_edgepaint(pkl)) return(1);
12
+ return(0);
13
+ }
14
+
15
+ //=============================================================================
16
+ // pkl_edgepaint
17
+ //=============================================================================
18
+ PKLExport int pkl_edgepaint(PKLImage pkl)
19
+ {
20
+ unsigned char *wrk, *p, *q;
21
+ int i, j, s, t, k, color, clipx, clipy, gray;
22
+ const int filter[9] = { -1, -1, -1,
23
+ -1, 8, -1,
24
+ -1, -1, -1};
25
+
26
+ wrk = malloc(pkl->width*pkl->height*pkl->channel);
27
+ if(!wrk) return(1);
28
+ memset(wrk, 0, pkl->width*pkl->height*pkl->channel);
29
+
30
+ /* convert grayscale */
31
+ for(i=0; i<pkl->width*pkl->height*pkl->channel; i+=pkl->channel){
32
+ gray=0;
33
+ for(k=0; k<pkl->channel; k++)
34
+ gray += pkl->image[i+k];
35
+ wrk[i] = PKL_COLOR_CLIP(gray/pkl->channel);
36
+ }
37
+
38
+ for(i=0; i<pkl->height; i++){
39
+ for(j=0; j<pkl->width; j++){
40
+ color=0;
41
+ for(s=0; s<3; s++){
42
+ for(t=0; t<3; t++){
43
+ clipx = (j+t)>=pkl->width ? pkl->width-1 : j+t;
44
+ clipy = (i+s)>=pkl->height ? pkl->height-1 : i+s;
45
+ color += wrk[(clipy*pkl->width+clipx) * pkl->channel] * filter[t+s*3];
46
+ }
47
+ }
48
+
49
+ if(color>0){
50
+ memset(&wrk[(i*pkl->width+j)*pkl->channel], 0, pkl->channel);
51
+ }else{
52
+ p = &wrk[(i*pkl->width+j)*pkl->channel];
53
+ q = &pkl->image[(i*pkl->width+j)*pkl->channel];
54
+ for(k=0; k<pkl->channel; k++) *p++=*q++;
55
+ }
56
+ }
57
+ }
58
+
59
+ free(pkl->image);
60
+ pkl->image = wrk;
61
+ return(0);
62
+ }
63
+
64
+ //=============================================================================
65
+ // pkl_tileslit
66
+ //=============================================================================
67
+ PKLExport int pkl_tileslit(PKLImage pkl, int area, int shift)
68
+ {
69
+ unsigned char *wrk;
70
+ int i, j, k, tx, ty;
71
+ int w, h, offsetx, offsety;
72
+
73
+ if(shift==0) return(0);
74
+ if(area<=1 && (area>pkl->width || area>pkl->height)) return(1);
75
+
76
+ wrk = malloc(pkl->width*pkl->height*pkl->channel);
77
+ if(!wrk) return(1);
78
+ memset(wrk, 0, pkl->width*pkl->height*pkl->channel);
79
+
80
+ //invert
81
+ for(i=0; i<pkl->height*pkl->width*pkl->channel; i++)
82
+ wrk[i] = (pkl->image[i] ^ 0xff);
83
+
84
+ srand48(1);
85
+ for(i=0; i<pkl->height; i+=area){
86
+ for(j=0; j<pkl->width; j+=area){
87
+ w = j+area>pkl->width ? pkl->width-j : area;
88
+ h = i+area>pkl->height ? pkl->height : i+area;
89
+
90
+ //�i�q�̂��炵��
91
+ //offsetx = rand()%(shift*2+1)-shift;
92
+ //offsety = rand()%(shift*2+1)-shift;
93
+ offsetx = lrand48()%(shift*2+1)-shift;
94
+ offsety = lrand48()%(shift*2+1)-shift;
95
+
96
+ for(k=i; k<h; k++){
97
+ tx = j+offsetx<0 ? 0 : j+offsetx>=pkl->width ? pkl->width-1 : j+offsetx;
98
+ ty = k+offsety<0 ? 0 : k+offsety>=pkl->height ? pkl->height-1 : k+offsety;
99
+ memcpy(&wrk[(ty*pkl->width+tx)*pkl->channel], &pkl->image[(k*pkl->width+j)*pkl->channel], w*pkl->channel);
100
+ }
101
+ }
102
+ }
103
+
104
+ free(pkl->image);
105
+ pkl->image = wrk;
106
+ return(0);
107
+ }
108
+
109
+ //=============================================================================
110
+ // pkl_grid
111
+ //=============================================================================
112
+ PKLExport int pkl_grid(PKLImage pkl, int msx, int msy, int color)
113
+ {
114
+ unsigned char *p;
115
+ int i, j, k, mpx, mpy;
116
+
117
+ //��U���U�C�N��
118
+ if(pkl_mosaic(pkl, msx, msy)) return(1);
119
+
120
+ mpx = (msx<1) ? 1 : (msx>=pkl->width) ? pkl->width-1 : msx;
121
+ mpy = (msy<1) ? 1 : (msy>=pkl->height) ? pkl->height-1 : msy;
122
+
123
+ for(i=0; i<pkl->height-1; i++){
124
+ if(i%mpy==0){
125
+ for(j=0; j<pkl->width; j++){
126
+ p = &pkl->image[(i*pkl->width+j)*pkl->channel];
127
+ for(k=0; k<pkl->channel; k++){
128
+ *p = PKL_COLOR_CLIP(*p+color);
129
+ p++;
130
+ }
131
+ }
132
+ }
133
+ for(j=mpx; j<pkl->width; j+=mpx){
134
+ p = &pkl->image[(i*pkl->width+j)*pkl->channel];
135
+ for(k=0; k<pkl->channel; k++){
136
+ *p = PKL_COLOR_CLIP(*p+color);
137
+ p++;
138
+ }
139
+ }
140
+ }
141
+
142
+ return(0);
143
+ }
144
+
145
+ //=============================================================================
146
+ // pkl_kuwahara
147
+ //=============================================================================
148
+ PKLExport int pkl_kuwahara(PKLImage pkl, int range)
149
+ {
150
+ unsigned char *wrk, *p;
151
+ int i, j, k, count, tr, t, tx, ty, clipx, clipy;
152
+ int value[4], mincolor, idx, total[PKL_CHANNEL];
153
+ struct { int sx, sy, ex, ey; } r[4]; //rect
154
+ unsigned char max_color[PKL_CHANNEL], min_color[PKL_CHANNEL], c;
155
+
156
+ if(range<=1) return(0);
157
+
158
+ wrk = malloc(pkl->width*pkl->height*pkl->channel);
159
+ if(!wrk) return(1);
160
+ memset(wrk, 0, pkl->width*pkl->height*pkl->channel);
161
+
162
+ //�̈�̃s�N�Z����
163
+ count = range*range;
164
+ tr = range-1;
165
+
166
+ for(i=0; i<pkl->height; i++){
167
+ for(j=0; j<pkl->width; j++){
168
+ /* �J�����g�s�N�Z���𒆐S��4�‚̗̈�ɕ����� */
169
+ //����(0)
170
+ r[0].sx = j-tr;
171
+ r[0].sy = i-tr;
172
+ r[0].ex = j;
173
+ r[0].ey = i;
174
+ //�E��(1)
175
+ r[1].sx = j;
176
+ r[1].sy = i-tr;
177
+ r[1].ex = j+tr;
178
+ r[1].ey = i;
179
+ //�E��(2)
180
+ r[2].sx = j;
181
+ r[2].sy = i;
182
+ r[2].ex = j+tr;
183
+ r[2].ey = i+tr;
184
+ //����(3)
185
+ r[3].sx = j-tr;
186
+ r[3].sy = i;
187
+ r[3].ex = j;
188
+ r[3].ey = i+tr;
189
+
190
+ /* �e�̈�Ŋe�F�̍ő�l�ƍŏ��l�̍���ݐς��� */
191
+ memset(value, 0, sizeof(value));
192
+ for(t=0; t<4; t++){
193
+ memset(max_color, 0x00, PKL_CHANNEL);
194
+ memset(min_color, 0xff, PKL_CHANNEL);
195
+ for(ty=r[t].sy; ty<=r[t].ey; ty++){
196
+ for(tx=r[t].sx; tx<=r[t].ex; tx++){
197
+ clipx = tx<0 ? 0 : tx>=pkl->width ? pkl->width-1 : tx;
198
+ clipy = ty<0 ? 0 : ty>=pkl->height ? pkl->height-1 : ty;
199
+ p = &pkl->image[(ty*pkl->width+tx)*pkl->channel];
200
+ for(k=0; k<pkl->channel; k++){
201
+ c = *p++;
202
+ max_color[k] = (c > max_color[k]) ? c : max_color[k];
203
+ min_color[k] = (c < min_color[k]) ? c : min_color[k];
204
+ }
205
+ }
206
+ }
207
+
208
+ for(k=0; k<pkl->channel; k++)
209
+ value[t] += (max_color[k] - min_color[k]);
210
+ }
211
+
212
+ /* �e�̈�ōł��F�̍������������̈��I������ */
213
+ mincolor = value[0];
214
+ idx = 0;
215
+ for(t=1; t<4; t++){
216
+ if(value[t]<mincolor){
217
+ mincolor = value[t];
218
+ idx = t;
219
+ }
220
+ }
221
+
222
+ /* �I�����ꂽ�̈�̐F�̕��ς��s�N�Z���ɓK�p���� */
223
+ memset(total, 0, sizeof(total));
224
+ for(ty=r[idx].sy; ty<=r[idx].ey; ty++){
225
+ for(tx=r[idx].sx; tx<=r[idx].ex; tx++){
226
+ clipx = tx<0 ? 0 : tx>=pkl->width ? pkl->width-1 : tx;
227
+ clipy = ty<0 ? 0 : ty>=pkl->height ? pkl->height-1 : ty;
228
+ p = &pkl->image[(ty*pkl->width+tx)*pkl->channel];
229
+ for(k=0; k<pkl->channel; k++)
230
+ total[k] += *p++;
231
+ }
232
+ }
233
+ p =&wrk[(i*pkl->width+j)*pkl->channel];
234
+ for(k=0; k<pkl->channel; k++)
235
+ *p++ = PKL_COLOR_CLIP(total[k]/count);
236
+ }
237
+ }
238
+
239
+ free(pkl->image);
240
+ pkl->image = wrk;
241
+ return(0);
242
+ }
243
+
244
+ //=============================================================================
245
+ // pkl_alphablend
246
+ //=============================================================================
247
+ PKLExport int pkl_alphablend(PKLImage parent, PKLImage child, int xpos, int ypos, int alpha)
248
+ {
249
+ int tw, th, i, j, k, alp, pp, cp;
250
+ const int limits=PKL_COLOR-1;
251
+
252
+ if(parent->channel != child->channel) return(1);
253
+ if(xpos<0 || xpos>=parent->width || ypos<0 || ypos>=parent->height) return(1);
254
+ alp = PKL_COLOR_CLIP(alpha);
255
+
256
+ tw = (xpos+child->width > parent->width) ? parent->width-xpos : child->width;
257
+ th = (ypos+child->height > parent->height) ? parent->height-ypos : child->height;
258
+
259
+ for(i=ypos; i<th+ypos; i++){
260
+ for(j=0; j<tw; j++){
261
+ pp = (i*parent->width+xpos+j)*parent->channel;
262
+ cp = ((i-ypos)*child->width+j)*child->channel;
263
+ for(k=0; k<parent->channel; k++)
264
+ parent->image[pp+k] =PKL_COLOR_CLIP((parent->image[pp+k]*alp + child->image[cp+k]*(limits-alp)) / limits);
265
+ }
266
+
267
+ }
268
+
269
+ return(0);
270
+ }
271
+
272
+ //=============================================================================
273
+ // pkl_alphablend2
274
+ //=============================================================================
275
+ PKLExport int pkl_alphablend2(PKLImage parent, PKLImage child, int xpos, int ypos)
276
+ {
277
+ int tw, th, i, j, k, pp, cp;
278
+ double alpha;
279
+
280
+ if(parent->channel != child->channel) return(1);
281
+ if(xpos<0 || xpos>=parent->width || ypos<0 || ypos>=parent->height) return(1);
282
+
283
+ tw = (xpos+child->width > parent->width) ? parent->width-xpos : child->width;
284
+ th = (ypos+child->height > parent->height) ? parent->height-ypos : child->height;
285
+
286
+ for(i=ypos; i<th+ypos; i++){
287
+ for(j=0; j<tw; j++){
288
+ alpha = sin((i-ypos)*M_PI/child->height) * sin(j*M_PI/child->width);
289
+ pp = (i*parent->width+xpos+j)*parent->channel;
290
+ cp = ((i-ypos)*child->width+j)*child->channel;
291
+ for(k=0; k<parent->channel; k++)
292
+ parent->image[pp+k] =PKL_COLOR_CLIP(parent->image[pp+k]*(1.0-alpha) + child->image[cp+k]*alpha);
293
+ }
294
+
295
+ }
296
+ return(0);
297
+ }
298
+
299
+ //=============================================================================
300
+ // pkl_shadowframe
301
+ //=============================================================================
302
+ PKLExport int pkl_shadowframe(PKLImage pkl, int margin, int backcolor, int shadowcolor)
303
+ {
304
+ unsigned char *img, back[PKL_CHANNEL], shadow[PKL_CHANNEL];
305
+ int i, j, w, h;
306
+
307
+ if(margin<=0) return(1);
308
+
309
+ w = pkl->width;
310
+ h = pkl->height;
311
+ img = pkl->image;
312
+ pkl->width = w + margin*3;
313
+ pkl->height = h + margin*3;
314
+
315
+ pkl->image = malloc(pkl->width*pkl->height*pkl->channel);
316
+ if(!pkl->image) return(1);
317
+ memset(pkl->image, 0, pkl->width*pkl->height*pkl->channel);
318
+
319
+ for(i=0; i<pkl->channel; i++){
320
+ back[i] = (backcolor>>((pkl->channel-i)*8) & 0xFF);
321
+ shadow[i] = (shadowcolor>>((pkl->channel-i)*8) & 0xFF);
322
+ }
323
+
324
+ //�w�i�F�ݒ�
325
+ for(i=0; i<pkl->width*pkl->height*pkl->channel; i+=pkl->channel)
326
+ memcpy(&pkl->image[i], back, pkl->channel);
327
+
328
+ //�t���[���ݒ�
329
+ for(i=margin*2; i<h+margin*2; i++)
330
+ for(j=margin*2; j<w+margin*2; j++)
331
+ memcpy(&pkl->image[(i*pkl->width+j)*pkl->channel], shadow, pkl->channel);
332
+
333
+ //shadowframe�����(�P�Ƀu���[)
334
+ pkl_gaussblur(pkl, 5.0);
335
+
336
+ for(i=margin,j=0; i<h+margin; i++,j++)
337
+ memcpy(&pkl->image[(i*pkl->width+margin)*pkl->channel], &img[(j*w)*pkl->channel], w*pkl->channel);
338
+
339
+ free(img);
340
+ return(0);
341
+ }
342
+
343
+ //=============================================================================
344
+ // pkl_edge
345
+ //=============================================================================
346
+ PKLExport int pkl_edge(PKLImage pkl, int threshold)
347
+ {
348
+ unsigned char *wrk, *p;
349
+ int i, j, k, color[PKL_CHANNEL], total;
350
+ int r1, r2, r3;
351
+
352
+ wrk = malloc(pkl->width*pkl->height*pkl->channel);
353
+ if(!wrk) return(1);
354
+ memset(wrk, 0xff, pkl->width*pkl->height*pkl->channel);
355
+
356
+ r3 = sqrt((double)pkl->channel);
357
+
358
+ for(i=0; i<pkl->height; i++){
359
+ r1=r2=0;
360
+ for(j=0; j<pkl->width; j++){
361
+ //x����
362
+ if(j+1 < pkl->width){
363
+ p = &pkl->image[(i*pkl->width+j)*pkl->channel];
364
+ for(k=0; k<pkl->channel; k++){
365
+ color[k] = *p - *(p+pkl->channel);
366
+ color[k] *= color[k];
367
+ p++;
368
+ }
369
+ total=0;
370
+ for(k=0; k<pkl->channel; k++)
371
+ total += color[k];
372
+ r1 = sqrt(total)/r3;
373
+ }
374
+
375
+ //y����
376
+ if(i+1 < pkl->height){
377
+ p = &pkl->image[(i*pkl->width+j)*pkl->channel];
378
+ for(k=0; k<pkl->channel; k++){
379
+ color[k] = *p - *(p+pkl->width*pkl->channel);
380
+ color[k] *= color[k];
381
+ p++;
382
+ }
383
+ total=0;
384
+ for(k=0; k<pkl->channel; k++)
385
+ total += color[k];
386
+ r2 = sqrt(total)/r3;
387
+ }
388
+
389
+ p = &wrk[(i*pkl->width+j)*pkl->channel];
390
+ if(r1>=threshold || r2>=threshold)
391
+ for(k=0; k<pkl->channel; k++) *p++=0;
392
+ }
393
+ }
394
+ free(pkl->image);
395
+ pkl->image = wrk;
396
+ return(0);
397
+ }
398
+
399
+ //=============================================================================
400
+ // pkl_dither
401
+ //=============================================================================
402
+ PKLExport int pkl_dither(PKLImage pkl, PKL_DITHER dither)
403
+ {
404
+ double FloydSteinberg[2][3] = {
405
+ { -1, -1, 7.0/16.0},
406
+ { 3.0/16.0, 5.0/16.0, 1.0/16.0} };
407
+
408
+ double Stucci[3][5] = {
409
+ { -1, -1, -1, 8.0/42.0, 4.0/42.0},
410
+ {2.0/42.0, 4.0/42.0, 8.0/42.0, 4.0/42.0, 2.0/42.0},
411
+ {1.0/42.0, 2.0/42.0, 4.0/42.0, 2.0/42.0, 1.0/42.0} };
412
+
413
+ double Sierra[3][5] = {
414
+ { -1, -1, -1, 5.0/32.0, 3.0/32.0},
415
+ {2.0/32.0, 4.0/32.0, 5.0/32.0, 4.0/32.0, 2.0/32.0},
416
+ { -1, 2.0/32.0, 3.0/32.0, 2.0/32.0, -1} };
417
+
418
+ double JaJuNi[3][5] = {
419
+ { -1, -1, -1, 7.0/48.0, 5.0/48.0},
420
+ {3.0/48.0, 5.0/48.0, 7.0/48.0, 5.0/48.0, 3.0/48.0},
421
+ {1.0/48.0, 3.0/48.0, 5.0/48.0, 3.0/48.0, 1.0/48.0} };
422
+
423
+ unsigned char *wrk, pix;
424
+ double *pt, err;
425
+ int i, j, k, gray, s, t, tx, ty, row, col, xrange;
426
+
427
+ wrk = malloc(pkl->width*pkl->height*pkl->channel);
428
+ if(!wrk) return(1);
429
+
430
+ //�f�B�U�p�^����I��
431
+ switch(dither){
432
+ case PKL_DITHER_STUCCI:
433
+ row = 5;
434
+ col = 3;
435
+ pt = &(Stucci[0][0]);
436
+ break;
437
+
438
+ case PKL_DITHER_SIERRA:
439
+ row = 5;
440
+ col = 3;
441
+ pt = &(Sierra[0][0]);
442
+ break;
443
+
444
+ case PKL_DITHER_JAJUNI:
445
+ row = 5;
446
+ col = 3;
447
+ pt = &(JaJuNi[0][0]);
448
+ break;
449
+
450
+ case PKL_DITHER_FLOYDSTEINBERG:
451
+ default:
452
+ row = 3;
453
+ col = 2;
454
+ pt = &(FloydSteinberg[0][0]);
455
+ }
456
+
457
+ for(i=0; i<pkl->height; i++){
458
+ for(j=0; j<pkl->width; j++){
459
+ gray=0;
460
+ for(k=0; k<pkl->channel; k++)
461
+ gray += pkl->image[(i*pkl->width+j)*pkl->channel+k];
462
+ pkl->image[(i*pkl->width+j)*pkl->channel] = PKL_COLOR_CLIP(gray/pkl->channel);
463
+ }
464
+ }
465
+
466
+ xrange = (row-1)/2;
467
+
468
+ for(i=0; i<pkl->height; i++){
469
+ for(j=0; j<pkl->width; j++){
470
+ pix = pkl->image[(i*pkl->width+j)*pkl->channel];
471
+ if(pix>127)
472
+ memset(&wrk[(i*pkl->width+j)*pkl->channel], 0xff, pkl->channel);
473
+ else
474
+ memset(&wrk[(i*pkl->width+j)*pkl->channel], 0x00, pkl->channel);
475
+ err = pix>127 ? pix-255 : pix;
476
+
477
+ for(s=0; s<col; s++){
478
+ for(t=-xrange; t<=xrange; t++){
479
+ tx = j+t;
480
+ ty = i+s;
481
+ if(tx<0 || tx>=pkl->width || ty>=pkl->height) continue;
482
+ if (pt[s*row+t+xrange] < 0) continue;
483
+
484
+ pkl->image[(ty*pkl->width+tx)*pkl->channel] =
485
+ PKL_COLOR_CLIP(pkl->image[(ty*pkl->width+tx)*pkl->channel] + err * pt[s*row+t+xrange]);
486
+ }
487
+ }
488
+ }
489
+ }
490
+
491
+ free(pkl->image);
492
+ pkl->image = wrk;
493
+ return(0);
494
+ }
495
+