pikl 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,152 @@
1
+ #include "pikl_bitmap.h"
2
+
3
+ static void bmp_header(unsigned char *data, int offset, int value, int size);
4
+ static int convert_numeric(unsigned char *src, int size);
5
+ static int color_b2p(int biBitCount);
6
+ static int bmp_channel(int biBitCount);
7
+ static int bmp_width(int width, int channel);
8
+
9
+ //=============================================================================
10
+ // load_bitmap
11
+ //=============================================================================
12
+ int load_bitmap(PKLImage pkl, FILE *image)
13
+ {
14
+ unsigned char header[BF_SIZE + BI_SIZE];
15
+ unsigned char *p, stuck;
16
+ int i, j, offset, scansize;
17
+
18
+ if( fread(header, 1, sizeof(header), image) != sizeof(header) ) return(1);
19
+
20
+ pkl->width = convert_numeric(&header[18], 4);
21
+ pkl->height = convert_numeric(&header[22], 4);
22
+ pkl->color = color_b2p(convert_numeric(&header[28], 2));
23
+ pkl->channel = bmp_channel(convert_numeric(&header[28], 2));
24
+ if(pkl->color==PKL_UNKNOWN) return(1);
25
+
26
+ pkl->image = malloc(pkl->width * pkl->height * pkl->channel);
27
+ if(!pkl->image) return(1);
28
+
29
+ offset = convert_numeric(&header[10], 4);
30
+ scansize = bmp_width(pkl->width, pkl->channel);
31
+
32
+ p = pkl->image + ((pkl->height-1) * pkl->width * pkl->channel);
33
+ fseek(image, offset, SEEK_SET);
34
+
35
+ for(i=0; i<pkl->height; i++){
36
+ fread(p, 1, pkl->width*pkl->channel, image);
37
+ for(j=0; j<pkl->width; j++){
38
+ stuck = p[j*pkl->channel];
39
+ p[j*pkl->channel] = p[j*pkl->channel+2];
40
+ p[j*pkl->channel+2] = stuck;
41
+ }
42
+ p -= pkl->width*pkl->channel;
43
+ offset += scansize;
44
+ fseek(image, offset, SEEK_SET);
45
+ }
46
+
47
+ return(0);
48
+ }
49
+
50
+ //=============================================================================
51
+ // color_b2p
52
+ // biBitCount = 1,4,8,16,24,32
53
+ //=============================================================================
54
+ static int color_b2p(int biBitCount)
55
+ {
56
+ switch(biBitCount){
57
+ case 24:
58
+ return PKL_RGB;
59
+ default:
60
+ return PKL_UNKNOWN;
61
+ }
62
+ return PKL_UNKNOWN;
63
+ }
64
+
65
+ //=============================================================================
66
+ // bmp_channel
67
+ // biBitCount = 1,4,8,16,24,32
68
+ //=============================================================================
69
+ static int bmp_channel(int biBitCount)
70
+ {
71
+ switch(biBitCount){
72
+ case 24:
73
+ return 3;
74
+ default:
75
+ return 0;
76
+ }
77
+ return 0;
78
+ }
79
+
80
+ //=============================================================================
81
+ // convert_nv
82
+ //=============================================================================
83
+ static int convert_numeric(unsigned char *src, int size)
84
+ {
85
+ int i, dst=0;
86
+ for(i=0; i<size; i++){
87
+ dst += (src[i]<<(8*i));
88
+ }
89
+ return(dst);
90
+ }
91
+
92
+ //=============================================================================
93
+ // bmp_width
94
+ //=============================================================================
95
+ static int bmp_width(int width, int channel)
96
+ {
97
+ int length;
98
+ if( width*channel % 4 ){
99
+ length = width*channel + (4 - width*channel % 4);
100
+ }else{
101
+ length = width*channel;
102
+ }
103
+ return(length);
104
+ }
105
+
106
+ //=============================================================================
107
+ // save_bitmap
108
+ //=============================================================================
109
+ int save_bitmap(PKLImage pkl, FILE *image)
110
+ {
111
+ unsigned char data[BF_SIZE+BI_SIZE], *wrk, stuck;
112
+ int linesize, i, j;
113
+
114
+ linesize = bmp_width(pkl->width, pkl->channel);
115
+
116
+ data[0] = 'B';
117
+ data[1] = 'M';
118
+ bmp_header(data, 2, BF_SIZE + BI_SIZE + linesize*pkl->height, 4);
119
+ bmp_header(data, 6, 0, 4);
120
+ bmp_header(data, 10, BF_SIZE + BI_SIZE, 4);
121
+ bmp_header(data, 14, BI_SIZE, 4);
122
+ bmp_header(data, 18, pkl->width, 4);
123
+ bmp_header(data, 22, pkl->height, 4);
124
+ bmp_header(data, 26, 1, 2);
125
+ bmp_header(data, 28, 24, 2);
126
+ bmp_header(data, 30, 0, 24);
127
+ fwrite(data, 1, BF_SIZE+BI_SIZE, image);
128
+
129
+ wrk = malloc(linesize);
130
+ for(i=pkl->height-1; i>=0; i--){
131
+ memcpy(wrk, &pkl->image[i*pkl->width*pkl->channel], pkl->width*pkl->channel);
132
+ for(j=0; j<pkl->width; j++){
133
+ stuck = wrk[j*pkl->channel];
134
+ wrk[j*pkl->channel] = wrk[j*pkl->channel+2];
135
+ wrk[j*pkl->channel+2] = stuck;
136
+ }
137
+ fwrite(wrk, 1, linesize, image);
138
+ }
139
+ free(wrk);
140
+ return(0);
141
+ }
142
+
143
+ //=============================================================================
144
+ // bmp_header
145
+ //=============================================================================
146
+ static void bmp_header(unsigned char *data, int offset, int value, int size)
147
+ {
148
+ int i;
149
+ for(i=0; i<size; i++){
150
+ data[offset+i] = (unsigned char)(value >> (8*i));
151
+ }
152
+ }
@@ -0,0 +1,18 @@
1
+ #ifndef _LIB_PIKL_BITMAP_
2
+ #define _LIB_PIKL_BITMAP_
3
+
4
+ #include <stdio.h>
5
+ #include <stdlib.h>
6
+ #include <string.h>
7
+
8
+ #include "pikl.h"
9
+ #include "pikl_private.h"
10
+
11
+ #define BF_SIZE 14 //BITMAPFILEHEADER size
12
+ #define BI_SIZE 40 //BITMAPINFOHEADER size
13
+
14
+ int load_bitmap(PKLImage pkl, FILE *image);
15
+ int save_bitmap(PKLImage pkl, FILE *image);
16
+
17
+ #endif
18
+
@@ -0,0 +1,191 @@
1
+ #include "pikl_effect.h"
2
+
3
+ //=================================================================================
4
+ // pkl_unsharp
5
+ //=================================================================================
6
+ PKLExport int pkl_unsharp(PKLImage pkl, int threshold, double edge)
7
+ {
8
+ int i, j, p, q, k;
9
+ int cnt, value, stock[PKL_CHANNEL];
10
+ unsigned char *wrk;
11
+
12
+ wrk = malloc(pkl->height * pkl->width * pkl->channel);
13
+ if(!wrk) return(1);
14
+ memset(wrk, 0, pkl->height * pkl->width * pkl->channel);
15
+
16
+ for(i=0; i<pkl->height; i++){
17
+ for(j=0; j<pkl->width; j++){
18
+ cnt = 0;
19
+ memset(stock, 0, sizeof(stock));
20
+
21
+ for(p=-RADIUS; p<=RADIUS; p++){
22
+ for(q=-RADIUS; q<=RADIUS; q++){
23
+ if(j+q>=0 && j+q<pkl->width && i+p>=0 && i+p<pkl->height){
24
+ for(k=0; k<pkl->channel; k++) stock[k] += pkl->image[((i+p)*pkl->width+(j+q))*pkl->channel+k];
25
+ cnt++;
26
+ }
27
+ }
28
+ }
29
+ for(k=0; k<pkl->channel; k++){
30
+ wrk[(i * pkl->width + j) * pkl->channel+k] = stock[k] / cnt;
31
+ }
32
+ }
33
+ }
34
+
35
+ for(i=0; i<pkl->height*pkl->width*pkl->channel; i++){
36
+ value = abs(pkl->image[i]-wrk[i])<threshold ? pkl->image[i] : pkl->image[i]+(pkl->image[i]-wrk[i])*edge;
37
+ pkl->image[i] = PKL_RGB(value);
38
+ }
39
+ free(wrk);
40
+ return(0);
41
+ }
42
+
43
+ //=================================================================================
44
+ // pkl_contrast
45
+ //=================================================================================
46
+ PKLExport int pkl_contrast(PKLImage pkl, int rate)
47
+ {
48
+ unsigned char ct[PKL_COLOR];
49
+ int value, i;
50
+
51
+ if(rate < -127 || rate > 127) return(1);
52
+
53
+ if(rate < 0.0){
54
+ //�d�݂������̎��͒����I�ɕ��R��
55
+ for(i=0; i<PKL_COLOR; i++){
56
+ value = i + (i-127) * rate/127;
57
+ ct[i] = PKL_RGB(value);
58
+ }
59
+ }else{
60
+ //�d�݂������̎��͎��g��
61
+ for(i=0; i<PKL_COLOR; i++){
62
+ value = i - rate*rate/127 * sin(M_PI/2+M_PI*i/255);
63
+ ct[i] = PKL_RGB(value);
64
+ }
65
+ }
66
+
67
+ for(i=0; i<pkl->width*pkl->height*pkl->channel; i++) pkl->image[i] = ct[pkl->image[i]];
68
+
69
+ return(0);
70
+ }
71
+
72
+ //=================================================================================
73
+ // pkl_level def=0.0050
74
+ //=================================================================================
75
+ PKLExport int pkl_level(PKLImage pkl, double low, double high, double coeff)
76
+ {
77
+ PKL_HISTGRAM hst[PKL_CHANNEL];
78
+ int i, j, cnt, value;
79
+
80
+ if((low < 0.0 && low > 100.0) ||
81
+ (high < 0.0 && high > 100.0) ||
82
+ (coeff< 0.0 && coeff> 2.0)) return(1);
83
+
84
+ memset(&hst, 0, sizeof(hst));
85
+ for(i=0; i<pkl->height*pkl->width; i++){
86
+ for(j=0; j<pkl->channel; j++){
87
+ hst[j].data[ pkl->image[i*pkl->channel+j] ]++;
88
+ }
89
+ }
90
+
91
+ for(i=0; i<pkl->channel; i++){
92
+ cnt=0;
93
+ for(j=0; j<PKL_COLOR; j++){
94
+ cnt += hst[i].data[j];
95
+ if(cnt > low/100*pkl->height*pkl->width){
96
+ hst[i].min = j;
97
+ break;
98
+ }
99
+ }
100
+ cnt=0;
101
+ for(j=PKL_COLOR-1; j>=0; j--){
102
+ cnt += hst[i].data[j];
103
+ if(cnt > high/100*pkl->height*pkl->width){
104
+ hst[i].max = j;
105
+ break;
106
+ }
107
+ }
108
+ }
109
+
110
+ for(i=0; i<pkl->width*pkl->height; i++){
111
+ for(j=0; j<pkl->channel; j++){
112
+ value = ((coeff * pkl->image[i*pkl->channel+j])-hst[j].min)/(hst[j].max-hst[j].min) * 255;
113
+ pkl->image[i*pkl->channel+j] = PKL_RGB(value);
114
+ }
115
+ }
116
+
117
+ return(0);
118
+ }
119
+
120
+ //=============================================================================
121
+ // pkl_brightness
122
+ //=============================================================================
123
+ PKLExport int pkl_brightness(PKLImage pkl, int color)
124
+ {
125
+ int i;
126
+ for(i=0; i<pkl->width*pkl->height*pkl->channel; i++){
127
+ pkl->image[i] = PKL_RGB(pkl->image[i] + color);
128
+ }
129
+ return(0);
130
+ }
131
+
132
+ //=============================================================================
133
+ // pkl_hls
134
+ //=============================================================================
135
+ PKLExport int pkl_hls(PKLImage pkl, double ym, double sm, double hd)
136
+ {
137
+ int i;
138
+ double c1, c2; //�F�M��
139
+ double y, s, h; //y=�P�x, s=�ʓx, h=�F��
140
+
141
+ if(pkl->color != PKL_RGB) return(1);
142
+
143
+ for(i=0; i<pkl->width*pkl->height; i++){
144
+ /* �P�x,�F�M��1,2 */
145
+ y = 0.299 * pkl->image[i*pkl->channel] +
146
+ 0.587 * pkl->image[i*pkl->channel+1] +
147
+ 0.114 * pkl->image[i*pkl->channel+2];
148
+ c1 = pkl->image[i*pkl->channel] - y;
149
+ c2 = pkl->image[i*pkl->channel+2] - y;
150
+
151
+ /* �ʓx */
152
+ s = sqrt(c1*c1 + c2*c2);
153
+
154
+ /* �F�� */
155
+ if(s < 0.0) h = 0.0;
156
+ else h = atan2(c1, c2) * 180.0 / M_PI;
157
+
158
+ /* ������������ */
159
+ y *= (1 + ym);
160
+ s *= (1 + sm);
161
+ h += hd;
162
+
163
+ /* �����l����RGB�ɖ߂� */
164
+ c1 = s * sin(M_PI * h / 180.0);
165
+ c2 = s * cos(M_PI * h / 180.0);
166
+
167
+ pkl->image[i*pkl->channel] = PKL_RGB(c1 + y);
168
+ pkl->image[i*pkl->channel+1] = PKL_RGB(y - (0.299*c1 + 0.114*c2)/0.587);
169
+ pkl->image[i*pkl->channel+2] = PKL_RGB(c2 + y);
170
+ }
171
+ return(0);
172
+ }
173
+
174
+ //=================================================================================
175
+ // pkl_gamma
176
+ //=================================================================================
177
+ PKLExport int pkl_gamma(PKLImage pkl, double gm)
178
+ {
179
+ unsigned char gt[PKL_COLOR];
180
+ int i;
181
+
182
+ if(gm < 0.0) return(1);
183
+
184
+ for(i=0; i<PKL_COLOR; i++)
185
+ gt[i] = PKL_RGB(255.0 * pow(i/255.0, 1.0/gm));
186
+
187
+ for(i=0; i<pkl->width*pkl->height*pkl->channel; i++)
188
+ pkl->image[i] = gt[pkl->image[i]];
189
+
190
+ return(0);
191
+ }
@@ -0,0 +1,24 @@
1
+ #ifndef _LIB_PIKL_EFFECT_
2
+ #define _LIB_PIKL_EFFECT_
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
+ //unsharp target pixel
13
+ #define RADIUS 1
14
+
15
+ #ifndef M_PI
16
+ #define M_PI 3.14159265358979323846
17
+ #endif
18
+
19
+ typedef struct {
20
+ int max, min;
21
+ long data[PKL_COLOR];
22
+ } PKL_HISTGRAM;
23
+
24
+ #endif
@@ -0,0 +1,137 @@
1
+ #include "pikl_io.h"
2
+
3
+ static int format_type(PKLImage pkl, FILE *image);
4
+ //=============================================================================
5
+ // pkl_open
6
+ //=============================================================================
7
+ PKLExport PKLImage pkl_open(const char *in)
8
+ {
9
+ PKLImage pkl;
10
+ FILE *image;
11
+ int result=0;
12
+
13
+ pkl = malloc( sizeof(struct _PKLImage) );
14
+ if(!pkl) return(NULL);
15
+ memset(pkl, 0, sizeof(struct _PKLImage));
16
+
17
+ pkl->compress = -1;
18
+
19
+ image = fopen(in, "rb");
20
+ if(!image) return(NULL);
21
+
22
+ format_type(pkl, image);
23
+
24
+ switch(pkl->format){
25
+ case PKL_FORMAT_JPEG:
26
+ result = load_jpeg(pkl, image);
27
+ break;
28
+ case PKL_FORMAT_PNG:
29
+ result = load_png(pkl, image);
30
+ break;
31
+ case PKL_FORMAT_BITMAP:
32
+ result = load_bitmap(pkl, image);
33
+ break;
34
+ default:
35
+ result = 1;
36
+ }
37
+ fclose(image);
38
+
39
+ if(result){
40
+ free(pkl);
41
+ pkl = NULL;
42
+ }
43
+
44
+ return(pkl);
45
+ }
46
+
47
+ //=============================================================================
48
+ // pkl_close
49
+ //=============================================================================
50
+ PKLExport void pkl_close(PKLImage pkl)
51
+ {
52
+ if(pkl->image) free(pkl->image);
53
+ free(pkl);
54
+ }
55
+
56
+ //=============================================================================
57
+ // format_type
58
+ //=============================================================================
59
+ static int format_type(PKLImage pkl, FILE *image)
60
+ {
61
+ unsigned char mark[PKL_HEADER_SIZE];
62
+ int count, i;
63
+
64
+ if( fread(mark, 1, PKL_HEADER_SIZE, image) < PKL_HEADER_SIZE ) return(1);
65
+
66
+ count = sizeof(pklheader) /sizeof(struct PKL_HEADER);
67
+ for(i=0; i<count; i++){
68
+ if( !memcmp(mark, pklheader[i].mark, pklheader[i].size) ){
69
+ pkl->format = pklheader[i].format;
70
+ fseek(image, 0, SEEK_SET);
71
+ return(0);
72
+ }
73
+ }
74
+ return(1);
75
+ }
76
+
77
+ //=============================================================================
78
+ // pkl_format
79
+ //=============================================================================
80
+ PKLExport PKL_FORMAT pkl_format(PKLImage pkl)
81
+ {
82
+ return(pkl->format);
83
+ }
84
+
85
+ //=================================================================================
86
+ // pkl_width
87
+ //=================================================================================
88
+ PKLExport int pkl_width(PKLImage pkl)
89
+ {
90
+ return(pkl->width);
91
+ }
92
+
93
+ //=================================================================================
94
+ // pkl_height
95
+ //=================================================================================
96
+ PKLExport int pkl_height(PKLImage pkl)
97
+ {
98
+ return(pkl->height);
99
+ }
100
+
101
+ //=============================================================================
102
+ // pkl_compress
103
+ //=============================================================================
104
+ PKLExport int pkl_compress(PKLImage pkl, int level)
105
+ {
106
+ if(level < 0 || level > 10) return(pkl->compress);
107
+ pkl->compress = level;
108
+ return(pkl->compress);
109
+ }
110
+
111
+ //=================================================================================
112
+ // pkl_save
113
+ //=================================================================================
114
+ PKLExport int pkl_save(PKLImage pkl, const char *out, PKL_FORMAT format)
115
+ {
116
+ FILE *image;
117
+ int result=0;
118
+
119
+ image = fopen(out, "wb");
120
+ if(!image) return(1);
121
+
122
+ switch(format){
123
+ case PKL_FORMAT_JPEG:
124
+ result = save_jpeg(pkl, image);
125
+ break;
126
+ case PKL_FORMAT_PNG:
127
+ result = save_png(pkl, image);
128
+ break;
129
+ case PKL_FORMAT_BITMAP:
130
+ result = save_bitmap(pkl, image);
131
+ break;
132
+ default:
133
+ result=1;
134
+ }
135
+ fclose(image);
136
+ return(result);
137
+ }