rqr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,152 @@
1
+ //---(1)ここから-------------------------
2
+ //// QR_Encode.h : CQR_Encode クラス宣言およびインターフェイス定義
3
+ //// Date 2006/05/17 Ver. 1.12 [Class Ver.1.22] Psytec Inc.
4
+ //
5
+ //#if !defined(AFX_QR_ENCODE_H__AC886DF7_C0AE_4C9F_AC7A_FCDA8CB1DD37__INCLUDED_)
6
+ //#define AFX_QR_ENCODE_H__AC886DF7_C0AE_4C9F_AC7A_FCDA8CB1DD37__INCLUDED_
7
+ //
8
+ //#if _MSC_VER > 1000
9
+ //#pragma once
10
+ //#endif // _MSC_VER > 1000
11
+ //---(1)ここまでをコメントアウト-----------
12
+
13
+ //---(2)ここから-----------
14
+ #ifndef __QR_ENCODER__
15
+ #define __QR_ENCODER__
16
+ #include "win2ansi.h"
17
+ //---(2)ここまでを追加-----------
18
+
19
+ /////////////////////////////////////////////////////////////////////////////
20
+ // 定数
21
+
22
+ // 誤り訂正レベル
23
+ #define QR_LEVEL_L 0
24
+ #define QR_LEVEL_M 1
25
+ #define QR_LEVEL_Q 2
26
+ #define QR_LEVEL_H 3
27
+
28
+ // データモード
29
+ #define QR_MODE_NUMERAL 0
30
+ #define QR_MODE_ALPHABET 1
31
+ #define QR_MODE_8BIT 2
32
+ #define QR_MODE_KANJI 3
33
+
34
+ // バージョン(型番)グループ
35
+ #define QR_VRESION_S 0 // 1 〜 9
36
+ #define QR_VRESION_M 1 // 10 〜 26
37
+ #define QR_VRESION_L 2 // 27 〜 40
38
+
39
+ #define MAX_ALLCODEWORD 3706 // 総コードワード数最大値
40
+ #define MAX_DATACODEWORD 2956 // データコードワード最大値(バージョン40-L)
41
+ #define MAX_CODEBLOCK 153 // ブロックデータコードワード数最大値(RSコードワードを含む)
42
+ #define MAX_MODULESIZE 177 // 一辺モジュール数最大値
43
+
44
+ // ビットマップ描画時マージン
45
+ #define QR_MARGIN 4
46
+
47
+
48
+ /////////////////////////////////////////////////////////////////////////////
49
+ typedef struct tagRS_BLOCKINFO
50
+ {
51
+ int ncRSBlock; // RSブロック数
52
+ int ncAllCodeWord; // ブロック内コードワード数
53
+ int ncDataCodeWord; // データコードワード数(コードワード数 - RSコードワード数)
54
+
55
+ } RS_BLOCKINFO, *LPRS_BLOCKINFO;
56
+
57
+
58
+ /////////////////////////////////////////////////////////////////////////////
59
+ // QRコードバージョン(型番)関連情報
60
+
61
+ typedef struct tagQR_VERSIONINFO
62
+ {
63
+ int nVersionNo; // バージョン(型番)番号(1〜40)
64
+ int ncAllCodeWord; // 総コードワード数
65
+
66
+ // 以下配列添字は誤り訂正率(0 = L, 1 = M, 2 = Q, 3 = H)
67
+ int ncDataCodeWord[4]; // データコードワード数(総コードワード数 - RSコードワード数)
68
+
69
+ int ncAlignPoint; // アライメントパターン座標数
70
+ int nAlignPoint[6]; // アライメントパターン中心座標
71
+
72
+ RS_BLOCKINFO RS_BlockInfo1[4]; // RSブロック情報(1)
73
+ RS_BLOCKINFO RS_BlockInfo2[4]; // RSブロック情報(2)
74
+
75
+ } QR_VERSIONINFO, *LPQR_VERSIONINFO;
76
+
77
+
78
+ /////////////////////////////////////////////////////////////////////////////
79
+ // CQR_Encode クラス
80
+
81
+ class CQR_Encode
82
+ {
83
+ // 構築/消滅
84
+ public:
85
+ CQR_Encode();
86
+ ~CQR_Encode();
87
+
88
+ public:
89
+ int m_nLevel; // 誤り訂正レベル
90
+ int m_nVersion; // バージョン(型番)
91
+ BOOL m_bAutoExtent; // バージョン(型番)自動拡張指定フラグ
92
+ int m_nMaskingNo; // マスキングパターン番号
93
+
94
+ public:
95
+ int m_nSymbleSize;
96
+ BYTE m_byModuleData[MAX_MODULESIZE][MAX_MODULESIZE]; // [x][y]
97
+ // bit5:機能モジュール(マスキング対象外)フラグ
98
+ // bit4:機能モジュール描画データ
99
+ // bit1:エンコードデータ
100
+ // bit0:マスク後エンコード描画データ
101
+ // 20hとの論理和により機能モジュール判定、11hとの論理和により描画(最終的にはBOOL値化)
102
+
103
+ private:
104
+ int m_ncDataCodeWordBit; // データコードワードビット長
105
+ BYTE m_byDataCodeWord[MAX_DATACODEWORD]; // 入力データエンコードエリア
106
+
107
+ int m_ncDataBlock;
108
+ BYTE m_byBlockMode[MAX_DATACODEWORD];
109
+ int m_nBlockLength[MAX_DATACODEWORD];
110
+
111
+ int m_ncAllCodeWord; // 総コードワード数(RS誤り訂正データを含む)
112
+ BYTE m_byAllCodeWord[MAX_ALLCODEWORD]; // 総コードワード算出エリア
113
+ BYTE m_byRSWork[MAX_CODEBLOCK]; // RSコードワード算出ワーク
114
+
115
+ // データエンコード関連ファンクション
116
+ public:
117
+ BOOL EncodeData(int nLevel, int nVersion, BOOL bAutoExtent, int nMaskingNo, LPCSTR lpsSource, int ncSource = 0);
118
+
119
+ private:
120
+ int GetEncodeVersion(int nVersion, LPCSTR lpsSource, int ncLength);
121
+ BOOL EncodeSourceData(LPCSTR lpsSource, int ncLength, int nVerGroup);
122
+
123
+ int GetBitLength(BYTE nMode, int ncData, int nVerGroup);
124
+
125
+ int SetBitStream(int nIndex, WORD wData, int ncData);
126
+
127
+ BOOL IsNumeralData(unsigned char c);
128
+ BOOL IsAlphabetData(unsigned char c);
129
+ BOOL IsKanjiData(unsigned char c1, unsigned char c2);
130
+
131
+ BYTE AlphabetToBinaly(unsigned char c);
132
+ WORD KanjiToBinaly(WORD wc);
133
+
134
+ void GetRSCodeWord(LPBYTE lpbyRSWork, int ncDataCodeWord, int ncRSCodeWord);
135
+
136
+ // モジュール配置関連ファンクション
137
+ private:
138
+ void FormatModule();
139
+
140
+ void SetFunctionModule();
141
+ void SetFinderPattern(int x, int y);
142
+ void SetAlignmentPattern(int x, int y);
143
+ void SetVersionPattern();
144
+ void SetCodeWordPattern();
145
+ void SetMaskingPattern(int nPatternNo);
146
+ void SetFormatInfoPattern(int nPatternNo);
147
+ int CountPenalty();
148
+ };
149
+
150
+ /////////////////////////////////////////////////////////////////////////////
151
+
152
+ #endif // !defined(AFX_QR_ENCODE_H__AC886DF7_C0AE_4C9F_AC7A_FCDA8CB1DD37__INCLUDED_)
@@ -0,0 +1,39 @@
1
+ =begin
2
+ usage: ruby extconf.rb [options ...]
3
+ configure options:
4
+ --with-opt-dir=/path/to/libraries
5
+ --with-jpeg-include=dir
6
+ --with-jpeg-lib=dir
7
+ --with-png-include=dir
8
+ --with-png-lib=dir
9
+ --with-tiff-include=dir
10
+ --with-tiff-lib=dir
11
+ =end
12
+ require 'mkmf'
13
+ require 'rbconfig'
14
+ $libs = append_library($libs, "supc++")
15
+
16
+ DARWIN_PORT_DIR = '/sw'
17
+
18
+ if RUBY_PLATFORM =~ /darwin/
19
+ dir_config('jpeg', DARWIN_PORT_DIR)
20
+ dir_config('png', DARWIN_PORT_DIR)
21
+ dir_config('tiff', DARWIN_PORT_DIR)
22
+ else
23
+ dir_config('jpeg')
24
+ dir_config('png')
25
+ dir_config('tiff')
26
+ end
27
+
28
+ if have_header('jpeglib.h') && have_library('jpeg')
29
+ $CFLAGS += ' -DUSE_JPG'
30
+ end
31
+
32
+ if have_header('png.h') && have_library('png')
33
+ $CFLAGS += ' -DUSE_PNG'
34
+ end
35
+
36
+ if have_header('tiff.h') && have_library('tiff')
37
+ $CFLAGS += ' -DUSE_TIFF'
38
+ end
39
+ create_makefile('rqr/QR')
data/ext/rqr/qr.i ADDED
@@ -0,0 +1,17 @@
1
+ %module QR
2
+ %{
3
+ #include "win2ansi.h"
4
+ #include "QR_Encode.h"
5
+ #include "qr_draw.h"
6
+ #include "qr_draw_jpeg.h"
7
+ #include "qr_draw_png.h"
8
+ #include "qr_draw_ps.h"
9
+ #include "qr_draw_tiff.h"
10
+ %}
11
+ %include win2ansi.h
12
+ %include qr_draw.h
13
+ %include qr_draw_jpeg.h
14
+ %include qr_draw_png.h
15
+ %include qr_draw_ps.h
16
+ %include qr_draw_tiff.h
17
+ %include QR_Encode.h
data/ext/rqr/qr_draw.h ADDED
@@ -0,0 +1,34 @@
1
+ #ifndef _QR_DRAW_
2
+ #define _QR_DRAW_
3
+
4
+ #define MARGIN_SIZE 4 /* マージンサイズ */
5
+ #define MAX_MODULESIZE 177 /* データバイト列の領域サイズ */
6
+
7
+ //=============================================================================
8
+ // QRDraw クラス
9
+ //=============================================================================
10
+ class QRDraw
11
+ {
12
+ public:
13
+ virtual ~QRDraw(){}
14
+
15
+ void setup(char *filename, int modulesize, int symbolsize){
16
+ this->msize = modulesize;
17
+ this->ssize = symbolsize;
18
+ this->rsize = (this->ssize + MARGIN_SIZE * 2) * this->msize;
19
+ this->filename = filename;
20
+ }
21
+
22
+ protected:
23
+ unsigned char **bit_image; //ピクセルイメージを格納する
24
+ int msize; // 1ドットを表現するピクセル数(=modulesize)
25
+ int rsize; // マージンを含めた実際のイメージの一辺
26
+ int ssize; // シンボルサイズ(マージンを含めない、ドットの個数)
27
+ char *filename; // 保存するファイル名
28
+
29
+ public:
30
+ virtual int draw(char *filename, int modulesize, int symbolsize,
31
+ unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE], void *opt) = 0;
32
+ };
33
+
34
+ #endif
@@ -0,0 +1,131 @@
1
+ #include "qr_draw_jpeg.h"
2
+
3
+ //=================================================================================
4
+ // QRDrawJPEG::QRDrawJPEG
5
+ //=================================================================================
6
+ QRDrawJPEG::QRDrawJPEG()
7
+ {
8
+ #ifdef USE_JPG
9
+ bit_image = NULL;
10
+ #endif
11
+ }
12
+
13
+ //=================================================================================
14
+ // QRDrawJPEG::~QRDrawJPEG
15
+ //=================================================================================
16
+ QRDrawJPEG::~QRDrawJPEG()
17
+ {
18
+ #ifdef USE_JPG
19
+ int i;
20
+
21
+ if(bit_image){
22
+ for(i=0; i<this->rsize; i++){
23
+ free(bit_image[i]);
24
+ }
25
+ free(bit_image);
26
+ }
27
+ #endif
28
+ }
29
+
30
+ //=============================================================================
31
+ // QRDrawJPEG::draw
32
+ //=============================================================================
33
+ int QRDrawJPEG::draw(char *filename, int modulesize, int symbolsize,
34
+ unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE], void *opt)
35
+ {
36
+ #ifdef USE_JPG
37
+ setup(filename, modulesize, symbolsize);
38
+
39
+ /* グレースケールイメージの構築 */
40
+ if( this->raster(data) ) return(1);
41
+
42
+ /* JPEG書き出し */
43
+ if( this->write() ) return(1);
44
+
45
+ return(0);
46
+ #else
47
+ return(1);
48
+ #endif
49
+ }
50
+
51
+ //=================================================================================
52
+ // QRDrawJPEG::raster
53
+ //=================================================================================
54
+ int QRDrawJPEG::raster(unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE])
55
+ {
56
+ #ifdef USE_JPG
57
+ int i, j, k;
58
+
59
+ /* 実際にデータを置く領域を確保 */
60
+ bit_image = (unsigned char **)malloc(sizeof(unsigned char *) * this->rsize);
61
+ for(i=0; i<this->rsize; i++){
62
+ bit_image[i] = (unsigned char *)malloc(this->rsize);
63
+ memset(bit_image[i], 0xFF, this->rsize);
64
+ }
65
+
66
+ for(i=0; i<this->ssize; i++){
67
+ for(j=0; j<this->ssize; j++){
68
+ /* 1行分生成 */
69
+ for(k=0; k<this->msize; k++){
70
+ bit_image[(i+MARGIN_SIZE)*this->msize][ (j+MARGIN_SIZE)*this->msize + k ] = data[j][i] ? 0 : 255;
71
+ }
72
+ }
73
+ /* モジュールサイズ分縦方向に増やす */
74
+ for(k=1; k<this->msize; k++){
75
+ memcpy(bit_image[(i+MARGIN_SIZE)*this->msize+k], bit_image[(i+MARGIN_SIZE)*this->msize], this->rsize);
76
+ }
77
+ }
78
+
79
+ return(0);
80
+ #else
81
+ return(1);
82
+ #endif
83
+ }
84
+
85
+ //=================================================================================
86
+ // QRDrawJPEG::write
87
+ //=================================================================================
88
+ int QRDrawJPEG::write()
89
+ {
90
+ #ifdef USE_JPG
91
+ struct jpeg_compress_struct oinfo;
92
+ struct jpeg_error_mgr jerr;
93
+ JSAMPROW row_pointer[1];
94
+ FILE *stream;
95
+ int i;
96
+
97
+ /* 出力先設定 */
98
+ if(!this->filename){
99
+ stream = stdout;
100
+ }else{
101
+ if( (stream=fopen(this->filename, "wb")) == NULL ) return(1);
102
+ }
103
+
104
+ oinfo.err = jpeg_std_error(&jerr);
105
+ jpeg_create_compress(&oinfo);
106
+ jpeg_stdio_dest(&oinfo, stream);
107
+
108
+ oinfo.image_width = this->rsize;
109
+ oinfo.image_height = this->rsize;
110
+ oinfo.in_color_space = JCS_GRAYSCALE;
111
+ oinfo.input_components = 1;
112
+ jpeg_set_defaults(&oinfo);
113
+ jpeg_set_quality(&oinfo, 100, TRUE);
114
+ oinfo.dct_method = JDCT_ISLOW;
115
+ oinfo.write_JFIF_header = TRUE;
116
+
117
+ jpeg_start_compress(&oinfo, TRUE);
118
+ for(i=0; i<this->rsize; i++){
119
+ row_pointer[0] = bit_image[i];
120
+ jpeg_write_scanlines(&oinfo, row_pointer, 1);
121
+ }
122
+
123
+ jpeg_finish_compress(&oinfo);
124
+ jpeg_destroy_compress(&oinfo);
125
+
126
+ fclose(stream);
127
+ return(0);
128
+ #else
129
+ return(1);
130
+ #endif
131
+ }
@@ -0,0 +1,29 @@
1
+ #ifndef _QR_DRAW_JPEG_
2
+ #define _QR_DRAW_JPEG_
3
+
4
+ #include <stdlib.h>
5
+ #include <stdio.h>
6
+ #include <string.h>
7
+ #include "qr_draw.h"
8
+
9
+ #ifdef USE_JPG
10
+ #include <jpeglib.h>
11
+ #endif
12
+
13
+ //=============================================================================
14
+ // QRDrawJPEG クラス
15
+ //=============================================================================
16
+ class QRDrawJPEG : public QRDraw
17
+ {
18
+ private:
19
+ int raster(unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE]);
20
+ int write();
21
+
22
+ public:
23
+ QRDrawJPEG();
24
+ ~QRDrawJPEG();
25
+ int draw(char *filename, int modulesize, int symbolsize,
26
+ unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE], void *opt);
27
+ };
28
+
29
+ #endif
@@ -0,0 +1,146 @@
1
+ #include "qr_draw_png.h"
2
+
3
+ //=================================================================================
4
+ // QRDrawPNG::QRDrawPNG
5
+ //=================================================================================
6
+ QRDrawPNG::QRDrawPNG()
7
+ {
8
+ #ifdef USE_PNG
9
+ bit_image = NULL;
10
+ #endif
11
+ }
12
+
13
+ //=================================================================================
14
+ // QRDrawPNG::~QRDrawPNG
15
+ //=================================================================================
16
+ QRDrawPNG::~QRDrawPNG()
17
+ {
18
+ #ifdef USE_PNG
19
+ int i;
20
+
21
+ if(bit_image){
22
+ for(i=0; i<this->rsize; i++){
23
+ free(bit_image[i]);
24
+ }
25
+ free(bit_image);
26
+ }
27
+ #endif
28
+ }
29
+
30
+ //=============================================================================
31
+ // QRDrawPNG::draw
32
+ //=============================================================================
33
+ int QRDrawPNG::draw(char *filename, int modulesize, int symbolsize,
34
+ unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE], void *opt)
35
+ {
36
+ #ifdef USE_PNG
37
+ setup(filename, modulesize, symbolsize);
38
+
39
+ /* ニ値イメージの構築 */
40
+ if( this->raster(data) ) return(1);
41
+
42
+ /* PNG書き出し */
43
+ if( this->write() ) return(1);
44
+
45
+ return(0);
46
+ #else
47
+ return(1);
48
+ #endif
49
+ }
50
+
51
+ //=================================================================================
52
+ // QRDrawPNG::create_image
53
+ //=================================================================================
54
+ int QRDrawPNG::raster(unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE])
55
+ {
56
+ #ifdef USE_PNG
57
+ int bitw = (int)ceil(this->rsize/8) + 1;
58
+
59
+ /* 実際にデータを置く領域を確保 */
60
+ bit_image = (unsigned char **)malloc(sizeof(unsigned char *) * this->rsize);
61
+ for(int i=0; i<this->rsize; i++){
62
+ bit_image[i] = (unsigned char *)malloc(bitw);
63
+ memset(bit_image[i], 0, bitw);
64
+ }
65
+
66
+ for(int i=0; i<this->ssize; i++){
67
+ int dp = MARGIN_SIZE*this->msize / 8; //横方向のバイト位置
68
+ int sht =(MARGIN_SIZE*this->msize % 8) ? 3 : 7; //ビットシフト
69
+ unsigned char c = 0; //1バイトの構成を保存
70
+
71
+ for(int j=0; j<this->ssize; j++){
72
+ /* 1行分生成 */
73
+ for(int k=0; k<this->msize; k++){
74
+ c += (data[j][i] << sht);
75
+ sht--;
76
+
77
+ bit_image[(i+MARGIN_SIZE)*this->msize][ dp ] = c;
78
+
79
+ if(sht < 0){
80
+ sht = 7;
81
+ c = 0;
82
+ dp++;
83
+ }
84
+ }
85
+ }
86
+ /* モジュールサイズ分縦方向に増やす */
87
+ for(int k=1; k<this->msize; k++){
88
+ memcpy(bit_image[(i+MARGIN_SIZE)*this->msize+k], bit_image[(i+MARGIN_SIZE)*this->msize], bitw);
89
+ }
90
+ }
91
+
92
+ return(0);
93
+ #else
94
+ return(1);
95
+ #endif
96
+ }
97
+
98
+ //=================================================================================
99
+ // QRDrawPNG::write
100
+ //=================================================================================
101
+ int QRDrawPNG::write()
102
+ {
103
+ #ifdef USE_PNG
104
+ png_structp png_ptr;
105
+ png_infop info_ptr;
106
+ FILE *stream;
107
+
108
+ /* 出力先設定 */
109
+ if(!this->filename){
110
+ stream = stdout;
111
+ }else{
112
+ if( (stream=fopen(this->filename, "wb")) == NULL ) return(1);
113
+ }
114
+
115
+ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
116
+ info_ptr = png_create_info_struct(png_ptr);
117
+
118
+ png_init_io(png_ptr, stream);
119
+ png_set_filter(png_ptr, 0, PNG_ALL_FILTERS);
120
+ png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
121
+ png_set_invert_mono(png_ptr); //白黒反転
122
+
123
+ /* IHDRチャンク情報を設定 */
124
+ png_set_IHDR(png_ptr, //png_structp
125
+ info_ptr, //png_infop
126
+ this->rsize, //width
127
+ this->rsize, //height
128
+ 1, //bit_depth(ニ値)
129
+ PNG_COLOR_TYPE_GRAY, //Colorタイプ(ニ値)
130
+ PNG_INTERLACE_NONE, //interlace_method
131
+ PNG_COMPRESSION_TYPE_DEFAULT, //compression_method
132
+ PNG_FILTER_TYPE_DEFAULT); //filter_method
133
+
134
+ png_write_info(png_ptr, info_ptr);
135
+ png_write_image(png_ptr, bit_image);
136
+ png_write_end(png_ptr, info_ptr);
137
+
138
+ png_destroy_info_struct(png_ptr, &info_ptr);
139
+ png_destroy_write_struct(&png_ptr, &info_ptr);
140
+
141
+ fclose(stream);
142
+ return(0);
143
+ #else
144
+ return(1);
145
+ #endif
146
+ }