rqr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,28 @@
1
+ #ifndef _QR_DRAW_PNG_
2
+ #define _QR_DRAW_PNG_
3
+
4
+ #include <stdlib.h>
5
+ #include <math.h>
6
+ #include "qr_draw.h"
7
+
8
+ #ifdef USE_PNG
9
+ #include <png.h>
10
+ #endif
11
+
12
+ //=============================================================================
13
+ // QRDrawPNG クラス
14
+ //=============================================================================
15
+ class QRDrawPNG : public QRDraw
16
+ {
17
+ private:
18
+ int raster(unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE]);
19
+ int write();
20
+
21
+ public:
22
+ QRDrawPNG();
23
+ ~QRDrawPNG();
24
+ int draw(char *filename, int modulesize, int symbolsize,
25
+ unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE], void *opt);
26
+ };
27
+
28
+ #endif
@@ -0,0 +1,240 @@
1
+ #include "qr_draw_ps.h"
2
+
3
+ //=================================================================================
4
+ // QRDrawPS::QRDrawPS
5
+ //=================================================================================
6
+ QRDrawPS::QRDrawPS()
7
+ {
8
+ }
9
+
10
+ //=============================================================================
11
+ // QRDrawPS::draw
12
+ //=============================================================================
13
+ int QRDrawPS::draw(char *filename, int modulesize, int symbolsize,
14
+ unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE], void *opt)
15
+ {
16
+ if(!filename) return(1);
17
+ setup(filename, modulesize, symbolsize);
18
+
19
+ bool preview;
20
+ if(!opt){
21
+ preview = false;
22
+ }else{
23
+ preview = (bool)opt;
24
+ }
25
+
26
+ if(preview){
27
+ #ifdef USE_TIFF
28
+ return( this->write2(data) );
29
+ #endif
30
+ }else{
31
+ return( this->write(data, "wb") );
32
+ }
33
+
34
+ return(1);
35
+ }
36
+
37
+ //=============================================================================
38
+ // QRDrawPS::write_preview_eps
39
+ //=============================================================================
40
+ int QRDrawPS::write2(unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE])
41
+ {
42
+ #ifdef USE_TIFF
43
+ FILE *stream;
44
+ unsigned char lt[4];
45
+ struct stat st;
46
+
47
+ if((stream=fopen(this->filename, "wb")) == NULL) return(1);
48
+
49
+ /* ダミーヘッダー書き込み */
50
+ this->write_header(stream);
51
+
52
+ /* TIFF書き込み */
53
+ this->write_preview(stream, data);
54
+ fclose(stream);
55
+
56
+ /* EPS書き込み */
57
+ this->write(data, "a");
58
+
59
+ /* EPSサイズ算出 */
60
+ stat(this->filename, &st);
61
+ unsigned int esize = st.st_size - (this->tsize+30);
62
+
63
+ /* ヘッダー書き換え */
64
+ if((stream=fopen(this->filename, "rb+")) == NULL) return(1);
65
+
66
+ /* PS開始位置 */
67
+ fseek(stream, 4, SEEK_SET);
68
+ this->littleEndian(30+this->tsize, lt);
69
+ fprintf(stream, "%c%c%c%c", lt[0], lt[1], lt[2], lt[3]);
70
+
71
+ /* PS長 */
72
+ fseek(stream, 8, SEEK_SET);
73
+ this->littleEndian(esize, lt);
74
+ fprintf(stream, "%c%c%c%c", lt[0], lt[1], lt[2], lt[3]);
75
+
76
+ /* TIFFプレビュー長 */
77
+ fseek(stream, 24, SEEK_SET);
78
+ this->littleEndian(this->tsize, lt);
79
+ fprintf(stream, "%c%c%c%c", lt[0], lt[1], lt[2], lt[3]);
80
+
81
+ fclose(stream);
82
+
83
+ return(0);
84
+ #else
85
+ return(1);
86
+ #endif
87
+ }
88
+
89
+ //=============================================================================
90
+ // QRDrawPS::write_header
91
+ //=============================================================================
92
+ void QRDrawPS::write_header(FILE *stream)
93
+ {
94
+ unsigned char lt[4];
95
+
96
+ /* ナミモニ */
97
+ fprintf(stream, "%c%c%c%c", 0xC5, 0xD0, 0xD3, 0xC6);
98
+
99
+ /* PostScript開始位置 */
100
+ fprintf(stream, "%c%c%c%c", 0x00, 0x00, 0x00, 0x00);
101
+
102
+ /* PostScript長さ */
103
+ fprintf(stream, "%c%c%c%c", 0x00, 0x00, 0x00, 0x00);
104
+
105
+ /* MetaFile */
106
+ fprintf(stream, "%c%c%c%c", 0x00, 0x00, 0x00, 0x00);
107
+ fprintf(stream, "%c%c%c%c", 0x00, 0x00, 0x00, 0x00);
108
+
109
+ /* TIFFプレビュー開始位置 */
110
+ littleEndian(30, lt);
111
+ fprintf(stream, "%c%c%c%c", lt[0], lt[1], lt[2], lt[3]);
112
+
113
+ /* TIFFプレビュー長 */
114
+ fprintf(stream, "%c%c%c%c", 0x00, 0x00, 0x00, 0x00);
115
+
116
+ /* チェックサム */
117
+ fprintf(stream, "%c%c", 0xff, 0xff);
118
+ }
119
+
120
+ //=================================================================================
121
+ // littleEndian
122
+ //=================================================================================
123
+ void QRDrawPS::littleEndian(int data, unsigned char *lt)
124
+ {
125
+ lt[0] = data & 0x000000FF;
126
+ lt[1] = (data & 0x0000FF00) >> 8;
127
+ lt[2] = (data & 0x00FF0000) >> 16;
128
+ lt[3] = (data & 0xFF000000) >> 24;
129
+ }
130
+
131
+ //=============================================================================
132
+ // QRDrawPS::write_preview
133
+ //=============================================================================
134
+ int QRDrawPS::write_preview(FILE *stream, unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE])
135
+ {
136
+ #ifdef USE_TIFF
137
+ FILE *t;
138
+ char tmp[L_tmpnam];
139
+ QRDrawTIFF tif;
140
+ unsigned char *p;
141
+
142
+ tmpnam(tmp);
143
+ if( tif.draw(tmp, this->msize, this->ssize, data, NULL) ) return(1);
144
+
145
+ /* TIFFをオープンしてファイルサイズを取得する */
146
+ if((t=fopen(tmp, "rb")) == NULL){
147
+ remove(tmp);
148
+ return(1);
149
+ }
150
+ fseek(t, 0, SEEK_END);
151
+ this->tsize = ftell(t);
152
+ fseek(t, 0, SEEK_SET);
153
+
154
+ /* TIFF本体の書き込み */
155
+ p = (unsigned char *)malloc(this->tsize);
156
+ fread(p, 1, this->tsize, t);
157
+ fwrite(p, 1, this->tsize, stream);
158
+
159
+ free(p);
160
+ fclose(t);
161
+ remove(tmp);
162
+
163
+ return(0);
164
+ #else
165
+ return(1);
166
+ #endif
167
+ }
168
+
169
+ //=============================================================================
170
+ // QRDrawPS::write_eps
171
+ //=============================================================================
172
+ int QRDrawPS::write(unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE], char *mode)
173
+ {
174
+ FILE *stream;
175
+ int i, j;
176
+ time_t mytime;
177
+ struct tm *lt;
178
+
179
+ if((stream=fopen(this->filename, mode)) == NULL) return(1);
180
+
181
+ fprintf(stream, "%%!PS-Adobe-3.0 EPSF-3.0\n");
182
+ fprintf(stream, "%%%%Creator: kei-soe\n");
183
+ fprintf(stream, "%%%%Title: %s\n", this->filename);
184
+
185
+ time(&mytime);
186
+ lt = localtime(&mytime);
187
+ fprintf(stream, "%%%%CreationDate: %04d:%02d:%02d %02d:%02d:%02d\n",
188
+ lt->tm_year+1900, lt->tm_mon+1, lt->tm_mday,
189
+ lt->tm_hour, lt->tm_min, lt->tm_sec);
190
+ fprintf(stream, "%%%%BoundingBox: 0 0 %d %d\n", this->rsize, this->rsize);
191
+ fprintf(stream, "%%%%HiResBoundingBox: 0 0 %d %d\n", this->rsize, this->rsize);
192
+ fprintf(stream, "%%%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
193
+ fprintf(stream, "%%%%EndComments\n");
194
+ fprintf(stream, "%%%%BeginProlog\n");
195
+ fprintf(stream, "%%%%EndProlog\n");
196
+ fprintf(stream, "%%%%BeginSetup\n");
197
+ fprintf(stream, "%%%%EndSetup\n");
198
+
199
+ fprintf(stream, "/size %d def\n", this->ssize);
200
+ fprintf(stream, "/margin %d def\n", MARGIN_SIZE);
201
+ fprintf(stream, "gsave\n");
202
+ fprintf(stream, "%d %d scale\n", this->msize, this->msize);
203
+ fprintf(stream, "newpath\n");
204
+ fprintf(stream, "0 0 moveto\n");
205
+ fprintf(stream, "0 size margin 2 mul add rlineto\n");
206
+ fprintf(stream, "size margin 2 mul add 0 rlineto\n");
207
+ fprintf(stream, "0 size margin 2 mul add neg rlineto\n");
208
+ fprintf(stream, "closepath\n");
209
+ fprintf(stream, "1 setgray\n");
210
+ fprintf(stream, "fill\n");
211
+
212
+ // ドット描画
213
+ for(i=0; i<this->ssize; i++){
214
+ for(j=0; j<this->ssize; j++) fprintf(stream, "%d ", data[j][i]);
215
+ fprintf(stream, "\n");
216
+ }
217
+
218
+ fprintf(stream, "margin margin translate\n");
219
+ fprintf(stream, "0 setgray\n");
220
+ fprintf(stream, "1 1 size {\n");
221
+ fprintf(stream, "/ypos exch def\n");
222
+ fprintf(stream, "size -1 1 {\n");
223
+ fprintf(stream, "/xpos exch def\n");
224
+ fprintf(stream, "1 eq {\n");
225
+ fprintf(stream, "newpath\n");
226
+ fprintf(stream, "xpos ypos moveto\n");
227
+ fprintf(stream, "0 -1 rlineto\n");
228
+ fprintf(stream, "-1 0 rlineto\n");
229
+ fprintf(stream, "0 1 rlineto\n");
230
+ fprintf(stream, "closepath\n");
231
+ fprintf(stream, "fill\n");
232
+ fprintf(stream, "} if\n");
233
+ fprintf(stream, "} for\n");
234
+ fprintf(stream, "} for\n");
235
+ fprintf(stream, "grestore\n");
236
+
237
+ fclose(stream);
238
+
239
+ return(0);
240
+ }
@@ -0,0 +1,36 @@
1
+ #ifndef _QR_DRAW_PS_
2
+ #define _QR_DRAW_PS_
3
+
4
+ #include <time.h>
5
+ #include <sys/types.h>
6
+ #include <sys/stat.h>
7
+ #include <stdlib.h>
8
+ #include <stdio.h>
9
+ #include "qr_draw.h"
10
+
11
+ #ifdef USE_TIFF
12
+ #include "qr_draw_tiff.h"
13
+ #endif
14
+
15
+ //=============================================================================
16
+ // QRDrawPS クラス
17
+ //=============================================================================
18
+ class QRDrawPS : public QRDraw
19
+ {
20
+ private:
21
+ unsigned int tsize;
22
+
23
+ private:
24
+ int write(unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE], char *mode); //normal ps
25
+ int write2(unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE]); //preview ps
26
+ void write_header(FILE *stream);
27
+ int write_preview(FILE *stream, unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE]);
28
+ void littleEndian(int data, unsigned char *lt);
29
+
30
+ public:
31
+ QRDrawPS();
32
+ int draw(char *filename, int modulesize, int symbolsize,
33
+ unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE], void *opt);
34
+ };
35
+
36
+ #endif
@@ -0,0 +1,135 @@
1
+ #include "qr_draw_tiff.h"
2
+
3
+ //=================================================================================
4
+ // QRDrawPNG::QRDrawPNG
5
+ //=================================================================================
6
+ QRDrawTIFF::QRDrawTIFF()
7
+ {
8
+ #ifdef USE_TIFF
9
+ bit_image = NULL;
10
+ #endif
11
+ }
12
+
13
+ //=================================================================================
14
+ // QRDrawTIFF::~QRDrawTIFF
15
+ //=================================================================================
16
+ QRDrawTIFF::~QRDrawTIFF()
17
+ {
18
+ #ifdef USE_TIFF
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 QRDrawTIFF::draw(char *filename, int modulesize, int symbolsize,
34
+ unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE], void *opt)
35
+ {
36
+ #ifdef USE_TIFF
37
+ if(!filename) return(1);
38
+ setup(filename, modulesize, symbolsize);
39
+ if( this->raster(data) )return(1);
40
+ if( this->write() ) return(1);
41
+
42
+ return(0);
43
+ #else
44
+ return(1);
45
+ #endif
46
+ }
47
+
48
+ //=================================================================================
49
+ // QRDrawTIFF::raster
50
+ //=================================================================================
51
+ int QRDrawTIFF::raster(unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE])
52
+ {
53
+ #ifdef USE_TIFF
54
+ int bitw = (int)ceil(this->rsize/8) + 1;
55
+
56
+ /* 実際にデータを置く領域を確保 */
57
+ bit_image = (unsigned char **)malloc(sizeof(unsigned char *) * this->rsize);
58
+ for(int i=0; i<this->rsize; i++){
59
+ bit_image[i] = (unsigned char *)malloc(bitw);
60
+ memset(bit_image[i], 0, bitw);
61
+ }
62
+
63
+ for(int i=0; i<this->ssize; i++){
64
+ int dp = MARGIN_SIZE*this->msize / 8; //横方向のバイト位置
65
+ int sht =(MARGIN_SIZE*this->msize % 8) ? 3 : 7; //ビットシフト
66
+ unsigned char c = 0; //1バイトの構成を保存
67
+
68
+ for(int j=0; j<this->ssize; j++){
69
+ /* 1行分生成 */
70
+ for(int k=0; k<this->msize; k++){
71
+ c += (data[j][i] << sht);
72
+ sht--;
73
+
74
+ bit_image[(i+MARGIN_SIZE)*this->msize][ dp ] = c;
75
+
76
+ if(sht < 0){
77
+ sht = 7;
78
+ c = 0;
79
+ dp++;
80
+ }
81
+ }
82
+ }
83
+ /* モジュールサイズ分縦方向に増やす */
84
+ for(int k=1; k<this->msize; k++){
85
+ memcpy(bit_image[(i+MARGIN_SIZE)*this->msize+k], bit_image[(i+MARGIN_SIZE)*this->msize], bitw);
86
+ }
87
+ }
88
+
89
+ return(0);
90
+ #else
91
+ return(1);
92
+ #endif
93
+ }
94
+
95
+ //=================================================================================
96
+ // QRDrawTIFF::write_png
97
+ //=================================================================================
98
+ int QRDrawTIFF::write()
99
+ {
100
+ #ifdef USE_TIFF
101
+ TIFF *tiff;
102
+ int i;
103
+
104
+ /* Open the TIFF file */
105
+ if( (tiff=TIFFOpen(this->filename, "w")) == NULL ) return(1);
106
+
107
+ /* タグ */
108
+ TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, this->rsize); /* 幅(ピクセル数) */
109
+ TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, this->rsize); /* 高(スキャンライン数) */
110
+ TIFFSetField(tiff, TIFFTAG_COMPRESSION, COMPRESSION_NONE); /* 圧縮モード */
111
+ TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 1); /* ピクセルの深さ */
112
+ TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE); /* カラータイプ */
113
+ TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, 1); /* カラープレーン数 */
114
+ TIFFSetField(tiff, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); /* スキャン方向 */
115
+ TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); /* ? */
116
+ TIFFSetField(tiff, TIFFTAG_XRESOLUTION, 72.0); /* 解像度 */
117
+ TIFFSetField(tiff, TIFFTAG_YRESOLUTION, 72.0); /* 解像度 */
118
+ TIFFSetField(tiff, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH); /* 解像度の単位(RESUNIT_INCH:インチ) */
119
+ TIFFSetField(tiff, TIFFTAG_ROWSPERSTRIP, this->rsize); /* 1ストリップに何行格納されるか */
120
+
121
+ /* 1行ずつ書き込み */
122
+ for(i=0 ; i<this->rsize ; i++){
123
+ if( TIFFWriteScanline(tiff, bit_image[i], i, 0) < 0 ){
124
+ TIFFClose(tiff);
125
+ return(1);
126
+ }
127
+ }
128
+
129
+ TIFFClose(tiff);
130
+
131
+ return(0);
132
+ #else
133
+ return(1);
134
+ #endif
135
+ }
@@ -0,0 +1,29 @@
1
+ #ifndef _QR_DRAW_TIFF_
2
+ #define _QR_DRAW_TIFF_
3
+
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+ #include <math.h>
7
+ #include "qr_draw.h"
8
+
9
+ #ifdef USE_TIFF
10
+ #include <tiffio.h>
11
+ #endif
12
+
13
+ //=============================================================================
14
+ // QRDrawTIFF クラス
15
+ //=============================================================================
16
+ class QRDrawTIFF : public QRDraw
17
+ {
18
+ private:
19
+ int raster(unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE]);
20
+ int write();
21
+
22
+ public:
23
+ QRDrawTIFF();
24
+ ~QRDrawTIFF();
25
+ int draw(char *filename, int modulesize, int symbolsize,
26
+ unsigned char data[MAX_MODULESIZE][MAX_MODULESIZE], void *opt);
27
+ };
28
+
29
+ #endif