pikl 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ #ifndef _EXTRA_WU_
2
+ #define _EXTRA_WU_
3
+
4
+ int wuReduce(unsigned char *RGBpic, int numcolors, long picsize,
5
+ unsigned char *Picture256, unsigned char *QuantizedPalette);
6
+
7
+ #endif
@@ -0,0 +1,250 @@
1
+ #include "pikl_affine.h"
2
+
3
+ static int pkl_affine_nn(PKLImage pkl, point2 *p, unsigned char *dst);
4
+ static int pkl_affine_bl(PKLImage pkl, point2 *p, unsigned char *dst);
5
+ static point2 *transform(matrix *mat, int x, int y, point2 *p);
6
+ static matrix *pkl_matrix(PKLImage pkl, matrix *mat);
7
+
8
+ static void matrix_init(matrix *mat);
9
+ static void matrix_scale(matrix *mat, double x_scale, double y_scale);
10
+ static void matrix_translate(matrix *mat, double x_trns, double y_trns);
11
+ static void matrix_rotate(matrix *mat, double angle);
12
+ static void matrix_build(matrix *mat1, matrix *mat2);
13
+
14
+ //=============================================================================
15
+ // pkl_matrix_scale
16
+ //=============================================================================
17
+ PKLExport void pkl_matrix_scale(PKLImage pkl, double x_scale, double y_scale)
18
+ {
19
+ pkl->x_scale = x_scale;
20
+ pkl->y_scale = y_scale;
21
+ }
22
+
23
+ //=============================================================================
24
+ // pkl_matrix_rotate
25
+ //=============================================================================
26
+ PKLExport void pkl_matrix_rotate(PKLImage pkl, double angle)
27
+ {
28
+ pkl->angle = angle;
29
+ }
30
+
31
+ //=============================================================================
32
+ // pkl_matrix_translate
33
+ //=============================================================================
34
+ PKLExport void pkl_matrix_translate(PKLImage pkl, int x, int y)
35
+ {
36
+ pkl->x_trns = x;
37
+ pkl->y_trns = y;
38
+ }
39
+
40
+ //=============================================================================
41
+ // pkl_affine
42
+ //=============================================================================
43
+ PKLExport int pkl_affine(PKLImage pkl, PKL_SAMPLE sampler, int width, int height, int backcolor)
44
+ {
45
+ int (*sample)(PKLImage pkl, point2 *p, unsigned char *dst);
46
+ unsigned char *wrk, back[4];
47
+ int i, j;
48
+ point2 p;
49
+ matrix mat;
50
+
51
+ //affine-matrix�̍\�z
52
+ pkl_matrix(pkl, &mat);
53
+
54
+ //�T���v�����O��ݒ�
55
+ switch(sampler){
56
+ case PKL_SAMPLE_NN:
57
+ sample = pkl_affine_nn;
58
+ break;
59
+ default:
60
+ sample = pkl_affine_bl;
61
+ }
62
+
63
+ wrk = malloc(width * height * pkl->channel);
64
+ if(!wrk) return(1);
65
+ memset(wrk, 0, width*height*pkl->channel);
66
+
67
+ //�w�i�F�����
68
+ for(i=0; i<pkl->channel; i++)
69
+ back[i] = (backcolor>>((pkl->channel-i)*8) & 0xFF);
70
+
71
+ //�ό`��T�C�Y��loop
72
+ for(i=0; i<height; i++){
73
+ for(j=0; j<width; j++){
74
+ if(sample(pkl, transform(&mat, j, i, &p), &wrk[(i*width+j)*pkl->channel]))
75
+ memcpy(&wrk[(i*width+j)*pkl->channel], back, pkl->channel);
76
+ }
77
+ }
78
+
79
+ free(pkl->image);
80
+ pkl->image = wrk;
81
+ pkl->width = width;
82
+ pkl->height = height;
83
+ return(0);
84
+ }
85
+
86
+ //=============================================================================
87
+ // pkl_affine_nn
88
+ //=============================================================================
89
+ static int pkl_affine_nn(PKLImage pkl, point2 *p, unsigned char *dst)
90
+ {
91
+ int x, y;
92
+
93
+ x = p->x < 0.0f ? (int)(p->x-0.5f) : (int)(p->x+0.5f);
94
+ y = p->y < 0.0f ? (int)(p->y-0.5f) : (int)(p->y+0.5f);
95
+
96
+ if(x>=0 && x<pkl->width && y>=0 && y<pkl->height){
97
+ memcpy(dst, &pkl->image[(y*pkl->width+x)*pkl->channel], pkl->channel);
98
+ return(0);
99
+ }
100
+ return(1);
101
+ }
102
+
103
+
104
+ //=============================================================================
105
+ // pkl_affine_bl
106
+ //=============================================================================
107
+ static int pkl_affine_bl(PKLImage pkl, point2 *p, unsigned char *dst)
108
+ {
109
+ int p0x, p0y, i;
110
+ double fracx, fracy;
111
+ double pixel[PKL_CHANNEL];
112
+
113
+ memset(pixel, 0, sizeof(double)*PKL_CHANNEL);
114
+ p0x = (int)floor(p->x);
115
+ p0y = (int)floor(p->y);
116
+ fracx = p->x - p0x;
117
+ fracy = p->y - p0y;
118
+ if(p0x<0 || p0x>=pkl->width || p0y<0 || p0y>=pkl->height) return(1);
119
+
120
+ if(p0x+1 < pkl->width){
121
+ if(p0y+1 < pkl->height){
122
+ for(i=0; i<pkl->channel; i++){
123
+ pixel[i] += pkl->image[(p0y*pkl->width+p0x)*pkl->channel+i] * (1-fracx)*(1-fracy);
124
+ pixel[i] += pkl->image[(p0y*pkl->width+p0x+1)*pkl->channel+i] * fracx*(1-fracy);
125
+ pixel[i] += pkl->image[((p0y+1)*pkl->width+p0x)*pkl->channel+i] * (1-fracx)*fracy;
126
+ pixel[i] += pkl->image[((p0y+1)*pkl->width+p0x+1)*pkl->channel+i] * fracx*fracy;
127
+ }
128
+ }else{
129
+ for(i=0; i<pkl->channel; i++){
130
+ pixel[i] += pkl->image[(p0y*pkl->width+p0x)*pkl->channel+i] * (1-fracx);
131
+ pixel[i] += pkl->image[(p0y*pkl->width+p0x+1)*pkl->channel+i] * fracx;
132
+ }
133
+ }
134
+ }else{
135
+ if(p0y+1 < pkl->height){
136
+ for(i=0; i<pkl->channel; i++){
137
+ pixel[i] += pkl->image[(p0y*pkl->width+p0x)*pkl->channel+i] * (1-fracy);
138
+ pixel[i] += pkl->image[((p0y+1)*pkl->width+p0x)*pkl->channel+i] * fracy;
139
+ }
140
+ }else{
141
+ for(i=0; i<pkl->channel; i++)
142
+ pixel[i] += pkl->image[((p0y+1)*pkl->width+p0x)*pkl->channel+i];
143
+ }
144
+ }
145
+
146
+ for(i=0; i<pkl->channel; i++)
147
+ dst[i] = PKL_COLOR_CLIP(pixel[i]);
148
+ return(0);
149
+ }
150
+
151
+ //=============================================================================
152
+ // pkl_matrix
153
+ //=============================================================================
154
+ static matrix *pkl_matrix(PKLImage pkl, matrix *mat)
155
+ {
156
+ matrix mat2;
157
+
158
+ matrix_init(mat);
159
+
160
+ //scale
161
+ matrix_scale(&mat2, pkl->x_scale, pkl->y_scale);
162
+ matrix_build(mat, &mat2);
163
+
164
+ //rotate
165
+ matrix_rotate(&mat2, pkl->angle);
166
+ matrix_build(mat, &mat2);
167
+
168
+ //translate
169
+ matrix_translate(&mat2, pkl->x_trns, pkl->y_trns);
170
+ matrix_build(mat, &mat2);
171
+
172
+ return(mat);
173
+ }
174
+
175
+ //=============================================================================
176
+ // matrix_init
177
+ //=============================================================================
178
+ static void matrix_init(matrix *mat)
179
+ {
180
+ mat->a = 1.0;
181
+ mat->b = 0.0;
182
+ mat->c = 0.0;
183
+ mat->d = 1.0;
184
+ mat->e = 0.0;
185
+ mat->f = 0.0;
186
+ }
187
+
188
+ //=============================================================================
189
+ // matrix_init
190
+ //=============================================================================
191
+ static void matrix_scale(matrix *mat, double x_scale, double y_scale)
192
+ {
193
+ matrix_init(mat);
194
+ mat->a = x_scale;
195
+ mat->d = y_scale;
196
+ }
197
+
198
+ //=============================================================================
199
+ // matrix_translate
200
+ //=============================================================================
201
+ static void matrix_translate(matrix *mat, double x_trns, double y_trns)
202
+ {
203
+ matrix_init(mat);
204
+ mat->e = x_trns;
205
+ mat->f = y_trns;
206
+ }
207
+
208
+ //=============================================================================
209
+ // matrix_rotate
210
+ //=============================================================================
211
+ static void matrix_rotate(matrix *mat, double angle)
212
+ {
213
+ double c, s;
214
+
215
+ matrix_init(mat);
216
+
217
+ c = cos(angle*3.14/180.0);
218
+ s = sin(angle*3.14/180.0);
219
+ mat->a = c;
220
+ mat->b = s;
221
+ mat->c = -s;
222
+ mat->d = c;
223
+ }
224
+
225
+ //=============================================================================
226
+ // matrix_init
227
+ //=============================================================================
228
+ static void matrix_build(matrix *mat1, matrix *mat2)
229
+ {
230
+ matrix mat;
231
+ memcpy(&mat, mat1, sizeof(matrix));
232
+
233
+ mat1->a = mat.a * mat2->a + mat.b * mat2->c;
234
+ mat1->b = mat.a * mat2->b + mat.b * mat2->d;
235
+ mat1->c = mat.c * mat2->a + mat.d * mat2->c;
236
+ mat1->d = mat.c * mat2->b + mat.d * mat2->d;
237
+ mat1->e = mat.e * mat2->a + mat.f * mat2->c + mat2->e;
238
+ mat1->f = mat.e * mat2->b + mat.f * mat2->d + mat2->f;
239
+ }
240
+
241
+ //=============================================================================
242
+ // transform
243
+ //=============================================================================
244
+ static point2 *transform(matrix *mat, int x, int y, point2 *p)
245
+ {
246
+ p->x = mat->a*x + mat->c*y + mat->e;
247
+ p->y = mat->b*x + mat->d*y + mat->f;
248
+ return p;
249
+ }
250
+
@@ -0,0 +1,20 @@
1
+ #ifndef _LIB_PIKL_AFFINE_
2
+ #define _LIB_PIKL_AFFINE_
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
+ typedef struct {
13
+ double x, y;
14
+ } point2;
15
+
16
+ typedef struct {
17
+ double a,b,c,d,e,f;
18
+ } matrix;
19
+
20
+ #endif
@@ -0,0 +1,244 @@
1
+ #include "pikl_blur.h"
2
+
3
+ static int pkl_raw_blur(PKLImage pkl, int x, int y, double ef, int weight, double angle, double adr);
4
+
5
+ //=============================================================================
6
+ // pkl_blur
7
+ //=============================================================================
8
+ PKLExport int pkl_blur(PKLImage pkl, int weight)
9
+ {
10
+ int i, j, p, q, k;
11
+ int cnt, stock[PKL_CHANNEL];
12
+ unsigned char *wrk;
13
+
14
+ wrk = malloc(pkl->height * pkl->width * pkl->channel);
15
+ if(!wrk) return(1);
16
+ memset(wrk, 0, pkl->height * pkl->width * pkl->channel);
17
+
18
+ for(i=0; i<pkl->height; i++){
19
+ for(j=0; j<pkl->width; j++){
20
+ cnt = 0;
21
+ memset(stock, 0, sizeof(stock));
22
+
23
+ for(p=-weight; p<=weight; p++){
24
+ for(q=-weight; q<=weight; q++){
25
+ if(j+q>=0 && j+q<pkl->width && i+p>=0 && i+p<pkl->height){
26
+ for(k=0; k<pkl->channel; k++)
27
+ stock[k] += pkl->image[((i+p)*pkl->width+(j+q))*pkl->channel+k];
28
+ cnt++;
29
+ }
30
+ }
31
+ }
32
+ if(cnt==0) continue;
33
+ for(k=0; k<pkl->channel; k++)
34
+ wrk[(i * pkl->width + j) * pkl->channel+k] = PKL_COLOR_CLIP(stock[k]/cnt);
35
+ }
36
+ }
37
+
38
+ free(pkl->image);
39
+ pkl->image = wrk;
40
+ return(0);
41
+ }
42
+
43
+ //=============================================================================
44
+ // pkl_gaussblur
45
+ //=============================================================================
46
+ PKLExport int pkl_gaussblur(PKLImage pkl, double weight)
47
+ {
48
+ int range, i, j, s, t, p;
49
+ double sg, *gauss, value[PKL_CHANNEL];
50
+ unsigned char *wrk;
51
+
52
+ if(weight<=0.0) return(1);
53
+
54
+ wrk = malloc(pkl->height * pkl->width * pkl->channel);
55
+ if(!wrk) return(1);
56
+ memset(wrk, 0, pkl->height * pkl->width * pkl->channel);
57
+
58
+ range = weight * 3;
59
+ gauss = malloc(sizeof(double)*(range+1));
60
+ memset(gauss, 0, sizeof(gauss));
61
+
62
+ //gaussian table
63
+ for(i=0; i<=range; i++)
64
+ gauss[i] = exp( -1.0*i*i / (2*weight*weight));
65
+
66
+ //horizon(pkl-->wrk)
67
+ for(i=0; i<pkl->height; i++){
68
+ for(j=0; j<pkl->width; j++){
69
+ memset(value, 0, sizeof(double)*PKL_CHANNEL);
70
+ sg=0;
71
+ for(s=-range; s<=range; s++){
72
+ t = j+s<0 ? 0 : j+s>=pkl->width ? pkl->width-1 : j+s;
73
+ sg += gauss[abs(s)];
74
+ for(p=0; p<pkl->channel; p++){
75
+ value[p] += pkl->image[(i*pkl->width+t)*pkl->channel+p] * gauss[abs(s)];
76
+ }
77
+ }
78
+ for(p=0; p<pkl->channel; p++)
79
+ wrk[(i*pkl->width+j)*pkl->channel+p] = PKL_COLOR_CLIP(value[p]/sg);
80
+ }
81
+ }
82
+
83
+ //vertical(wrk-->pkl)
84
+ for(i=0; i<pkl->height; i++){
85
+ for(j=0; j<pkl->width; j++){
86
+ memset(value, 0, sizeof(double)*PKL_CHANNEL);
87
+ sg=0;
88
+ for(s=-range; s<=range; s++){
89
+ t = i+s<0 ? 0 : i+s>=pkl->height ? pkl->height-1 : i+s;
90
+ sg += gauss[abs(s)];
91
+ for(p=0; p<pkl->channel; p++){
92
+ value[p] += wrk[(t*pkl->width+j)*pkl->channel+p] * gauss[abs(s)];
93
+ }
94
+ }
95
+ for(p=0; p<pkl->channel; p++)
96
+ pkl->image[(i*pkl->width+j)*pkl->channel+p] = PKL_COLOR_CLIP(value[p]/sg);
97
+ }
98
+ }
99
+
100
+ free(gauss);
101
+ free(wrk);
102
+ return(0);
103
+ }
104
+
105
+
106
+ //=============================================================================
107
+ // pkl_rblur(����)
108
+ //=============================================================================
109
+ PKLExport int pkl_rblur(PKLImage pkl, int x, int y, double ef, int weight)
110
+ {
111
+ return pkl_raw_blur(pkl, x, y, ef, weight, 0.0, 0.0);
112
+ }
113
+
114
+ //=============================================================================
115
+ // pkl_ablur(��])
116
+ //=============================================================================
117
+ PKLExport int pkl_ablur(PKLImage pkl, int x, int y, double ef, int weight)
118
+ {
119
+ return pkl_raw_blur(pkl, x, y, ef, weight, 0.0, M_PI/2.0);
120
+ }
121
+
122
+ //=============================================================================
123
+ // pkl_wblur
124
+ //=============================================================================
125
+ PKLExport int pkl_wblur(PKLImage pkl, int x, int y, double ef, int weight, double angle)
126
+ {
127
+ return pkl_raw_blur(pkl, x, y, ef, weight, angle, M_PI*angle/180.0);
128
+ }
129
+
130
+ //=============================================================================
131
+ // pkl_rab_blur(����,��],�����܂��̃R�A)
132
+ //=============================================================================
133
+ static int pkl_raw_blur(PKLImage pkl, int x, int y, double ef, int weight, double angle, double adr)
134
+ {
135
+ unsigned char *wrk;
136
+ int i, j, k, t, tx, ty, pat;
137
+ long color[PKL_CHANNEL], range;
138
+ double rad, dis, disI, rate, disMAX;
139
+ double ox, oy, dx, dy, nf;
140
+
141
+ if(ef==0.0 || weight==0) return(1);
142
+
143
+ wrk = malloc(pkl->width*pkl->height*pkl->channel);
144
+ if(!wrk) return(1);
145
+ memset(wrk, 0, pkl->width*pkl->height*pkl->channel);
146
+
147
+ nf = weight/2;
148
+ //ox = (double)(pkl->width)/2.0;
149
+ //oy = (double)(pkl->height)/2.0;
150
+ ox = (x<0 || x>=pkl->width) ? (double)(pkl->width)/2.0 : (double)x; //���S�_�����߂�
151
+ oy = (y<0 || y>=pkl->height) ? (double)(pkl->height)/2.0 : (double)y; //���S�_�����߂�
152
+ dx = (double)pkl->width-ox;
153
+ dy = (double)pkl->height-oy;
154
+ disMAX = sqrt(dx*dx+dy*dy);
155
+
156
+ for(i=0; i<pkl->height; i++){
157
+ for(j=0; j<pkl->width; j++){
158
+ range=0;
159
+ memset(color, 0, sizeof(color));
160
+
161
+ dx = (double)j-ox;
162
+ dy = (double)i-oy;
163
+ //rad = ((dx!=0.0) ? atan(dy/dx) : M_PI/2.0) + (M_PI*angle/180.0); //�����܂���
164
+ //rad = ((dx!=0.0) ? atan(dy/dx) : M_PI/2.0); //���ˏ�
165
+ //rad = ((dx!=0.0) ? atan(dy/dx) : M_PI/2.0) + (M_PI/2.0); //��]
166
+ rad = ((dx!=0.0) ? atan(dy/dx) : M_PI/2.0) + adr;
167
+ dis = sqrt(dx*dx+dy*dy);
168
+ rate = (ef*dis/disMAX) / (double)nf;
169
+
170
+ for(t=0; t<weight; t++){
171
+ pat = (t==nf) ? 3 : 1;
172
+ disI = (double)(t-nf)*rate;
173
+ tx = (int)(disI * cos(rad)) + j;
174
+ ty = (int)(disI * sin(rad)) + i;
175
+ tx = tx<0 ? 0 : tx>=pkl->width ? pkl->width-1 : tx;
176
+ ty = ty<0 ? 0 : ty>=pkl->height ? pkl->height-1 : ty;
177
+ for(k=0; k<pkl->channel; k++)
178
+ color[k] += pkl->image[(ty*pkl->width+tx)*pkl->channel+k] * pat;
179
+ range += pat;
180
+ }
181
+
182
+ for(k=0; k<pkl->channel; k++)
183
+ wrk[(i*pkl->width+j)*pkl->channel+k] = PKL_COLOR_CLIP(color[k]/range);
184
+ }
185
+ }
186
+
187
+ free(pkl->image);
188
+ pkl->image = wrk;
189
+ return(0);
190
+ }
191
+
192
+ //=============================================================================
193
+ // pkl_mblur
194
+ //=============================================================================
195
+ PKLExport int pkl_mblur(PKLImage pkl, double angle, int weight)
196
+ {
197
+ unsigned char *wrk;
198
+ int i, j, k, t, tx, ty;
199
+ int *dx, *dy, color[PKL_CHANNEL];
200
+ double s, c;
201
+
202
+ if(weight < 2) return(0);
203
+
204
+ wrk = malloc(pkl->width*pkl->height*pkl->channel);
205
+ if(!wrk) return(1);
206
+ memset(wrk, 0, pkl->width*pkl->height*pkl->channel);
207
+
208
+ //�C�ӊp�x�̃Y���ʃe�[�u�����쐬
209
+ s = sin((angle+180.0) * M_PI / 180.0);
210
+ c = cos((angle+180.0) * M_PI / 180.0);
211
+
212
+ dx = malloc(sizeof(int)*(weight+1));
213
+ dy = malloc(sizeof(int)*(weight+1));
214
+ memset(dx, 0, sizeof(int)*(weight+1));
215
+ memset(dy, 0, sizeof(int)*(weight+1));
216
+
217
+ for(i=1; i<=weight; i++){
218
+ dx[i] = (int)(c * (i+1) + 0.5);
219
+ dy[i] = (int)(s * (i+1) + 0.5);
220
+ }
221
+
222
+ for(i=0; i<pkl->height; i++){
223
+ for(j=0; j<pkl->width; j++){
224
+ memset(color, 0, sizeof(color));
225
+ for(t=0; t<=weight; t++){
226
+ tx = j+dx[t] < 0 ? 0 : j+dx[t]>=pkl->width ? pkl->width-1 : j+dx[t];
227
+ ty = i+dy[t] < 0 ? 0 : i+dy[t]>=pkl->height ? pkl->height-1 : i+dy[t];
228
+
229
+ for(k=0; k<pkl->channel; k++)
230
+ color[k] += pkl->image[(ty*pkl->width+tx)*pkl->channel+k];
231
+ }
232
+
233
+ for(k=0; k<pkl->channel; k++)
234
+ wrk[(i*pkl->width+j)*pkl->channel+k] = PKL_COLOR_CLIP(color[k]/weight);
235
+ }
236
+ }
237
+
238
+ free(dx);
239
+ free(dy);
240
+ free(pkl->image);
241
+ pkl->image = wrk;
242
+ return(0);
243
+ }
244
+