pikl 0.2.2-x86-mswin32

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.
data/History.txt ADDED
@@ -0,0 +1,8 @@
1
+ == 0.2.1
2
+ * fix gem for mswin32.
3
+
4
+ == 0.2.0
5
+ * Supports method chain.
6
+
7
+ == 0.1.0
8
+ * First release!
data/License.txt ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2008 pikl Ryota Maruko
2
+ Copyright (c) 2008 libpikl Keiko Soejima
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest.txt ADDED
@@ -0,0 +1,37 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ config/hoe.rb
7
+ config/requirements.rb
8
+ ext/pikl/extconf.rb
9
+ ext/pikl/pikl.h
10
+ ext/pikl/pikl_bitmap.c
11
+ ext/pikl/pikl_bitmap.h
12
+ ext/pikl/pikl_effect.c
13
+ ext/pikl/pikl_effect.h
14
+ ext/pikl/pikl_io.c
15
+ ext/pikl/pikl_io.h
16
+ ext/pikl/pikl_jpeg.c
17
+ ext/pikl/pikl_jpeg.h
18
+ ext/pikl/pikl_png.c
19
+ ext/pikl/pikl_png.h
20
+ ext/pikl/pikl_private.h
21
+ ext/pikl/pikl_resize.c
22
+ ext/pikl/pikl_resize.h
23
+ ext/pikl/pikl_rotate.c
24
+ ext/pikl/pikl_rotate.h
25
+ ext/pikl/pikl_trim.c
26
+ ext/pikl/pikl_trim.h
27
+ lib/pikl.rb
28
+ lib/pikl/const.rb
29
+ lib/pikl/errors.rb
30
+ lib/pikl/ext.rb
31
+ lib/pikl/image.rb
32
+ lib/pikl/pikl.dll
33
+ lib/pikl/version.rb
34
+ setup.rb
35
+ test/sample.jpg
36
+ test/test_helper.rb
37
+ test/test_pikl_image.rb
data/README.txt ADDED
@@ -0,0 +1,66 @@
1
+ = pikl
2
+
3
+ Pikl is an image librarry for ruby. This library aims easily image processing. Supports JPEG, PNG and BITMAP.
4
+
5
+ === Installation of pikl
6
+
7
+ The simplest way is to install the gem:
8
+
9
+ $ sudo gem install pikl
10
+
11
+ To use option for libjpeg and libpng directories:
12
+
13
+ $ sudo gem install pikl -- --with-opt-dir=path/to/libraries
14
+
15
+ == SYNOPSIS:
16
+
17
+ Basic use of pikl:
18
+ require "rubygems"
19
+ require "pikl"
20
+ Pikl::Image.open('path/to/image.jpg') do |img|
21
+ img.trim(10,5,-10,-5)
22
+ img.save('path/to/output.png', :png)
23
+ end
24
+
25
+ Use method chain for processing image:
26
+ require "rubygems"
27
+ require "pikl"
28
+ Pikl::Image.open('path/to/image.jpg') do |img|
29
+ img.resize(120,:auto).rotate(90).save('path/to/output.png')
30
+ end
31
+
32
+
33
+ == REQUIREMENTS:
34
+
35
+ Currently, pikl-core (implemented with C) depends on the following libraries:
36
+
37
+ * libjpeg
38
+ * libpng
39
+
40
+ notice: pikl use binary library on windows. so these libraries aren't necessary on windows.
41
+
42
+ == LICENSE:
43
+
44
+ (The MIT License)
45
+
46
+ Copyright (c) 2008 pikl.rb Ryota Maruko
47
+ Copyright (c) 2008 pikl.so Keiko Soejima
48
+
49
+ Permission is hereby granted, free of charge, to any person obtaining
50
+ a copy of this software and associated documentation files (the
51
+ 'Software'), to deal in the Software without restriction, including
52
+ without limitation the rights to use, copy, modify, merge, publish,
53
+ distribute, sublicense, and/or sell copies of the Software, and to
54
+ permit persons to whom the Software is furnished to do so, subject to
55
+ the following conditions:
56
+
57
+ The above copyright notice and this permission notice shall be
58
+ included in all copies or substantial portions of the Software.
59
+
60
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
61
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
62
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
63
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
64
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
65
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
66
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ PACKAGE_PLATFORM = (ARGV[0] =~ /mswin32$/) ? 'mswin32' : RUBY_PLATFORM
2
+ require 'config/requirements'
3
+ require 'config/hoe' # setup Hoe + all gem configuration
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
data/config/hoe.rb ADDED
@@ -0,0 +1,89 @@
1
+ require 'pikl/version'
2
+
3
+ AUTHOR = 'Ryota Maruko' # can also be an array of Authors
4
+ EMAIL = ""
5
+ DESCRIPTION = "Pikl is an image librarry for ruby. This library aims easily image processing. Supports JPEG, PNG and BITMAP."
6
+ GEM_NAME = 'pikl' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'pikl' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+
11
+ @config_file = "~/.rubyforge/user-config.yml"
12
+ @config = nil
13
+ RUBYFORGE_USERNAME = "unknown"
14
+ def rubyforge_username
15
+ unless @config
16
+ begin
17
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
18
+ rescue
19
+ puts <<-EOS
20
+ ERROR: No rubyforge config file found: #{@config_file}
21
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
22
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
23
+ EOS
24
+ exit
25
+ end
26
+ end
27
+ RUBYFORGE_USERNAME.replace @config["username"]
28
+ end
29
+
30
+ REV = nil
31
+ # UNCOMMENT IF REQUIRED:
32
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
33
+ VERS = Pikl::VERSION::STRING + (REV ? ".#{REV}" : "")
34
+ RDOC_OPTS = ['--quiet', '--title', 'pikl documentation',
35
+ "--opname", "index.html",
36
+ "--line-numbers",
37
+ "--main", "README",
38
+ "--inline-source"]
39
+
40
+ class Hoe
41
+ def extra_deps
42
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
43
+ @extra_deps
44
+ end
45
+
46
+ def spec= s
47
+
48
+ if PACKAGE_PLATFORM =~ /mswin32/
49
+ s.files = s.files.reject! {|f| f =~ /extconf\.rb/ }
50
+ else
51
+ s.files = s.files.reject! {|f| f =~ /pikl\.dll/ }
52
+ end
53
+ @spec = s
54
+ end
55
+ end
56
+
57
+ # Generate all the Rake tasks
58
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
59
+ $hoe = Hoe.new(GEM_NAME, VERS) do |p|
60
+ p.developer(AUTHOR, EMAIL)
61
+ p.description = DESCRIPTION
62
+ p.summary = DESCRIPTION
63
+ p.url = HOMEPATH
64
+ p.rubyforge_name = (PACKAGE_PLATFORM =~ /mswin32/) ? "#{GEM_NAME}-mswin32" : GEM_NAME
65
+
66
+ p.test_globs = ["test/**/test_*.rb"]
67
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
68
+
69
+ # == Optional
70
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
71
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
72
+ if PACKAGE_PLATFORM =~ /mswin32/
73
+ platform = Gem::Platform.new('mswin32')
74
+ p.spec_extras[:platform] = platform
75
+ else
76
+ p.spec_extras[:extensions] = ['ext/pikl/extconf.rb']
77
+ end
78
+ # A hash of extra values to set in the gemspec.
79
+ end
80
+
81
+ CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
82
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
83
+ $hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
84
+ $hoe.rsync_args = '-av --delete --ignore-errors'
85
+
86
+
87
+
88
+
89
+
@@ -0,0 +1,15 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
data/ext/pikl/pikl.h ADDED
@@ -0,0 +1,264 @@
1
+ /*! @file
2
+ @brief image library "pikl"
3
+ simple image library
4
+ @author soe(soezimaster@gmail.com)
5
+ @date 2008.4
6
+ */
7
+ #ifndef _LIB_PIKL_
8
+ #define _LIB_PIKL_
9
+
10
+ #ifdef WIN32
11
+ #define PKLExport __declspec(dllexport)
12
+ #define XMD_H
13
+ #else
14
+ #define PKLExport
15
+ #endif
16
+
17
+ #if defined __cplusplus
18
+ extern "C" {
19
+ #endif
20
+
21
+ /*! version */
22
+ #define PKL_VERSION "libpikl 0.1"
23
+
24
+ // PKLImage
25
+ typedef struct _PKLImage * PKLImage;
26
+
27
+ /*!
28
+ support format<br>
29
+ ■JPEG:<br>
30
+ gray, rgb, cmyk<br>
31
+ * color-typeのYCbCrおよびYCCには読み取りも保存も対応していません。<br>
32
+ ■PNG:<br>
33
+ gray,gray-alpha,rgb,rgb-alpha,palette<br>
34
+ * alphaは読取は可能ですが、保存はできません。<br>
35
+ * paletteはRGBとして扱われます。<br>
36
+ ■BMP(windows bitmap):<br>
37
+ 24bit only
38
+ */
39
+ typedef enum {
40
+ PKL_FORMAT_ERROR, /*!< error */
41
+ PKL_FORMAT_JPEG, /*!< jpeg */
42
+ PKL_FORMAT_PNG, /*!< png */
43
+ PKL_FORMAT_BITMAP /*!< windows bitmap */
44
+ } PKL_FORMAT;
45
+
46
+ /*! color type */
47
+ typedef enum {
48
+ PKL_UNKNOWN, /*!< error */
49
+ PKL_BLACKWHITE, /*!< non support */
50
+ PKL_GRAYSCALE, /*!< gray scale */
51
+ PKL_RGB, /*!< 24bit RGB */
52
+ PKL_RGBA, /*!< non support */
53
+ PKL_YCbCr, /*!< non support */
54
+ PKL_CMYK, /*!< CMYK(for jpeg) */
55
+ PKL_YCCK /*!< non support */
56
+ } PKL_COLOR_SPACE;
57
+
58
+ /*! right turn angle(for rotate) */
59
+ typedef enum {
60
+ PKL_ANGLE_000,
61
+ PKL_ANGLE_090,
62
+ PKL_ANGLE_180,
63
+ PKL_ANGLE_270
64
+ } PKL_ANGLE;
65
+
66
+ /*!
67
+ sampling pattern(for resize)<br>
68
+ ※全てのサンプリングパターンに拡大縮小対応しています。<br>
69
+ ※但し、pixcel averageで拡大をする場合は、lanczosと品質は同じになります。
70
+ */
71
+ typedef enum {
72
+ PKL_SAMPLE_ERROR,
73
+ PKL_SAMPLE_NN, /*!< nearest neighbor */
74
+ PKL_SAMPLE_BL, /*!< bilinear */
75
+ PKL_SAMPLE_BC, /*!< bicubic */
76
+ PKL_SAMPLE_PA, /*!< pixcel average */
77
+ PKL_SAMPLE_LZ /*!< lanczos */
78
+ } PKL_SAMPLE;
79
+
80
+ //=============================================================================
81
+ // method
82
+ //=============================================================================
83
+ /*!
84
+ PKLImageを構築します。<br>
85
+ エラーが発生した場合は、NULLが返ります。<br>
86
+ @param in [in] ロードする画像
87
+ @return 成功した場合は、PKLImageのポインタが返ります。失敗したときはNULLが返ります。
88
+ */
89
+ PKLExport PKLImage pkl_open(const char *in);
90
+
91
+ /*!
92
+ 画像フォーマットを返します。<br>
93
+ @param pkl [in] PKLImageオブジェクト
94
+ @return PKL_FORMAT参照
95
+ */
96
+ PKLExport PKL_FORMAT pkl_format(PKLImage pkl);
97
+
98
+ /*!
99
+ 画像の幅を返します。<br>
100
+ @param pkl [in] PKLImageオブジェクト
101
+ @return 画像の幅(ピクセル数)
102
+ */
103
+ PKLExport int pkl_width(PKLImage pkl);
104
+
105
+ /*!
106
+ 画像の高さを返します。<br>
107
+ @param pkl [in] PKLImageオブジェクト
108
+ @return 画像の高さ(ピクセル数)
109
+ */
110
+ PKLExport int pkl_height(PKLImage pkl);
111
+
112
+ /*!
113
+ トリミングします。<br>
114
+ ・パラメータは左上原点です。<br>
115
+ ・パラメータは左上原点です。<br>
116
+ ・左上原点はsx=0, sy=0になります。<br>
117
+ @param pkl [in] PKLImageオブジェクト
118
+ @param sx [in] 切り出し位置(x)
119
+ @param sy [in] 切り出し位置(y)
120
+ @param width [in] sxからの切り出す幅(ピクセル数)
121
+ @param height [in] syからの切り出す高さ(ピクセル数)
122
+ @return 成功した場合は0。失敗した場合は真を返します。
123
+ */
124
+ PKLExport int pkl_trim(PKLImage pkl, int sx, int sy, int width, int height);
125
+
126
+ /*!
127
+ 左回り90度単位で回転します。<br>
128
+ @param pkl [in] PKLImageオブジェクト
129
+ @param angle [in] 回転角度(PKL_ANGLE参照)
130
+ @return 成功した場合は0。失敗した場合は真を返します。
131
+ */
132
+ PKLExport int pkl_rotate(PKLImage pkl, PKL_ANGLE angle);
133
+
134
+ /*!
135
+ 拡大・縮小します。<br>
136
+ @param pkl [in] PKLImageオブジェクト
137
+ @param width [in] 横方向の変形後サイズ(ピクセル数)
138
+ @param height [in] 縦方向の変形後サイズ(ピクセル数)
139
+ @param sample [in] サンプリングパターン(PKL_SAMPLE参照)
140
+ @return 成功した場合は0。失敗した場合は真を返します。
141
+ */
142
+ PKLExport int pkl_resize(PKLImage pkl, int width, int height, PKL_SAMPLE sample);
143
+
144
+ /*!
145
+ アンシャープ処理<br>
146
+ @param pkl [in] PKLImageオブジェクト
147
+ @param threshold [in]
148
+ 適応量。0から255の範囲で指定します。
149
+ 0の時:変化しません。
150
+ 255の時:全ての色にアンシャープ処理が働きます
151
+ @param edge [in]
152
+ エッジ。想定結果が得られる範囲は-10 .. 10程度です。
153
+ edge=0の時:変化しません。
154
+ edge>0の時:値が大きいほど先鋭化されます。
155
+ edge<0の時:値が小さいほどぼやけます。
156
+
157
+ 想定結果が得られる範囲は-10 .. 10程度です。
158
+ これを超える場合は、品質の保証はありません。
159
+ ※画質によりこの通りではありません。-10..10の範囲より小さくなる可能性があります。
160
+ @return 成功した場合は0。失敗した場合は真を返します。
161
+ */
162
+ PKLExport int pkl_unsharp(PKLImage pkl, int threshold, double edge);
163
+
164
+ /*!
165
+ コントラスト調整<br>
166
+ @param pkl [in] PKLImageオブジェクト
167
+ @param rate [in]
168
+ 調整係数。-127 .. 127の範囲で指定します。<br>
169
+ 0の時:変化しません。
170
+ * rateが0以上の時は周波数によるコントラスト強調がおこなわれます。
171
+ * rateが0未満の時は直線的に平坦化されます。
172
+ @return 成功した場合は0。失敗した場合は真を返します。
173
+ */
174
+ PKLExport int pkl_contrast(PKLImage pkl, int rate);
175
+
176
+ /*!
177
+ レベル補正<br>
178
+ ヒストグラムの平坦化をおこないます。各色の分布に偏りがある画像に有効な場合があります。
179
+ @param pkl [in] PKLImageオブジェクト
180
+ @param low [in]
181
+ 全ピクセル数に対して、切り捨てる暗い色の総数の割合(0-100%)
182
+ @param high [in]
183
+ 全ピクセル数に対して、切り捨てる明るい色の総数の割合(0-100%)
184
+ @param coeff [in]
185
+ 平坦化時の係数。
186
+ 1が標準です。1より小さい場合は暗く、1より大きい場合は明るくなります(0.0..2.0)
187
+ @return 成功した場合は0。失敗した場合は真を返します。
188
+ */
189
+ PKLExport int pkl_level(PKLImage pkl, double low, double high, double coeff);
190
+
191
+ /*!
192
+ 明るさ調整<br>
193
+ 各色にcolor値を加算する単純な処理です。
194
+ @param pkl [in] PKLImageオブジェクト
195
+ @param color [in]
196
+ 各色に加算する色。
197
+ 255を指定すれば、ただの白い画像になります。
198
+ -255を指定すると、ただの黒い画像になります。
199
+ @return 成功した場合は0。失敗した場合は真を返します。
200
+ */
201
+ PKLExport int pkl_brightness(PKLImage pkl, int color);
202
+
203
+ /*!
204
+ 輝度(明るさ)・彩度(鮮やかさ)・色相(色合い)調整<br>
205
+ @param pkl [in] PKLImageオブジェクト
206
+ @param ym [in]
207
+ 輝度(-1..1)。
208
+ +0.1で10%up。
209
+ 0.0は変化なし
210
+ @param sm [in]
211
+ 彩度(-1..1)。
212
+ +0.1で10%up。
213
+ 0.0は変化なし
214
+ @param hd [in]
215
+ 色相(360度回転)。
216
+ 360度の倍数では変化しません。
217
+ 参考:R=113.2/Ye=173.0/G=225.0/Cy=293.2/B=353.0/Mg=45.0
218
+ @return 成功した場合は0。失敗した場合は真を返します。
219
+ */
220
+ PKLExport int pkl_hls(PKLImage pkl, double ym, double sm, double hd);
221
+
222
+ /*!
223
+ ガンマ補正<br>
224
+ @param pkl [in] PKLImageオブジェクト
225
+ @param gm [in]
226
+ 補正係数(0.0以上の値が指定できます)。
227
+ 1.0より小さい時は暗く、1.0より大きい時は明るく調整されます。
228
+ 1.0を指定した時は調整されません。
229
+ @return 成功した場合は0。失敗した場合は真を返します。
230
+ */
231
+ PKLExport int pkl_gamma(PKLImage pkl, double gm);
232
+
233
+ /*!
234
+ PNG/JPEG保存時の圧縮率<br>
235
+ @param pkl [in] PKLImageオブジェクト
236
+ @param level [in]
237
+ 0(無圧縮) .. 10(最高圧縮)。
238
+ 指定がない場合は、デフォルトレベルが設定されます。
239
+ @return 成功した場合は0。失敗した場合は真を返します。
240
+ */
241
+ PKLExport int pkl_compress(PKLImage pkl, int level);
242
+
243
+ /*!
244
+ 保存<br>
245
+ @param pkl [in] PKLImageオブジェクト
246
+ @param out [in] 保存ファイル名
247
+ @param format [in] 保存フォーマット(PKL_FORMAT参照)
248
+ @return 成功した場合は0。失敗した場合は真を返します。
249
+ */
250
+ PKLExport int pkl_save(PKLImage pkl, const char *out, PKL_FORMAT format);
251
+
252
+ /*!
253
+ PKLImageオブジェクトの破棄<br>
254
+ 確保していたメモリを全て解放します。用がなくなったら実行するようにしてください。
255
+ @param pkl [in] PKLImageオブジェクト
256
+ @return 成功した場合は0。失敗した場合は真を返します。
257
+ */
258
+ PKLExport void pkl_close(PKLImage pkl);
259
+
260
+ #if defined __cplusplus
261
+ }
262
+ #endif
263
+
264
+ #endif
@@ -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
+