OSDN Git Service

* changed feature.
[modchxj/mod_chxj.git] / src / chxj_qr_code.c
1 /*
2  * Copyright (C) 2005-2011 Atsushi Konno All rights reserved.
3  * Copyright (C) 2005 QSDN,Inc. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <unistd.h>
18 #include <assert.h>
19 #include "apr_strings.h"
20 #include "httpd.h"
21 #include "http_protocol.h"
22 #include "http_log.h"
23
24 #include "chxj_str_util.h"
25 #include "chxj_qr_code.h"
26 #include "chxj_calc_ecc.h"
27 #include "mod_chxj.h"
28 #include "chxj_specified_device.h"
29 #include "qs_malloc.h"
30 #include "qs_log.h"
31 #include "qs_parse_file.h"
32
33 #include <wand/magick_wand.h>
34
35 /*
36 #define QR_CODE_DEBUG
37 */
38 #define EXIT_MAGICK_ERROR() \
39           do { \
40              char *description; \
41              ExceptionType severity; \
42              description=MagickGetException(magick_wand,&severity); \
43              ap_log_rerror(APLOG_MARK,APLOG_DEBUG, 0, r,"%s %s %d %s\n",__FILE__,(__func__),__LINE__,description); \
44              description=(char *) MagickRelinquishMemory(description); \
45              DestroyMagickWand(magick_wand); \
46           }while(0)
47
48 /*----------------------------------------------------------------------------*/
49 /* ¥â¡¼¥É»Ø¼¨»Ò¥Æ¡¼¥Ö¥ë                                                       */
50 /*----------------------------------------------------------------------------*/
51 static char* v_mode_table[] = {
52 /* ¿ô»ú          */  "0001",
53 /* ±Ñ¿ô»ú        */  "0010",
54 /* 8¥Ó¥Ã¥È¥Ð¥¤¥È */  "0100",
55 /* ´Á»ú          */  "1000",
56 /* ECI           */  "0111",
57 /* FNC1 1        */  "0101",
58 /* FNC1 2        */  "1001",
59 /* ¹½Â¤ÅªÏ¢ÀÜ    */  "0011",
60 /* ½ªÃ¼¥Ñ¥¿¡¼¥ó  */  "0000",
61 /* Í½Ìó          */  "0110",
62 /* Í½Ìó          */  "1010",
63 /* Í½Ìó          */  "1011",
64 /* Í½Ìó          */  "1100",
65 /* Í½Ìó          */  "1101",
66 /* Í½Ìó          */  "1110",
67 /* Í½Ìó          */  "1111",
68 };
69
70 /*----------------------------------------------------------------------------*/
71 /* Ê¸»ú¿ô»Ø¼¨»Ò¤Î¥Ó¥Ã¥È¿ô                                                     */
72 /*----------------------------------------------------------------------------*/
73 static char v_char_count_table[][4] = {
74 /* ¥Ð¡¼¥¸¥ç¥ó *//* Ê¸»ú¿ô»Ø¼¨»Ò¤Î¥Ó¥Ã¥È¿ô */
75 /*  1         */  { 10, 9, 8, 8, },
76 /*  2         */  { 10, 9, 8, 8, },
77 /*  3         */  { 10, 9, 8, 8, },
78 /*  4         */  { 10, 9, 8, 8, },
79 /*  5         */  { 10, 9, 8, 8, },
80 /*  6         */  { 10, 9, 8, 8, },
81 /*  7         */  { 10, 9, 8, 8, },
82 /*  8         */  { 10, 9, 8, 8, },
83 /*  9         */  { 10, 9, 8, 8, },
84 /* 10         */  { 12,11,16,10, },
85 /* 11         */  { 12,11,16,10, },
86 /* 12         */  { 12,11,16,10, },
87 /* 13         */  { 12,11,16,10, },
88 /* 14         */  { 12,11,16,10, },
89 /* 15         */  { 12,11,16,10, },
90 /* 16         */  { 12,11,16,10, },
91 /* 17         */  { 12,11,16,10, },
92 /* 18         */  { 12,11,16,10, },
93 /* 19         */  { 12,11,16,10, },
94 /* 20         */  { 12,11,16,10, },
95 /* 21         */  { 12,11,16,10, },
96 /* 22         */  { 12,11,16,10, },
97 /* 23         */  { 12,11,16,10, },
98 /* 24         */  { 12,11,16,10, },
99 /* 25         */  { 12,11,16,10, },
100 /* 26         */  { 12,11,16,10, },
101 /* 27         */  { 14,13,16,12, },
102 /* 28         */  { 14,13,16,12, },
103 /* 29         */  { 14,13,16,12, },
104 /* 30         */  { 14,13,16,12, },
105 /* 31         */  { 14,13,16,12, },
106 /* 32         */  { 14,13,16,12, },
107 /* 33         */  { 14,13,16,12, },
108 /* 34         */  { 14,13,16,12, },
109 /* 35         */  { 14,13,16,12, },
110 /* 36         */  { 14,13,16,12, },
111 /* 37         */  { 14,13,16,12, },
112 /* 38         */  { 14,13,16,12, },
113 /* 39         */  { 14,13,16,12, },
114 /* 40         */  { 14,13,16,12, },
115 };
116 static int v_data_code_count[][4] = {
117 /* ¥Ð¡¼¥¸¥ç¥ó *//* ¥Ç¡¼¥¿¥³¡¼¥É¿ô */
118                 /*  L,    M,   Q,   H, */
119 /* 1          */  {19,   16,  13,   9,},
120 /* 2          */  {34,   28,  22,  16,},
121 /* 3          */  {55,   44,  34,  26,},
122 /* 4          */  {80,   64,  48,  36,},
123 /* 5          */  {108,  86,  62,  46,},
124 /* 6          */  {136, 108,  76,  60,},
125 /* 7          */  {156, 124,  88,  66,},
126 /* 8          */  {194, 154, 110,  86,},
127 /* 9          */  {232, 182, 132, 100,},
128 /* 10         */  {274, 216, 154, 122,},
129 /* 11         */  {324, 254, 180, 140,},
130 /* 12         */  {370, 290, 206, 158,},
131 /* 13         */  {428, 334, 244, 180,},
132 /* 14         */  {461, 365, 261, 197,},
133 /* 15         */  {523, 415, 295, 223,},
134 /* 16         */  {589, 453, 325, 253,},
135 /* 17         */  {647, 507, 367, 283,},
136 /* 18         */  {721, 563, 397, 313,},
137 /* 19         */  {795, 627, 445, 341,},
138 /* 20         */  {861, 669, 485, 385,},
139 /* 21         */  {932, 714, 512, 406,},
140 /* 22         */  {1006,782, 568, 442,},
141 /* 23         */  {1094,860, 614, 464,},
142 /* 24         */  {1174,914, 664, 514,},
143 /* 25         */  {1276,1000,718, 538,},
144 /* 26         */  {1370,1062,754, 596,},
145 /* 27         */  {1468,1128,808, 628,},
146 /* 28         */  {1531,1193,871, 661,},
147 /* 29         */  {1631,1267,911, 701,},
148 /* 30         */  {1735,1373,985, 745,},
149 /* 31         */  {1843,1455,1033,793,},
150 /* 32         */  {1955,1541,1115,845,},
151 /* 33         */  {2071,1631,1171,901,},
152 /* 34         */  {2191,1725,1231,961,},
153 /* 35         */  {2306,1812,1286,986,},
154 /* 36         */  {2434,1914,1354,1054,},
155 /* 37         */  {2566,1992,1426,1096,},
156 /* 38         */  {2702,2102,1502,1142,},
157 /* 39         */  {2812,2216,1582,1222,},
158 /* 40         */  {2956,2334,1666,1276,},
159 };
160
161 /*----------------------------------------------------------------------------*/
162 /* ¤¢¤Þ¤Ã¤¿¾ì¹ç¤ÎÉղåӥåȠ                                                  */
163 /*----------------------------------------------------------------------------*/
164 static char* v_pend_bit[]  = {
165   "11101100",
166   "00010001",
167 };
168
169 /*----------------------------------------------------------------------------*/
170 /* ¥â¥¸¥å¡¼¥ë¿ô                                                               */
171 /*----------------------------------------------------------------------------*/
172 static int v_module_count_table[] = {
173   21, 25, 29, 33, 37, 41, 45, 49,
174   53, 57, 61, 65, 69, 73, 77, 81,
175   85, 89, 93, 97, 101, 105, 109, 113,
176   117, 121, 125, 129, 133, 137, 141, 145,
177   149, 153, 157, 161, 165, 169, 173, 177,
178 };
179 /*----------------------------------------------------------------------------*/
180 /* °ÌÃÖ¤¢¤ï¤»¥Ñ¥¿¡¼¥ó¤Î°ÌÃÖ                                                   */
181 /*----------------------------------------------------------------------------*/
182 typedef struct _qr_position_pattern_t {
183   int count;
184   int position[7];
185 } qr_position_pattern_t;
186
187 static qr_position_pattern_t v_position_adjust_table[] = {
188   { 0, {0,0,0,0,0,0,0,},},
189   { 1, {6,18,0,0,0,0,0,},},
190   { 1, {6,22,0,0,0,0,0,},},
191   { 1, {6,26,0,0,0,0,0,},},
192   { 1, {6,30,0,0,0,0,0,},},
193   { 1, {6,34,0,0,0,0,0,},},
194   { 6, {6,22,38,0,0,0,0,},},
195   { 6, {6,24,42,0,0,0,0,},},
196   { 6, {6,26,46,0,0,0,0,},},
197   { 6, {6,28,50,0,0,0,0,},},
198   { 6, {6,30,54,0,0,0,0,},},
199   { 6, {6,32,58,0,0,0,0,},},
200   { 6, {6,34,62,0,0,0,0,},},
201   {13, {6,26,46,66,0,0,0,},},
202   {13, {6,26,48,70,0,0,0,},},
203   {13, {6,26,50,74,0,0,0,},},
204   {13, {6,30,54,78,0,0,0,},},
205   {13, {6,30,56,82,0,0,0,},},
206   {13, {6,30,58,86,0,0,0,},},
207   {13, {6,34,62,90,0,0,0,},},
208   {22, {6,28,50,72,94,0,0,},},
209   {22, {6,26,50,74,98,0,0,},},
210   {22, {6,30,54,78,102,0,0,},},
211   {22, {6,28,54,80,106,0,0,},},
212   {22, {6,32,58,84,110,0,0,},},
213   {22, {6,30,58,86,114,0,0,},},
214   {22, {6,34,62,90,118,0,0,},},
215   {33, {6,26,50,74,98,122,0,},},
216   {33, {6,30,54,78,102,126,0,},},
217   {33, {6,26,52,78,104,130,0,},},
218   {33, {6,30,56,82,108,134,0,},},
219   {33, {6,34,60,86,112,138,0,},},
220   {33, {6,30,58,86,114,142,0,},},
221   {33, {6,34,62,90,118,146,0,},},
222   {46, {6,30,54,78,102,126,150,},},
223   {46, {6,24,50,76,102,128,154,},},
224   {46, {6,28,54,80,106,132,158,},},
225   {46, {6,32,58,84,110,136,162,},},
226   {46, {6,26,54,82,110,138,166,},},
227   {46, {6,30,58,86,114,142,170,},},
228 };
229
230 /*----------------------------------------------------------------------------*/
231 /* ¥Þ¥¹¥¯ÄêµÁ                                                                 */
232 /*----------------------------------------------------------------------------*/
233 typedef enum _qr_mask_pattern_t {
234   QR_MASK_1 = 0,
235   QR_MASK_2,
236   QR_MASK_3,
237   QR_MASK_4,
238   QR_MASK_5,
239   QR_MASK_6,
240   QR_MASK_7,
241   QR_MASK_8,
242
243 qr_mask_pattern_t;
244
245 /*----------------------------------------------------------------------------*/
246 /* Çò¤¤²èÁü(PNG)                                                              */
247 /*----------------------------------------------------------------------------*/
248 static unsigned char v_white_base_pic[] = {
249   0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
250   0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
251   0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x18,
252   0x08, 0x02, 0x00, 0x00, 0x00, 0x6f, 0x15, 0xaa,
253   0xaf, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59,
254   0x73, 0x00, 0x00, 0x0b, 0x12, 0x00, 0x00, 0x0b,
255   0x12, 0x01, 0xd2, 0xdd, 0x7e, 0xfc, 0x00, 0x00,
256   0x00, 0x07, 0x74, 0x49, 0x4d, 0x45, 0x07, 0xd5,
257   0x04, 0x04, 0x17, 0x13, 0x07, 0x12, 0x27, 0x77,
258   0x6f, 0x00, 0x00, 0x00, 0x22, 0x49, 0x44, 0x41,
259   0x54, 0x78, 0x9c, 0x63, 0xfc, 0xff, 0xff, 0x3f,
260   0x03, 0x35, 0x00, 0x13, 0x55, 0x4c, 0x19, 0x35, 
261   0x68, 0xd4, 0xa0, 0x51, 0x83, 0x46, 0x0d, 0x1a, 
262   0x35, 0x68, 0xd4, 0x20, 0x8a, 0x00, 0x00, 0x19,
263   0xd1, 0x03, 0x2d, 0xc6, 0xa1, 0x61, 0x7f, 0x00, 
264   0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae,
265   0x42, 0x60, 0x82, 0x0a
266 };
267 /*----------------------------------------------------------------------------*/
268 /* ¹õ¤¤²èÁü(PNG)                                                              */
269 /*----------------------------------------------------------------------------*/
270 static unsigned char v_black_pixel_pic[] = {
271   0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
272   0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
273   0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
274   0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4,
275   0x89, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47,
276   0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00,
277   0x00, 0x04, 0x67, 0x41, 0x4d, 0x41, 0x00, 0x00,
278   0xb1, 0x8f, 0x0b, 0xfc, 0x61, 0x05, 0x00, 0x00,
279   0x00, 0x20, 0x63, 0x48, 0x52, 0x4d, 0x00, 0x00,
280   0x7a, 0x26, 0x00, 0x00, 0x80, 0x84, 0x00, 0x00,
281   0xfa, 0x00, 0x00, 0x00, 0x80, 0xe8, 0x00, 0x00,
282   0x75, 0x30, 0x00, 0x00, 0xea, 0x60, 0x00, 0x00,
283   0x3a, 0x98, 0x00, 0x00, 0x17, 0x70, 0x9c, 0xba,
284   0x51, 0x3c, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x44,
285   0x41, 0x54, 0x18, 0x57, 0x63, 0x60, 0x60, 0x60,
286   0xf8, 0x0f, 0x00, 0x01, 0x04, 0x01, 0x00, 0x70,
287   0x20, 0x65, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x49,
288   0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, 
289 };
290 /*----------------------------------------------------------------------------*/
291 /* ·¿ÈÖ¾ðÊó                                                                   */
292 /*----------------------------------------------------------------------------*/
293 typedef struct _qr_version_info_t {
294   qr_ver_t version;
295   char* bits;
296 } qr_version_info_t;
297
298 static qr_version_info_t v_version_info_table[] = {
299   {QR_VER_1, NULL,},
300   {QR_VER_2, NULL,},
301   {QR_VER_3, NULL,},
302   {QR_VER_4, NULL,},
303   {QR_VER_5, NULL,},
304   {QR_VER_6, NULL,},
305   {QR_VER_7, "000111110010010100",},
306   {QR_VER_8, "001000010110111100",},
307   {QR_VER_9, "001001101010011001",},
308   {QR_VER_10,"001010010011010011",},
309   {QR_VER_11,"001011101111110110",},
310   {QR_VER_12,"001100011101100010",},
311   {QR_VER_13,"001101100001000111",},
312   {QR_VER_14,"001110011000001101",},
313   {QR_VER_15,"001111100100101000",},
314   {QR_VER_16,"010000101101111000",},
315   {QR_VER_17,"010001010001011101",},
316   {QR_VER_18,"010010101000010111",},
317   {QR_VER_19,"010011010100110010",},
318   {QR_VER_20,"010100100110100110",},
319   {QR_VER_21,"010101011010000011",},
320   {QR_VER_22,"010110100011001001",},
321   {QR_VER_23,"010111011111101100",},
322   {QR_VER_24,"011000111011000100",},
323   {QR_VER_25,"011001000111100001",},
324   {QR_VER_26,"011010111110101011",},
325   {QR_VER_27,"011011000010001110",},
326   {QR_VER_28,"011100110000011010",},
327   {QR_VER_29,"011101001100111111",},
328   {QR_VER_30,"011110110101110101",},
329   {QR_VER_31,"011111001001010000",},
330   {QR_VER_32,"100000100111010101",},
331   {QR_VER_33,"100001011011110000",},
332   {QR_VER_34,"100010100010111010",},
333   {QR_VER_35,"100011011110011111",},
334   {QR_VER_36,"100100101100001011",},
335   {QR_VER_37,"100101010000101110",},
336   {QR_VER_38,"100110101001100100",},
337   {QR_VER_39,"100111010101000001",},
338   {QR_VER_40,"101000110001101001",},
339 };
340
341 /**
342  * ¥Ç¡¼¥¿ÍÆÎ̤Υơ¼¥Ö¥ë
343  */
344 /*----------------------------------------------------------------------------*/
345 /* ¥Ð¡¼¥¸¥ç¥óÊÌ¡¢¥ì¥Ù¥ëÊÌ¡¢¥â¡¼¥ÉÊ̥ǡ¼¥¿ÍÆÎÌ                                 */
346 /*----------------------------------------------------------------------------*/
347 typedef struct _qr_capacity_t {
348   qr_ver_t    version; /* ¾éĹ¾ðÊó */
349   qr_level_t  level;   /* ¾éĹ¾ðÊó */
350   int size[4];
351 } qr_capacity_t;
352
353 static qr_capacity_t v_capacity_table[] = {
354   {QR_VER_1, QR_LEVEL_L, {  41,  25,  17,  10, },},
355   {QR_VER_1, QR_LEVEL_M, {  34,  20,  14,   8, },},
356   {QR_VER_1, QR_LEVEL_Q, {  27,  16,  11,   7, },},
357   {QR_VER_1, QR_LEVEL_H, {  17,  10,   7,   4, },},
358
359   {QR_VER_2, QR_LEVEL_L, {  77,  47,  32,  20, },},
360   {QR_VER_2, QR_LEVEL_M, {  63,  38,  26,  16, },},
361   {QR_VER_2, QR_LEVEL_Q, {  48,  29,  20,  12, },},
362   {QR_VER_2, QR_LEVEL_H, {  34,  20,  14,   8, },},
363
364   {QR_VER_3, QR_LEVEL_L, { 127,  77,  53,  32, },},
365   {QR_VER_3, QR_LEVEL_M, { 101,  61,  42,  26, },},
366   {QR_VER_3, QR_LEVEL_Q, {  77,  47,  32,  20, },},
367   {QR_VER_3, QR_LEVEL_H, {  58,  35,  24,  15, },},
368
369   {QR_VER_4, QR_LEVEL_L, { 187, 114,  78,  48, },},
370   {QR_VER_4, QR_LEVEL_M, { 149,  90,  62,  38, },},
371   {QR_VER_4, QR_LEVEL_Q, { 111,  67,  46,  28, },},
372   {QR_VER_4, QR_LEVEL_H, {  82,  50,  34,  21, },},
373
374   {QR_VER_5, QR_LEVEL_L, { 255, 154, 106,  65, },},
375   {QR_VER_5, QR_LEVEL_M, { 202, 122,  84,  52, },},
376   {QR_VER_5, QR_LEVEL_Q, { 144,  87,  60,  37, },},
377   {QR_VER_5, QR_LEVEL_H, { 106,  64,  44,  27, },},
378
379   {QR_VER_6, QR_LEVEL_L, { 322, 195, 134,  82, },},
380   {QR_VER_6, QR_LEVEL_M, { 255, 154, 106,  65, },},
381   {QR_VER_6, QR_LEVEL_Q, { 178, 108,  74,  45, },},
382   {QR_VER_6, QR_LEVEL_H, { 139,  84,  58,  36, },},
383
384   {QR_VER_7, QR_LEVEL_L, { 370, 224, 154,  95, },},
385   {QR_VER_7, QR_LEVEL_M, { 293, 178, 122,  75, },},
386   {QR_VER_7, QR_LEVEL_Q, { 207, 125,  86,  53, },},
387   {QR_VER_7, QR_LEVEL_H, { 154,  93,  64,  39, },},
388
389   {QR_VER_8, QR_LEVEL_L, { 461, 279, 192, 118, },},
390   {QR_VER_8, QR_LEVEL_M, { 365, 221, 152,  93, },},
391   {QR_VER_8, QR_LEVEL_Q, { 259, 157, 108,  66, },},
392   {QR_VER_8, QR_LEVEL_H, { 202, 122,  84,  52, },},
393
394   {QR_VER_9, QR_LEVEL_L, { 552, 335, 230, 141, },},
395   {QR_VER_9, QR_LEVEL_M, { 432, 262, 180, 111, },},
396   {QR_VER_9, QR_LEVEL_Q, { 312, 189, 130,  80, },},
397   {QR_VER_9, QR_LEVEL_H, { 235, 143,  98,  60, },},
398
399   {QR_VER_10, QR_LEVEL_L, { 652, 395, 271, 167, },},
400   {QR_VER_10, QR_LEVEL_M, { 513, 311, 213, 131, },},
401   {QR_VER_10, QR_LEVEL_Q, { 364, 221, 151,  93, },},
402   {QR_VER_10, QR_LEVEL_H, { 288, 174, 119,  74, },},
403
404   {QR_VER_11, QR_LEVEL_L, { 772, 468, 321, 198, },},
405   {QR_VER_11, QR_LEVEL_M, { 604, 366, 251, 155, },},
406   {QR_VER_11, QR_LEVEL_Q, { 427, 259, 177, 109, },},
407   {QR_VER_11, QR_LEVEL_H, { 331, 200, 137,  85, },},
408
409   {QR_VER_12, QR_LEVEL_L, { 883, 535, 367, 226, },},
410   {QR_VER_12, QR_LEVEL_M, { 691, 419, 287, 177, },},
411   {QR_VER_12, QR_LEVEL_Q, { 489, 296, 203, 125, },},
412   {QR_VER_12, QR_LEVEL_H, { 374, 227, 155,  96, },},
413
414   {QR_VER_13, QR_LEVEL_L, {1022, 619, 425, 262, },},
415   {QR_VER_13, QR_LEVEL_M, { 796, 483, 331, 204, },},
416   {QR_VER_13, QR_LEVEL_Q, { 580, 352, 241, 149, },},
417   {QR_VER_13, QR_LEVEL_H, { 427, 259, 177, 109, },},
418
419   {QR_VER_14, QR_LEVEL_L, {1101, 667, 458, 282, },},
420   {QR_VER_14, QR_LEVEL_M, { 871, 528, 362, 223, },},
421   {QR_VER_14, QR_LEVEL_Q, { 621, 376, 258, 159, },},
422   {QR_VER_14, QR_LEVEL_H, { 468, 283, 194, 120, },},
423
424   {QR_VER_15, QR_LEVEL_L, {1250, 758, 520, 320, },},
425   {QR_VER_15, QR_LEVEL_M, { 991, 600, 412, 254, },},
426   {QR_VER_15, QR_LEVEL_Q, { 703, 426, 292, 180, },},
427   {QR_VER_15, QR_LEVEL_H, { 530, 321, 220, 136, },},
428
429   {QR_VER_16, QR_LEVEL_L, {1408, 854, 586, 361, },},
430   {QR_VER_16, QR_LEVEL_M, {1082, 656, 450, 277, },},
431   {QR_VER_16, QR_LEVEL_Q, { 775, 470, 322, 198, },},
432   {QR_VER_16, QR_LEVEL_H, { 602, 365, 250, 154, },},
433
434   {QR_VER_17, QR_LEVEL_L, {1548, 938, 644, 397, },},
435   {QR_VER_17, QR_LEVEL_M, {1212, 734, 504, 310, },},
436   {QR_VER_17, QR_LEVEL_Q, { 876, 531, 364, 224, },},
437   {QR_VER_17, QR_LEVEL_H, { 674, 408, 280, 173, },},
438
439   {QR_VER_18, QR_LEVEL_L, {1725,1046, 718, 442, },},
440   {QR_VER_18, QR_LEVEL_M, {1346, 816, 560, 345, },},
441   {QR_VER_18, QR_LEVEL_Q, { 948, 574, 394, 243, },},
442   {QR_VER_18, QR_LEVEL_H, { 746, 452, 310, 191, },},
443
444   {QR_VER_19, QR_LEVEL_L, {1903,1153, 792, 488, },},
445   {QR_VER_19, QR_LEVEL_M, {1500, 909, 624, 384, },},
446   {QR_VER_19, QR_LEVEL_Q, {1063, 644, 442, 272, },},
447   {QR_VER_19, QR_LEVEL_H, { 813, 493, 338, 208, },},
448
449   {QR_VER_20, QR_LEVEL_L, {2061,1249, 858, 528, },},
450   {QR_VER_20, QR_LEVEL_M, {1600, 970, 666, 410, },},
451   {QR_VER_20, QR_LEVEL_Q, {1159, 702, 482, 297, },},
452   {QR_VER_20, QR_LEVEL_H, { 919, 557, 382, 235, },},
453
454   {QR_VER_21, QR_LEVEL_L, {2232,1352, 929, 572, },},
455   {QR_VER_21, QR_LEVEL_M, {1708,1035, 711, 438, },},
456   {QR_VER_21, QR_LEVEL_Q, {1224, 742, 509, 314, },},
457   {QR_VER_21, QR_LEVEL_H, { 969, 587, 403, 248, },},
458
459   {QR_VER_22, QR_LEVEL_L, {2409,1460,1003, 618, },},
460   {QR_VER_22, QR_LEVEL_M, {1872,1134, 779, 480, },},
461   {QR_VER_22, QR_LEVEL_Q, {1358, 823, 565, 348, },},
462   {QR_VER_22, QR_LEVEL_H, {1056, 640, 439, 270, },},
463
464   {QR_VER_23, QR_LEVEL_L, {2620,1588,1091, 672, },},
465   {QR_VER_23, QR_LEVEL_M, {2059,1248, 857, 528, },},
466   {QR_VER_23, QR_LEVEL_Q, {1468, 890, 611, 376, },},
467   {QR_VER_23, QR_LEVEL_H, {1108, 672, 461, 284, },},
468
469   {QR_VER_24, QR_LEVEL_L, {2812,1704,1171, 721, },},
470   {QR_VER_24, QR_LEVEL_M, {2188,1326, 911, 561, },},
471   {QR_VER_24, QR_LEVEL_Q, {1588, 963, 661, 407, },},
472   {QR_VER_24, QR_LEVEL_H, {1228, 744, 511, 315, },},
473
474   {QR_VER_25, QR_LEVEL_L, {3057,1853,1273, 784, },},
475   {QR_VER_25, QR_LEVEL_M, {2395,1451, 997, 614, },},
476   {QR_VER_25, QR_LEVEL_Q, {1718,1041, 715, 440, },},
477   {QR_VER_25, QR_LEVEL_H, {1286, 779, 535, 330, },},
478
479   {QR_VER_26, QR_LEVEL_L, {3283,1990,1367, 842, },},
480   {QR_VER_26, QR_LEVEL_M, {2544,1542,1059, 652, },},
481   {QR_VER_26, QR_LEVEL_Q, {1804,1094, 751, 462, },},
482   {QR_VER_26, QR_LEVEL_H, {1425, 864, 593, 365, },},
483
484   {QR_VER_27, QR_LEVEL_L, {3517,2132,1465, 902, },},
485   {QR_VER_27, QR_LEVEL_M, {2701,1637,1125, 692, },},
486   {QR_VER_27, QR_LEVEL_Q, {1933,1172, 805, 496, },},
487   {QR_VER_27, QR_LEVEL_H, {1501, 910, 625, 385, },},
488
489   {QR_VER_28, QR_LEVEL_L, {3669,2223,1528, 940, },},
490   {QR_VER_28, QR_LEVEL_M, {2857,1732,1190, 732, },},
491   {QR_VER_28, QR_LEVEL_Q, {2085,1263, 868, 534, },},
492   {QR_VER_28, QR_LEVEL_H, {1581, 958, 658, 405, },},
493
494   {QR_VER_29, QR_LEVEL_L, {3909,2369,1628,1002, },},
495   {QR_VER_29, QR_LEVEL_M, {3035,1839,1264, 778, },},
496   {QR_VER_29, QR_LEVEL_Q, {2181,1322, 908, 559, },},
497   {QR_VER_29, QR_LEVEL_H, {1677,1016, 698, 430, },},
498
499   {QR_VER_30, QR_LEVEL_L, {4158,2520,1732,1066, },},
500   {QR_VER_30, QR_LEVEL_M, {3289,1994,1370, 843, },},
501   {QR_VER_30, QR_LEVEL_Q, {2358,1429, 982, 604, },},
502   {QR_VER_30, QR_LEVEL_H, {1782,1080, 742, 457, },},
503
504   {QR_VER_31, QR_LEVEL_L, {4417,2677,1840,1132, },},
505   {QR_VER_31, QR_LEVEL_M, {3486,2113,1452, 894, },},
506   {QR_VER_31, QR_LEVEL_Q, {2473,1499,1030, 634, },},
507   {QR_VER_31, QR_LEVEL_H, {1897,1150, 790, 486, },},
508
509   {QR_VER_32, QR_LEVEL_L, {4686,2840,1952,1201, },},
510   {QR_VER_32, QR_LEVEL_M, {3693,2238,1538, 947, },},
511   {QR_VER_32, QR_LEVEL_Q, {2670,1618,1112, 684, },},
512   {QR_VER_32, QR_LEVEL_H, {2022,1226, 842, 518, },},
513
514   {QR_VER_33, QR_LEVEL_L, {4965,3009,2068,1273, },},
515   {QR_VER_33, QR_LEVEL_M, {3909,2369,1628,1002, },},
516   {QR_VER_33, QR_LEVEL_Q, {2805,1700,1168, 719, },},
517   {QR_VER_33, QR_LEVEL_H, {2157,1307, 898, 553, },},
518
519   {QR_VER_34, QR_LEVEL_L, {5253,3183,2188,1347, },},
520   {QR_VER_34, QR_LEVEL_M, {4134,2506,1722,1060, },},
521   {QR_VER_34, QR_LEVEL_Q, {2949,1787,1228, 756, },},
522   {QR_VER_34, QR_LEVEL_H, {2301,1394, 958, 590, },},
523
524   {QR_VER_35, QR_LEVEL_L, {5529,3351,2303,1417, },},
525   {QR_VER_35, QR_LEVEL_M, {4343,2632,1809,1113, },},
526   {QR_VER_35, QR_LEVEL_Q, {3081,1867,1283, 790, },},
527   {QR_VER_35, QR_LEVEL_H, {2361,1431, 983, 605, },},
528
529   {QR_VER_36, QR_LEVEL_L, {5836,3537,2431,1496, },},
530   {QR_VER_36, QR_LEVEL_M, {4588,2780,1911,1176, },},
531   {QR_VER_36, QR_LEVEL_Q, {3244,1966,1351, 832, },},
532   {QR_VER_36, QR_LEVEL_H, {2524,1530,1051, 647, },},
533
534   {QR_VER_37, QR_LEVEL_L, {6153,3729,2563,1577, },},
535   {QR_VER_37, QR_LEVEL_M, {4775,2894,1989,1224, },},
536   {QR_VER_37, QR_LEVEL_Q, {3417,2071,1423, 876, },},
537   {QR_VER_37, QR_LEVEL_H, {2625,1591,1093, 673, },},
538
539   {QR_VER_38, QR_LEVEL_L, {6479,3927,2699,1661, },},
540   {QR_VER_38, QR_LEVEL_M, {5039,3054,2099,1292, },},
541   {QR_VER_38, QR_LEVEL_Q, {3599,2181,1499, 923, },},
542   {QR_VER_38, QR_LEVEL_H, {2735,1658,1139, 701, },},
543
544   {QR_VER_39, QR_LEVEL_L, {6743,4087,2809,1729, },},
545   {QR_VER_39, QR_LEVEL_M, {5313,3220,2213,1362, },},
546   {QR_VER_39, QR_LEVEL_Q, {3791,2298,1579, 972, },},
547   {QR_VER_39, QR_LEVEL_H, {2927,1774,1219, 750, },},
548
549   {QR_VER_40, QR_LEVEL_L, {7089,4296,2953,1817, },},
550   {QR_VER_40, QR_LEVEL_M, {5596,3391,2331,1435, },},
551   {QR_VER_40, QR_LEVEL_Q, {3993,2420,1663,1024, },},
552   {QR_VER_40, QR_LEVEL_H, {3057,1852,1273, 784, },},
553 };
554
555
556 static char   *s_get_mode_spec       (qr_code_t *qrcode);
557 static char   *s_get_char_bit_count  (qr_code_t *qrcode, int len);
558 static char   *s_data_to_bin_num     (qr_code_t *qrcode, int data_code_count);
559 static char   *s_data_to_bin_alpha   (qr_code_t *qrcode, int data_code_count);
560 static int    s_char_to_num_alpha    (qr_code_t *qrcode, char src);
561 static char   *s_data_to_bin_8bit    (qr_code_t *qrcode, int data_code_count);
562 static char   *s_data_to_bin_kanji   (qr_code_t *qrcode, int data_code_count);
563 static char   *s_tidy_8bit_code      (qr_code_t *qrcode, const char *indata, int data_code_count);
564 static char   *s_str_to_bin          (qr_code_t *qrcode, char *indata, int data_code_count);
565 static void   s_init_modules         (qr_code_t *qrcode, qr_mask_pattern_t pat, char *module[]);
566 static void   s_setup_probe_position (qr_code_t *qrcode, char *dst[], int pos_x, int pos_y);
567 static void   s_setup_timing_pattern (qr_code_t *qrcode, char *dst[]);
568 static void   s_setup_position_adjust(qr_code_t *qrcode, char *dst[]);
569 static void   s_setup_type_info      (qr_code_t *qrcode, char *dst[], qr_mask_pattern_t pat);
570 static void   s_setup_version_info   (qr_code_t *qrcode, char *dst[]);
571 static void   s_map_data             (qr_code_t *qrcode, char *dst[], unsigned char *indata,
572                 int data_count, qr_mask_pattern_t pat);
573 static int    s_get_mask            (qr_mask_pattern_t pat, int yy, int xx);
574 static int    s_get_bit_count       (int data);
575 static int    s_calc_lost_point     (qr_code_t *qrcode, char *dst[]);
576 static int    s_count_same_module   (qr_ver_t ver, char* dst[]);
577 static int    s_count_same_block    (qr_ver_t ver, char* dst[]);
578 static int    s_count_11311_pattern (qr_ver_t ver, char* dst[]);
579 static int    s_count_dark_ratio    (qr_ver_t ver, char* dst[]);
580
581 static void   chxj_qr_code          (qr_code_t *qrcode, char *module[]);
582
583 int
584 chxj_qr_code_handler(request_rec *r)
585 {
586   int                sts;
587   size_t             len;
588   qr_code_t          qrcode;
589   Doc                doc;
590   char               *img;
591   Node               *root;
592   mod_chxj_config    *conf;
593
594   DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
595
596   if (strcasecmp(r->handler, "chxj-qrcode")) {
597     DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
598     return DECLINED;
599   }
600
601   /*--------------------------------------------------------------------------*/
602   /* ¤â¤·¡¢¥¤¥á¡¼¥¸ÊÑ´¹¥Ï¥ó¥É¥éÃæ¤Ç¤¢¤ì¤Ð¡¢¤³¤³¤Ç¤Ï½èÍý¤·¤Ê¤¤                 */
603   /*--------------------------------------------------------------------------*/
604   conf = chxj_get_module_config(r->per_dir_config, &chxj_module);
605   if (!conf || conf->image == CHXJ_IMG_ON)
606     return DECLINED;
607
608   memset(&doc, 0, sizeof(Doc));
609   memset(&qrcode, 0, sizeof(qr_code_t));
610
611   doc.r           = r;
612   doc.parse_mode  = PARSE_MODE_CHTML;
613   qrcode.doc      = &doc;
614   qrcode.r        = r;
615
616   qs_init_malloc(&doc);
617   root = qs_parse_file(&doc, r->filename);
618   qrcode.found = QR_NOT_FOUND;
619   chxj_qrcode_node_to_qrcode(&qrcode, root);
620
621   if (qrcode.found == QR_NOT_FOUND) {
622     DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
623     return HTTP_NOT_FOUND;
624   }
625
626   qs_all_free(&doc,QX_LOGMARK);
627
628   sts = chxj_qrcode_create_image_data(&qrcode, &img, &len);
629   if (sts != OK) {
630     DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
631     return sts;
632   }
633
634   chxj_set_content_type(r, "image/jpg");
635
636   ap_rwrite((void *)img, len, r);
637
638   DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
639   return OK;
640 }
641
642
643 char *
644 chxj_qr_code_blob_handler(request_rec *r, const char *indata, size_t *len)
645 {
646   int                sts;
647   qr_code_t          qrcode;
648   Doc                doc;
649   char               *img;
650   Node               *root;
651
652   DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
653
654   memset(&doc, 0, sizeof(Doc));
655   memset(&qrcode, 0, sizeof(qr_code_t));
656
657   doc.r           = r;
658   doc.parse_mode  = PARSE_MODE_CHTML;
659   qrcode.doc      = &doc;
660   qrcode.r        = r;
661
662   qs_init_malloc(&doc);
663   root = qs_parse_string(&doc, indata, *len);
664   qrcode.found = QR_NOT_FOUND;
665   chxj_qrcode_node_to_qrcode(&qrcode, root);
666   qs_all_free(&doc,QX_LOGMARK);
667
668   if (qrcode.found == QR_NOT_FOUND) {
669     DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
670     return NULL;
671   }
672
673   sts = chxj_qrcode_create_image_data(&qrcode, &img, len);
674   if (sts != OK) {
675     DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
676     return NULL;
677   }
678
679   chxj_set_content_type(r, "image/jpg");
680
681   DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
682
683   return img;
684 }
685
686
687 int
688 chxj_qrcode_create_image_data(
689   qr_code_t   *qrcode,
690   char        **img,
691   apr_size_t  *img_len)
692 {
693   int                xx, yy;
694   int                module_count;
695   MagickBooleanType  status;
696   request_rec        *r = qrcode->r;
697   MagickWand         *magick_wand;
698   MagickWand         *black_wand;
699   unsigned char      *tmp;
700   char               **module;
701
702 #ifdef QR_CODE_DEBUG
703   DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
704 #endif
705
706   module_count  = v_module_count_table[qrcode->version];
707   module = (char **)apr_palloc(r->pool, sizeof(char*)*(module_count+1));
708   for (yy=0; yy<module_count; yy++) {
709     module[yy] = (char*)apr_palloc(r->pool, module_count+1);
710     memset(module[yy], -1, module_count+1);
711   }
712
713   chxj_qr_code(qrcode,  module);
714
715   magick_wand = NewMagickWand();
716   black_wand = NewMagickWand();
717
718
719   if (MagickSetSize(magick_wand, 24, 24) == MagickFalse)
720     goto on_error;
721
722   if (MagickSetSize(black_wand, 24, 24) == MagickFalse)
723     goto on_error;
724
725   if (MagickReadImageBlob(magick_wand, v_white_base_pic, sizeof(v_white_base_pic)) == MagickFalse) 
726     goto on_error;
727
728   if (MagickReadImageBlob(black_wand, v_black_pixel_pic, sizeof(v_black_pixel_pic)) == MagickFalse)
729     goto on_error;
730
731   if (MagickSetImageUnits(magick_wand, PixelsPerInchResolution) == MagickFalse)
732     goto on_error;
733
734   if (MagickSetImageUnits(black_wand, PixelsPerInchResolution) == MagickFalse)
735     goto on_error;
736
737   if (MagickResizeImage(magick_wand, (module_count)+6, (module_count)+6 , LanczosFilter,0.0) == MagickFalse)
738     goto on_error;
739
740   if (MagickSetImageResolution(magick_wand, 96,96) == MagickFalse) 
741     goto on_error;
742
743   if (MagickSetImageResolution(black_wand, 96,96) == MagickFalse)
744     goto on_error;
745
746   if (MagickSetImageType(magick_wand, GrayscaleType) == MagickFalse)
747     goto on_error;
748
749   if (MagickSetImageType(black_wand, GrayscaleType) == MagickFalse)
750     goto on_error;
751
752
753   for (yy=0; yy<module_count; yy++) {
754     for (xx=0; xx<module_count; xx++) {
755       if (module[yy][xx] == '1') {
756         if (MagickCompositeImage(magick_wand, black_wand, CopyCompositeOp, xx+3,yy+3) == MagickFalse)
757           goto on_error;
758       }
759     }
760   }
761
762   if (qrcode->size <= 0) {
763     status = MagickResizeImage(magick_wand, ((module_count)*4+6), ((module_count)*4+6) , LanczosFilter,0.0);
764   }
765   else {  
766     status = MagickResizeImage(magick_wand, 
767                                (module_count * qrcode->size + 6), 
768                                (module_count * qrcode->size + 6) , 
769                                LanczosFilter,
770                                0.0);
771   }
772   if (status == MagickFalse) 
773     goto on_error;
774
775   if (MagickSetImageFormat(magick_wand,"jpg") == MagickFalse)
776     goto on_error;
777
778 #ifdef QR_CODE_DEBUG
779   DBG(r,"REQ[%X] convert to jpg",TO_ADDR(r));
780 #endif
781
782   tmp = (unsigned char*)MagickGetImageBlob(magick_wand,img_len);
783   *img = apr_palloc(r->pool, *img_len);
784   memcpy(*img, tmp, *img_len);
785
786
787   DestroyMagickWand(magick_wand);
788   DestroyMagickWand(black_wand);
789
790
791 #ifdef QR_CODE_DEBUG
792   DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
793 #endif
794
795   return OK;
796
797 on_error:
798   EXIT_MAGICK_ERROR();
799   return HTTP_NOT_FOUND;
800 }
801
802
803
804 static void
805 chxj_qr_code(qr_code_t *qrcode, char *module[])
806 {
807   request_rec       *r     = qrcode->doc->r;
808   unsigned char     *eccstr;
809   int               ii;
810   int               data_code_count;
811   char              *real_data  = NULL;
812   char              *decstr;
813   char              *binstr;
814   int               data_total_count;
815   int               min_lost_point;
816   int               min_mask_pattern;
817   int               module_count = v_module_count_table[qrcode->version];
818
819 #ifdef QR_CODE_DEBUG
820   DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
821 #endif
822   do {
823     /*------------------------------------------------------------------------*/
824     /* ¥â¡¼¥É»Ø¼¨»Ò¤ò¼èÆÀ¤·¤Þ¤¹                                               */
825     /*------------------------------------------------------------------------*/
826     binstr = apr_psprintf(r->pool, "%s", s_get_mode_spec(qrcode));
827 #ifdef QR_CODE_DEBUG
828     DBG(r,"REQ[%X] s_get_mode_spec()",TO_ADDR(r));
829 #endif
830
831     /*------------------------------------------------------------------------*/
832     /* Ê¸»ú¿ô»Ø¼¨»Ò¤ò¼èÆÀ¤·¤Þ¤¹                                               */
833     /*------------------------------------------------------------------------*/
834     binstr = apr_pstrcat(r->pool, binstr, 
835                   s_get_char_bit_count(qrcode, strlen(qrcode->indata)), NULL);
836 #ifdef QR_CODE_DEBUG
837     DBG(r,"REQ[%X] s_get_char_bit_count()[%s]", TO_ADDR(r),binstr);
838 #endif
839
840     /*------------------------------------------------------------------------*/
841     /* ¥Ç¡¼¥¿¥³¡¼¥É¿ô¤ò»»½Ð                                                   */
842     /*------------------------------------------------------------------------*/
843     data_code_count = v_data_code_count[qrcode->version][qrcode->level];
844 #ifdef QR_CODE_DEBUG
845     DBG(r,"REQ[%X] data_code_count=[%d]", TO_ADDR(r),data_code_count);
846 #endif
847
848     /*------------------------------------------------------------------------*/
849     /* ¼Â¥Ç¡¼¥¿¤Î£²¿Ê²½                                                       */
850     /*------------------------------------------------------------------------*/
851     qrcode->mode_change = QR_NOT_CHANGE;
852     switch(qrcode->mode) {
853     case QR_NUM_MODE:
854       real_data = s_data_to_bin_num(qrcode, data_code_count);
855       break;
856
857     case QR_ALPHA_MODE:
858       real_data = s_data_to_bin_alpha(qrcode, data_code_count);
859       break;
860
861     case QR_8BIT_MODE:
862       real_data = s_data_to_bin_8bit(qrcode,data_code_count);
863       break;
864
865     case QR_KANJI_MODE:
866       real_data = s_data_to_bin_kanji(qrcode,data_code_count);
867       break;
868
869     default:
870       qrcode->mode = QR_8BIT_MODE;
871       qrcode->mode_change = QR_CHANGE;
872       break;
873     }
874   }
875   while(qrcode->mode_change == QR_CHANGE);
876
877   /*--------------------------------------------------------------------------*/
878   /* ½ªÃ¼¥Ñ¥¿¡¼¥ó¤ÎÉղà                                                      */
879   /*--------------------------------------------------------------------------*/
880   binstr = apr_pstrcat(r->pool, binstr, real_data, NULL);
881 #ifdef QR_CODE_DEBUG
882   DBG(r,"REQ[%X] Before TERM BIT[%s]", TO_ADDR(r),binstr);
883 #endif
884   if ((size_t)(data_code_count * 8) > strlen(binstr)) {
885     int binstr_len = strlen(binstr);
886     for (ii=0; ii< (data_code_count * 8) - binstr_len && ii < 4; ii++) {
887       binstr = apr_pstrcat(r->pool, binstr, "0", NULL);
888     }
889   }
890 #ifdef QR_CODE_DEBUG
891   DBG(r,"REQ[%X] After TERM BIT[%s] len[%d]bit [%f]byte", TO_ADDR(r),binstr, strlen(binstr), (float)(((float)strlen(binstr))/8.0));
892   DBG(r,"REQ[%X] add term data",TO_ADDR(r));
893 #endif
894
895   /*--------------------------------------------------------------------------*/
896   /* 8bitñ°Ì¤Ë¤½¤í¤¨¤ë                                                       */
897   /*--------------------------------------------------------------------------*/
898   binstr = s_tidy_8bit_code(qrcode, binstr, data_code_count);
899 #ifdef QR_CODE_DEBUG
900   DBG(r,"REQ[%X] After Tidy 8 BIT[%s]", TO_ADDR(r),binstr);
901   DBG(r,"REQ[%X] s_tidy_8bit_code()",TO_ADDR(r));
902 #endif
903
904   /*--------------------------------------------------------------------------*/
905   /* Ê¸»úÎó¤ò¥Ð¥¤¥Ê¥ê¤Ø                                                       */
906   /*--------------------------------------------------------------------------*/
907   decstr = s_str_to_bin(qrcode, binstr, data_code_count);
908 #ifdef QR_CODE_DEBUG
909   DBG(r,"REQ[%X] s_str_to_bin()",TO_ADDR(r));
910 #endif
911
912 #ifdef QR_CODE_DEBUG
913   /*--------------------------------------------------------------------------*/
914   /* DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG  */
915   /*--------------------------------------------------------------------------*/
916   do {
917     char* rows = apr_psprintf(r->pool, " ");
918     for (ii=0; ii<data_code_count; ii++) {
919       rows = apr_pstrcat(r->pool, rows, apr_psprintf(r->pool, "[%02x]\n", (unsigned char)decstr[ii]), NULL);
920     }
921     DBG(r,"REQ[%X] decstr[%s]", TO_ADDR(r),rows);
922   }while(0);
923 #endif
924
925
926   /*--------------------------------------------------------------------------*/
927   /* ECC¤ÎÄɲà                                                               */
928   /* ½ÐÎϤȤ·¤Æ¤Îeccstr¤Ï¡¢¥¤¥ó¥¿¥ê¡¼¥ÖºÑ¤ß¤Î¥Ç¡¼¥¿¤¬¥»¥Ã¥È¤µ¤ì¤Þ¤¹.          */
929   /*--------------------------------------------------------------------------*/
930   eccstr = apr_palloc(r->pool, module_count* module_count);
931   data_total_count = chxj_calc_ecc(qrcode, (unsigned char*)decstr, eccstr);
932 #ifdef QR_CODE_DEBUG
933   DBG(r,"REQ[%X] chxj_calc_ecc() total_count[%d]", TO_ADDR(r),data_total_count);
934 #endif
935
936   /*--------------------------------------------------------------------------*/
937   /* ºÇŬ¤Î¥Þ¥¹¥¯¤ò¼èÆÀ¤¹¤ë                                                   */
938   /* Á´¤Æ¤Î¥Ñ¥¿¡¼¥ó¤ò»î¤·¡¢ºÇ¤â¼ºÅÀ¤Î¾¯¤Ê¤¤¥Þ¥¹¥¯¤òÁªÂò¤·¤Þ¤¹                 */
939   /*--------------------------------------------------------------------------*/
940   min_lost_point = -1;
941   min_mask_pattern = -1;
942   for (ii=0; ii<8; ii++) {
943     int lost_point;
944     s_init_modules(qrcode, ii, module);
945     s_map_data(qrcode,  module, (unsigned char*)eccstr, data_total_count, ii);
946     lost_point = s_calc_lost_point(qrcode, module);
947
948     if (min_mask_pattern == -1) {
949       min_mask_pattern = ii;
950       min_lost_point   = lost_point;
951     }
952     else {
953       if (min_lost_point >= lost_point) {
954         min_mask_pattern = ii;
955         min_lost_point   = lost_point;
956       }
957     }
958   }
959
960   /*--------------------------------------------------------------------------*/
961   /* ºÇŬ¥Þ¥¹¥¯¤ÇºÆÅÙºî¤ê¤Ê¤ª¤·                                               */
962   /*--------------------------------------------------------------------------*/
963 #ifdef QR_CODE_DEBUG
964   DBG(r,"REQ[%X] selected mask[%d]", TO_ADDR(r),min_mask_pattern);
965 #endif
966
967   s_init_modules(qrcode, min_mask_pattern, module);
968   s_map_data(qrcode, 
969              module, 
970              (unsigned char*)eccstr, 
971              data_total_count, 
972              min_mask_pattern);
973
974
975 #ifdef QR_CODE_DEBUG
976   /*--------------------------------------------------------------------------*/
977   /* DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG  */
978   /*--------------------------------------------------------------------------*/
979   do {
980     int xx, yy;
981     int module_count = v_module_count_table[qrcode->version];
982     DBG(r,"REQ[%X] loop to [%d]", TO_ADDR(r),module_count);
983     for (yy=0; yy<module_count; yy++) {
984       char *rows ;
985
986       rows = apr_psprintf(r->pool, " ");
987       
988       for (xx=0; xx<module_count; xx++)
989         rows = apr_pstrcat(r->pool, rows, "[", apr_psprintf(r->pool, "%c", module[yy][xx]), "]", NULL);
990
991       DBG(r,"REQ[%X] %d:%s", TO_ADDR(r),yy,rows);
992     }
993   } while(0);
994   /* DEBUG END */
995 #endif
996
997 #ifdef QR_CODE_DEBUG
998   DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
999 #endif
1000 }
1001
1002 void
1003 chxj_qrcode_node_to_qrcode(qr_code_t *qrcode, Node *node)
1004 {
1005   request_rec  *r;
1006   Doc          *doc;
1007   Node         *child;
1008
1009   r   = qrcode->r;
1010   doc = qrcode->doc;
1011
1012   for (child = qs_get_child_node(doc,node);
1013        child ;
1014        child = qs_get_next_node(doc,child)) {
1015     char *name = qs_get_node_name(doc,child);
1016
1017     if (strcasecmp("qrcode",name) == 0) {
1018
1019       qrcode->found       = QR_FOUND;
1020       qrcode->mode_change = QR_NOT_CHANGE;
1021       qrcode->version     = QR_VER_5;
1022       qrcode->level       = QR_LEVEL_L;
1023       qrcode->mode        = QR_8BIT_MODE;
1024       qrcode->size        = 0;
1025       qrcode->indata      = NULL;
1026       chxj_qrcode_node_to_qrcode(qrcode, child);
1027     }
1028     else
1029     if (strcasecmp("version", name) == 0) {
1030       int   ver;
1031       Node  *cchild;
1032       char  *value;
1033
1034       cchild = qs_get_child_node(doc, child);
1035
1036       if (cchild == NULL) {
1037         qrcode->version = QR_VER_1;
1038         continue;
1039       }
1040
1041       name = qs_get_node_name(doc, cchild);
1042       value = qs_get_node_value(doc, cchild);
1043       if (strcasecmp("TEXT", name) != 0) {
1044         qrcode->version = QR_VER_1;
1045         continue;
1046       }
1047       if (value == NULL || strlen(value) == 0) {
1048         qrcode->version = QR_VER_1;
1049         continue;
1050       }
1051
1052       if (chxj_chk_numeric(value)) {
1053         qrcode->version = QR_VER_1;
1054         continue;
1055       }
1056       ver = chxj_atoi(value);
1057       if (ver <= 0 || ver > 40) {
1058         qrcode->version = QR_VER_1;
1059         continue;
1060       }
1061
1062       qrcode->version = ver - 1;
1063     }
1064     else
1065     if (strcasecmp("level", name) == 0) {
1066       Node *cchild = qs_get_child_node(doc, child);
1067       char *value;
1068       if (cchild == NULL) {
1069         qrcode->level = QR_LEVEL_L;
1070         continue;
1071       }
1072       name  = qs_get_node_name(doc, cchild);
1073       value = qs_get_node_value(doc, cchild);
1074       if (strcasecmp("TEXT", name) != 0) {
1075         qrcode->level = QR_LEVEL_L;
1076         continue;
1077       }
1078
1079       if (value == NULL || strlen(value) == 0) {
1080         qrcode->level = QR_LEVEL_L;
1081         continue;
1082       }
1083
1084       if (strcasecmp("L", value) == 0) {
1085         qrcode->level = QR_LEVEL_L;
1086       }
1087       else
1088       if (strcasecmp("M", value) == 0) {
1089         qrcode->level = QR_LEVEL_M;
1090       }
1091       else
1092       if (strcasecmp("Q", value) == 0) {
1093         qrcode->level = QR_LEVEL_Q;
1094       }
1095       else
1096       if (strcasecmp("H", value) == 0) {
1097         qrcode->level = QR_LEVEL_H;
1098       }
1099       else
1100       {
1101         qrcode->level = QR_LEVEL_L;
1102       }
1103     }
1104     else
1105     if (strcasecmp("mode", name) == 0)
1106     {
1107       Node *cchild = qs_get_child_node(doc, child);
1108       char *value;
1109       if (cchild == NULL)
1110       {
1111         qrcode->mode = QR_NUM_MODE;
1112         continue;
1113       }
1114       name = qs_get_node_name(doc, cchild);
1115       value = qs_get_node_value(doc, cchild);
1116       if (strcasecmp("TEXT", name) != 0)
1117       {
1118         qrcode->level = QR_NUM_MODE;
1119         continue;
1120       }
1121       if (value == NULL || strlen(value) == 0)
1122       {
1123         qrcode->level = QR_NUM_MODE;
1124         continue;
1125       }
1126
1127       /*----------------------------------------------------------------------*/
1128       /* ¿ô»ú¥â¡¼¥É                                                           */
1129       /*----------------------------------------------------------------------*/
1130       if (strcasecmp("NUM", value) == 0)
1131       {
1132         qrcode->mode    = QR_NUM_MODE;
1133       }
1134       else
1135       /*----------------------------------------------------------------------*/
1136       /* ±Ñ¿ô»ú¥â¡¼¥É                                                         */
1137       /*----------------------------------------------------------------------*/
1138       if (strcasecmp("ALPHA", value) == 0)
1139       {
1140         qrcode->mode    = QR_ALPHA_MODE;
1141       }
1142       else
1143       /*----------------------------------------------------------------------*/
1144       /* 8¥Ó¥Ã¥È¥Ð¥¤¥È¥â¡¼¥É                                                  */
1145       /*----------------------------------------------------------------------*/
1146       if (strcasecmp("8BIT", value) == 0)
1147       {
1148         qrcode->mode    = QR_8BIT_MODE;
1149       }
1150       else
1151       /*----------------------------------------------------------------------*/
1152       /* ´Á»ú¥â¡¼¥É                                                           */
1153       /*----------------------------------------------------------------------*/
1154       if (strcasecmp("KANJI", value) == 0)
1155       {
1156         qrcode->mode    = QR_KANJI_MODE;
1157       }
1158       else
1159       {
1160         qrcode->mode    = QR_NUM_MODE;
1161       }
1162     }
1163     else
1164     if (strcasecmp("data", name) == 0)
1165     {
1166       /* TODO: ²þ¹Ô¤â²Äǽ¤Ê¤è¤¦¤Ë½¤Àµ¤¹¤ë */
1167       Node *cchild = qs_get_child_node(doc, child);
1168       char *value;
1169
1170       qrcode->indata = apr_palloc(r->pool, 1);
1171       qrcode->indata[0] = 0;
1172       if (cchild == NULL)
1173       {
1174         continue;
1175       }
1176       for (;cchild; cchild = cchild->next)
1177       {
1178         name = qs_get_node_name(doc, cchild);
1179         value = qs_get_node_value(doc, cchild);
1180         if (strcasecmp("TEXT", name) != 0)
1181         {
1182           continue;
1183         }
1184         if (strlen(qrcode->indata) > 0)
1185         {
1186           qrcode->indata = apr_pstrcat(r->pool, qrcode->indata, "\r\n", NULL);
1187         }
1188         qrcode->indata = apr_pstrcat(r->pool, qrcode->indata, value, NULL);
1189       }
1190     }
1191     else
1192     if (strcasecmp("size", name) == 0)
1193     {
1194       int size;
1195       char *value;
1196       Node *cchild = qs_get_child_node(doc, child);
1197
1198       if (cchild == NULL)
1199       {
1200         qrcode->size = 0;
1201         continue;
1202       }
1203       name = qs_get_node_name(doc, cchild);
1204       value = qs_get_node_value(doc, cchild);
1205       if (strcasecmp("TEXT", name) != 0)
1206       {
1207         qrcode->size = 0;
1208         continue;
1209       }
1210       if (value == NULL || strlen(value) == 0)
1211       {
1212         qrcode->size = 0;
1213         continue;
1214       }
1215       if (chxj_chk_numeric(value))
1216       {
1217         qrcode->size = 0;
1218         continue;
1219       }
1220
1221       size = chxj_atoi(value);
1222       if (size <= 0)
1223       {
1224         qrcode->size = 0;
1225         continue;
1226       }
1227       if (size > 20)
1228       {
1229         qrcode->size = 20;
1230         continue;
1231       }
1232       qrcode->size = size;
1233     }
1234   }
1235 #ifdef QR_CODE_DEBUG
1236   DBG(r,"REQ[%X] qrcode->version[%d]", TO_ADDR(r),qrcode->version);
1237   DBG(r,"REQ[%X] qrcode->level[%d]", TO_ADDR(r),qrcode->level);
1238   DBG(r,"REQ[%X] qrcode->mode[%d]", TO_ADDR(r),qrcode->mode);
1239   DBG(r,"REQ[%X] qrcode->indata[%s]", TO_ADDR(r),qrcode->indata);
1240 #endif
1241 }
1242
1243
1244 /**
1245  * ¥â¡¼¥É»Ø¼¨»Ò¤ò¼èÆÀ¤·¤Þ¤¹.
1246  */
1247 static char *
1248 s_get_mode_spec(qr_code_t *qrcode)
1249 {
1250   char *result = (char*)apr_palloc(qrcode->r->pool, 4+1);
1251
1252   memset(result, 0, 5);
1253   memcpy(result, v_mode_table[qrcode->mode], 4);
1254 #ifdef QR_CODE_DEBUG
1255   DBG(r,"REQ[%X] Mode Spec[%s]", TO_ADDR(r),result);
1256 #endif
1257
1258   return result;
1259 }
1260
1261
1262 /**
1263  * Ê¸»ú¿ô»Ø¼¨»Ò¤ò¼èÆÀ¤·¤Þ¤¹.
1264  */
1265 static char *
1266 s_get_char_bit_count(qr_code_t *qrcode, int len)
1267 {
1268   int bit_count = v_char_count_table[qrcode->version][qrcode->mode];
1269   int ii,jj;
1270   char *tmp;
1271   char *result;
1272   int data_capacity   = v_capacity_table[qrcode->version*4+qrcode->level].size[qrcode->mode];
1273
1274   DBG(qrcode->r,"REQ[%X] len [%d]", TO_ADDR(qrcode->r),len);
1275   if (qrcode->mode == QR_KANJI_MODE && data_capacity * 2 < len) {
1276     len = data_capacity * 2;
1277   }
1278   else
1279   if (qrcode->mode != QR_KANJI_MODE && data_capacity < len) {
1280     len = data_capacity;
1281   }
1282
1283   if (qrcode->mode == QR_KANJI_MODE) {
1284     len /= 2;
1285   }
1286
1287   DBG(qrcode->r, "REQ[%X] len [%d]", TO_ADDR(qrcode->r),len);
1288
1289   tmp = (char *)apr_palloc(qrcode->r->pool, bit_count + 1);
1290   for (ii=0; ii<bit_count; ii++) {
1291     tmp[ii] = (len & 0x01) ? '1' : '0';
1292     len = len >> 1;
1293   }
1294
1295   tmp[bit_count] = '\0';
1296
1297   result = (char *)apr_palloc(qrcode->r->pool, bit_count+1);
1298   for (jj=0,ii=bit_count-1; ii>=0 && jj < bit_count; ii--, jj++) {
1299     result[ii] = tmp[jj];
1300   }
1301
1302   result[bit_count] = '\0';
1303
1304 #ifdef QR_CODE_DEBUG
1305   DBG(qrcode->r,"REQ[%X] Character Count bit [%s]", TO_ADDR(qrcode->r),result);
1306 #endif
1307
1308   return result;
1309 }
1310
1311 /**
1312  * ¿ô»ú¥â¡¼¥É
1313  * ÆþÎϥǡ¼¥¿¤«¤é¡¢£²¿Êʸ»úÎó¤ò¼èÆÀ¤·¤Þ¤¹.
1314  */
1315 static char *
1316 s_data_to_bin_num(qr_code_t *qrcode, int UNUSED(data_code_count))
1317 {
1318   int len = strlen(qrcode->indata);
1319   int setn;
1320   int modn;
1321   int ii;
1322   int jj;
1323   int kk;
1324   char  *result;
1325   char  tmp[4];
1326   char  tmp_bit[11];
1327   int data_capacity   = v_capacity_table[qrcode->version*4+qrcode->level].size[qrcode->mode];
1328
1329   if (data_capacity < len) {
1330     DBG(qrcode->r, "REQ[%X] input data is too long",TO_ADDR(qrcode->r));
1331     len = data_capacity;
1332   }
1333   setn = len / 3;
1334   modn = len % 3;
1335
1336   if (chxj_chk_numeric(qrcode->indata) != 0) {
1337     qrcode->mode_change = QR_CHANGE;
1338     qrcode->mode        = QR_8BIT_MODE;
1339     return NULL;
1340   }
1341
1342   result = (char*)apr_palloc(qrcode->r->pool, setn*10 + ((modn == 1) ? 4 : (modn == 2) ? 7 : 0) + 1); 
1343   kk = 0;
1344   for (ii=0; ii<len; ii++) {
1345     tmp[ii % 3] = qrcode->indata[ii];
1346     if ((ii % 3) == 2) {
1347       tmp[4] = 0;
1348       int n = atoi(tmp);
1349       for (jj=0; jj<10; jj++) {
1350         tmp_bit[jj] = (n & 0x01) ?  '1'  : '0';
1351         n = n >> 1;
1352       }
1353       tmp_bit[10] = 0;
1354       for (jj=10-1; jj>=0; jj--) {
1355         result[kk++] = tmp_bit[jj];
1356       }
1357     }
1358   }
1359
1360   if (modn != 0) {
1361     tmp[modn] = 0;
1362     int n = atoi(tmp);
1363     int bb = (modn == 1) ? 4 : 7;
1364
1365     for (jj=0; jj< bb; jj++) {
1366       tmp_bit[jj] = (n & 0x01) ?  '1'  : '0';
1367       n = n >> 1;
1368     }
1369
1370     tmp_bit[(modn == 1) ? 4 : 7] = 0;
1371
1372     for (jj=bb -1; jj>=0; jj--) {
1373       result[kk++] = tmp_bit[jj];
1374     }
1375   }
1376   result[kk] = 0;
1377   return result;
1378 }
1379
1380
1381 /**
1382  * ±Ñ¿ô»ú¥â¡¼¥É
1383  * ÆþÎϥǡ¼¥¿¤«¤é¡¢£²¿Êʸ»úÎó¤ò¼èÆÀ¤·¤Þ¤¹.
1384  */
1385 static char *
1386 s_data_to_bin_alpha(qr_code_t *qrcode, int UNUSED(data_code_count))
1387 {
1388   int len = strlen(qrcode->indata);
1389   int setn;
1390   int modn;
1391   int ii;
1392   int jj;
1393   int kk;
1394   char  *result;
1395   char  tmp[2+1];
1396   char  tmp_bit[11+1];
1397   int data_capacity   = v_capacity_table[qrcode->version*4+qrcode->level].size[qrcode->mode];
1398
1399   if (data_capacity < len) {
1400     DBG(qrcode->r, "REQ[%X] input data is too long",TO_ADDR(qrcode->r));
1401     len = data_capacity;
1402   }
1403   setn = len / 2;
1404   modn = len % 2;
1405
1406   result = (char *)apr_palloc(qrcode->r->pool, setn*11 + ((modn == 1) ? 6 : 0) + 1); 
1407
1408   kk = 0;
1409   for (ii=0; ii<len; ii++) {
1410     tmp[ii % 2] = qrcode->indata[ii];
1411     if ((ii % 2) == 1) {
1412       tmp[3] = 0;
1413       int n = s_char_to_num_alpha(qrcode,tmp[0])*45;
1414
1415       if (qrcode->mode_change == QR_CHANGE)
1416         return NULL;
1417
1418       n += s_char_to_num_alpha(qrcode,tmp[1]);
1419       if (qrcode->mode_change == QR_CHANGE)
1420         return NULL;
1421
1422       for (jj=0; jj<11; jj++) {
1423         tmp_bit[jj] = (n & 0x01) ?  '1'  : '0';
1424         n = n >> 1;
1425       }
1426
1427       tmp_bit[11] = 0;
1428
1429       for (jj=11-1; jj>=0; jj--)
1430         result[kk++] = tmp_bit[jj];
1431     }
1432   }
1433
1434   if (modn) {
1435
1436     tmp[modn] = 0;
1437     int n = s_char_to_num_alpha(qrcode,tmp[0]);
1438
1439     if (qrcode->mode_change == QR_CHANGE)
1440       return NULL;
1441
1442     for (jj=0; jj< 6; jj++) {
1443       tmp_bit[jj] = (n & 0x01) ?  '1'  : '0';
1444       n = n >> 1;
1445     }
1446
1447     tmp_bit[6] = 0;
1448
1449     for (jj=6-1; jj>=0; jj--)
1450       result[kk++] = tmp_bit[jj];
1451   }
1452   result[kk] = 0;
1453   return result;
1454 }
1455
1456
1457 /**
1458  * ±Ñ¿ô»ú¥â¡¼¥É¤ÎÊä½õ´Ø¿ô.
1459  * ±Ñ¿ô»ú¤«¤é¡¢¿ôÃͤËÊÑ´¹¤·¤Þ¤¹.
1460  */
1461 static int
1462 s_char_to_num_alpha(qr_code_t *qrcode, char src)
1463 {
1464   switch(src) {
1465   case '0': return 0;
1466   case '1': return 1;
1467   case '2': return 2;
1468   case '3': return 3;
1469   case '4': return 4;
1470   case '5': return 5;
1471   case '6': return 6;
1472   case '7': return 7;
1473   case '8': return 8;
1474   case '9': return 9;
1475   case 'A': return 10;
1476   case 'B': return 11;
1477   case 'C': return 12;
1478   case 'D': return 13;
1479   case 'E': return 14;
1480   case 'F': return 15;
1481   case 'G': return 16;
1482   case 'H': return 17;
1483   case 'I': return 18;
1484   case 'J': return 19;
1485   case 'K': return 20;
1486   case 'L': return 21;
1487   case 'M': return 22;
1488   case 'N': return 23;
1489   case 'O': return 24;
1490   case 'P': return 25;
1491   case 'Q': return 26;
1492   case 'R': return 27;
1493   case 'S': return 28;
1494   case 'T': return 29;
1495   case 'U': return 30;
1496   case 'V': return 31;
1497   case 'W': return 32;
1498   case 'X': return 33;
1499   case 'Y': return 34;
1500   case 'Z': return 35;
1501   case ' ': 
1502     return 36;
1503   case '$': 
1504     return 37;
1505   case '%': 
1506     return 38;
1507   case '*': 
1508     return 39;
1509   case '+': 
1510     return 40;
1511   case '-': 
1512     return 41;
1513   case '.': 
1514     return 42;
1515   case '/': 
1516     return 43;
1517   case ':': 
1518     return 44;
1519   default:
1520     qrcode->mode_change = QR_CHANGE;
1521     qrcode->mode = QR_8BIT_MODE;
1522     return -1;
1523   }
1524 }
1525
1526 /**
1527  * 8bit¥Ð¥¤¥È¥â¡¼¥É
1528  * ÆþÎϥǡ¼¥¿¤«¤é£²¿Êʸ»úÎó¤ò¼èÆÀ¤·¤Þ¤¹.
1529  */
1530 static char *
1531 s_data_to_bin_8bit(qr_code_t *qrcode, int UNUSED(data_code_count))
1532 {
1533   int len = strlen(qrcode->indata);
1534   int ii;
1535   int jj;
1536   int kk;
1537   char  *result;
1538   char  tmp_bit[8+1];
1539   int data_capacity   = v_capacity_table[qrcode->version*4+qrcode->level].size[qrcode->mode];
1540
1541   if (data_capacity < len) {
1542     DBG(qrcode->r, "REQ[%X] input data is too long",TO_ADDR(qrcode->r));
1543     len = data_capacity;
1544   }
1545
1546   result = (char*)apr_palloc(qrcode->r->pool, len*8 + 1); 
1547
1548   kk = 0;
1549   for (ii=0; ii<len; ii++) {
1550     int n;
1551
1552     n = (int)qrcode->indata[ii];
1553     for (jj=0; jj<8; jj++) {
1554       tmp_bit[jj] = (n & 0x01) ? '1' : '0';
1555       n = n >> 1;
1556     }
1557
1558     tmp_bit[8] = 0;
1559
1560     for (jj=8-1; jj>=0; jj--)
1561       result[kk++] = tmp_bit[jj];
1562   }
1563   result[kk] = 0;
1564
1565 #ifdef QR_CODE_DEBUG
1566   DBG(qrcode->r,"REQ[%X] input data --> 2bin result[%s] len:[%d]", TO_ADDR(qrcode->r),result, strlen(result));
1567 #endif
1568
1569   return result;
1570 }
1571
1572 /**
1573  * Kanji mode.
1574  * A binary character string is acquired from input data.
1575  */
1576 static char *
1577 s_data_to_bin_kanji(qr_code_t *qrcode, int UNUSED(data_code_count))
1578 {
1579   int len = strlen(qrcode->indata);
1580   int ii;
1581   int jj;
1582   int kk;
1583   char  *result;
1584   char  tmp_bit[13+1];
1585
1586   int data_capacity   = v_capacity_table[qrcode->version*4+qrcode->level].size[qrcode->mode];
1587
1588   if (data_capacity * 2 < len) {
1589     DBG(qrcode->r, "REQ[%X] input data is too long",TO_ADDR(qrcode->r));
1590     len = data_capacity * 2;
1591   }
1592
1593   if ((len % 2) != 0) {
1594     DBG(qrcode->r,"REQ[%X] invalid data.",TO_ADDR(qrcode->r));
1595     qrcode->mode_change = QR_CHANGE;
1596     qrcode->mode        = QR_8BIT_MODE;
1597     return NULL;
1598   }
1599
1600   result = (char*)apr_palloc(qrcode->r->pool, (len/2)*13 + 1); 
1601   for (kk=0,ii=0; ii<len-1; ii++) {
1602     int c;
1603     int up_c;
1604     int dn_c;
1605     c = (qrcode->indata[ii+0] & 0xff)<< 8;
1606     c +=(qrcode->indata[ii+1] & 0xff);
1607
1608     if (c >= 0x8140 && c <= 0x9FFC) {
1609       c -= 0x8140;
1610       up_c = ((c >> 8) & 0xff) * 0xC0;
1611       dn_c = ( c       & 0xff);
1612       c  = up_c;
1613       c += dn_c;
1614     }
1615     else
1616     if (c >= 0xE040 && c <= 0xEBBF) {
1617       c -= 0xC140;
1618       up_c = ((c >> 8) & 0xff) * 0xC0;
1619       dn_c = ( c       & 0xff);
1620       c  = up_c;
1621       c += dn_c;
1622     }
1623     else {
1624       qrcode->mode_change = QR_CHANGE;
1625       qrcode->mode        = QR_8BIT_MODE;
1626       return NULL;
1627     }
1628
1629     memset(tmp_bit, 0, 13+1);
1630     for (jj=0; jj<13; jj++) {
1631       tmp_bit[jj] = (c & 0x01) ? '1' : '0';
1632       c = c >> 1;
1633     }
1634
1635     tmp_bit[13] = 0;
1636     for (jj=13-1; jj>=0; jj--)
1637       result[kk++] = tmp_bit[jj];
1638
1639     ii++;
1640   }
1641   result[kk] = 0;
1642
1643 #ifdef QR_CODE_DEBUG
1644   DBG(qrcode->r,"REQ[%X] input data --> 2bin result[%s] len:[%d]", TO_ADDR(qrcode->r),result, strlen(result));
1645 #endif
1646
1647   return result;
1648 }
1649
1650 /**
1651  * 8bitŤˤ¢¤ï¤»¤Þ¤¹
1652  */
1653 static char *
1654 s_tidy_8bit_code(qr_code_t *qrcode, const char *indata, int data_code_count)
1655 {
1656   int len = strlen(indata);
1657   int ii;
1658   int n;
1659   int rest;
1660   char *tmp = NULL;
1661   char *result;
1662
1663 #ifdef QR_CODE_DEBUG
1664   DBG(qrcode->r,"REQ[%X] len[%d] data_code_count * 8 [%d]", TO_ADDR(qrcode->r),len, data_code_count * 8);
1665 #endif
1666
1667   assert (len <= data_code_count * 8);
1668   /* 8bit¤Ç³ä¤ì¤Ê¤¤¾ì¹ç¤Ï¡¢»Ä¤ê¤ò£°Ëä¤á¤·¡¢8¤Ç³ä¤ì¤ë¤è¤¦¤Ë¤¹¤ë */
1669   if (len % 8) {
1670     n = 8 - (len % 8);
1671     assert (len + n <= data_code_count * 8);
1672
1673     tmp = (char*)apr_palloc(qrcode->r->pool, n);
1674     for (ii=0; ii<n; ii++)
1675       tmp[ii] = '0';
1676
1677     tmp[ii] = 0;
1678   }
1679
1680   result = (char*)apr_palloc(qrcode->r->pool, data_code_count * 8 + 1);
1681   memset(result, 0, data_code_count * 8+ 1);
1682   memcpy(result, indata, len);
1683   if (len % 8)
1684     memcpy(&result[len], tmp, strlen(tmp));
1685
1686   len = strlen(result);
1687   rest = data_code_count - (len / 8);
1688   n = 0;
1689   while(rest>0) {
1690     for (ii=0; ii<8; ii++)
1691       result[len++] = v_pend_bit[n][ii];
1692
1693     n = (n == 0) ? 1 : 0;
1694     rest--;
1695   }
1696
1697 #ifdef QR_CODE_DEBUG
1698   DBG(qrcode->r,"REQ[%X] tidy len[%d] data_code_count[%d]", TO_ADDR(qrcode->r),strlen(result)/8, data_code_count);
1699 #endif
1700
1701   return result;
1702 }
1703
1704 /**
1705  * £²¿Êʸ»úÎó¤ò¥Ð¥¤¥Ê¥ê¤ËÊÑ´¹¤·¤Þ¤¹.
1706  */
1707 static char *
1708 s_str_to_bin(qr_code_t *qrcode, char *indata, int data_code_count)
1709 {
1710   char *result;
1711   int pos;
1712   int len ;
1713   int ii;
1714
1715   len = strlen(indata);
1716   result = (char *)apr_palloc(qrcode->r->pool, data_code_count);
1717   memset(result, 0, data_code_count);
1718
1719   pos = 0;
1720   for (ii=0; ii<len; ii++) {
1721     result[pos] = (result[pos] << 1) | (indata[ii] - '0');
1722     if ((ii % 8) == 7)
1723       pos ++;
1724   }
1725   return result;
1726 }
1727
1728 /**
1729  * ½ÐÎÏÎΰè¤ò½é´ü²½¤·¤Þ¤¹.
1730  * °ÌÃÖ¸¡½Ð¥Ñ¥¿¡¼¥ó¡¢°ÌÃÖ¤¢¤ï¤»¡¢¥¿¥¤¥ß¥ó¥°¡¢·Á¼°¾ðÊó¤òÀßÄꤷ¡¢
1731  * ¤½¤Î¤Û¤«¤Î¾ì½ê¤Ë¤Ï¡¢-1¤òÀßÄꤷ¤Þ¤¹.
1732  */
1733 static void
1734 s_init_modules(qr_code_t *qrcode, qr_mask_pattern_t pat, char *dst[])
1735 {
1736   int module_count = v_module_count_table[qrcode->version];
1737   int yy;
1738
1739 #ifdef QR_CODE_DEBUG
1740   request_rec *r = qrcode->r;
1741   DBG(qrcode->r,"REQ[%X] start %s()",TO_ADDR(qrcode->r),__func__);
1742   DBG(qrcode->r,"REQ[%X] module_count[%d]", TO_ADDR(r),module_count);
1743 #endif
1744
1745   for (yy=0; yy<module_count; yy++)
1746     memset(&dst[yy][0], -1, module_count+1);
1747
1748   s_setup_probe_position(qrcode, dst, 0, 0);
1749   s_setup_probe_position(qrcode, dst, 0, module_count - 7);
1750   s_setup_probe_position(qrcode, dst, module_count - 7, 0);
1751   s_setup_position_adjust(qrcode, dst);
1752   s_setup_timing_pattern(qrcode, dst);
1753   s_setup_type_info(qrcode,dst, pat);
1754   s_setup_version_info(qrcode, dst);
1755
1756 #ifdef QR_CODE_DEBUG
1757   DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
1758 #endif
1759
1760 }
1761
1762 /* 
1763  * °ÌÃÖ¸¡½Ð¥Ñ¥¿¡¼¥ó¤È¤Ä¤¤¤Ç¤ËʬΥ¥Ñ¥¿¡¼¥ó¤Î½ÐÎÏ
1764  */
1765 static void
1766 s_setup_probe_position(qr_code_t *qrcode, char *dst[], int pos_x, int pos_y)
1767 {
1768   int module_count = v_module_count_table[qrcode->version];
1769   int xx;
1770   int yy;
1771 #ifdef QR_CODE_DEBUG
1772   request_rec *r = qrcode->r;
1773   DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
1774   DBG(r,"REQ[%X] module_count[%d]", TO_ADDR(r),module_count);
1775 #endif
1776
1777   for (yy=-1; yy<=7; yy++) {
1778     for (xx=-1; xx<=7; xx++) {
1779       /* °ÌÃÖ¸¡½Ð¥Ñ¥¿¡¼¥ó */
1780       if ((yy==0 || yy == 6) && xx != -1 && xx != 7) {
1781         assert(yy+pos_y >= 0 && yy+pos_y < module_count);
1782         assert(xx+pos_x >= 0 && xx+pos_x < module_count);
1783         dst[yy+pos_y][xx+pos_x] = '1';
1784       }
1785       else
1786       if ((xx == 0 && yy != -1 && yy != 7)
1787       ||  (xx >= 2 && xx <= 4 && yy >=2 && yy <= 4)
1788       ||  (xx == 6 && yy != -1 && yy != 7)) {
1789         assert(yy+pos_y >= 0 && yy+pos_y < module_count);
1790         assert(xx+pos_x >= 0 && xx+pos_x < module_count);
1791         dst[yy+pos_y][xx+pos_x] = '1';
1792       }
1793       else {
1794         /* Ê¬Î¥¥Ñ¥¿¡¼¥ó */
1795         if ((yy == 7  
1796         &&   yy + pos_y >= 0 
1797         &&   yy + pos_y < module_count 
1798         &&   xx + pos_x >= 0 
1799         &&   xx + pos_x < module_count)
1800         ||  (yy == -1  
1801         &&   yy + pos_y >= 0 
1802         &&   yy + pos_y < module_count
1803         &&   xx + pos_x >= 0 
1804         &&   xx + pos_x < module_count)
1805         ||  (xx == 7  
1806         &&   xx + pos_x >= 0 
1807         &&   xx + pos_x < module_count 
1808         &&   yy + pos_y >= 0 
1809         &&   yy + pos_y < module_count)
1810         ||  (xx == -1  
1811         &&   xx + pos_x < module_count 
1812         &&   xx + pos_x >= 0
1813         &&   yy + pos_y >= 0 
1814         &&   yy + pos_y < module_count)) {
1815           assert(yy+pos_y >= 0 && yy+pos_y < module_count);
1816           assert(xx+pos_x >= 0 && xx+pos_x < module_count);
1817           dst[yy+pos_y][xx+pos_x] = '0';
1818         }
1819         else
1820         if (yy != -1 && yy != 7 && xx != -1 && xx != 7) {
1821           assert(yy+pos_y >= 0 && yy+pos_y < module_count);
1822           assert(xx+pos_x >= 0 && xx+pos_x < module_count);
1823           dst[yy+pos_y][xx+pos_x] = '0';
1824         }
1825       }
1826     }
1827   }
1828 #ifdef QR_CODE_DEBUG
1829   DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
1830 #endif
1831 }
1832 /* 
1833  * ¥¿¥¤¥ß¥ó¥°¥Ñ¥¿¡¼¥ó
1834  */
1835 static void
1836 s_setup_timing_pattern(qr_code_t *qrcode, char *dst[])
1837 {
1838   int module_count = v_module_count_table[qrcode->version];
1839   int xx, yy;
1840 #ifdef QR_CODE_DEBUG
1841   request_rec *r = qrcode->r;
1842   DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
1843   DBG(r,"REQ[%X] module_count[%d]", TO_ADDR(r),module_count);
1844 #endif
1845
1846   yy=6;
1847   for (xx=8; xx < module_count-8; xx++) {
1848     if (dst[yy][xx] != -1)
1849       continue;
1850
1851     assert(yy >= 0 && yy < module_count);
1852     assert(xx >= 0 && xx < module_count);
1853
1854     dst[yy][xx] = (xx % 2 == 0) ? '1' : '0';
1855   }
1856
1857   xx=6;
1858   for (yy=8; yy < module_count-8; yy++) {
1859     if (dst[yy][xx] != -1)
1860       continue;
1861
1862     assert(yy >= 0 && yy < module_count);
1863     assert(xx >= 0 && xx < module_count);
1864
1865     dst[yy][xx] = (yy % 2 == 0) ? '1' : '0';
1866   }
1867
1868 #ifdef QR_CODE_DEBUG
1869   DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
1870 #endif
1871 }
1872
1873 /*
1874  * °ÌÃÖ¹ç¤ï¤»¥Ñ¥¿¡¼¥ó
1875  */
1876 static void
1877 s_setup_position_adjust(qr_code_t *qrcode, char *dst[])
1878 {
1879   int module_count = v_module_count_table[qrcode->version];
1880   int *pos_list = v_position_adjust_table[qrcode->version].position;
1881   int xx, yy;
1882   int mxx, myy;
1883   int dxx, dyy;
1884
1885 #ifdef QR_CODE_DEBUG
1886   request_rec *r = qrcode->r;
1887   DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
1888 #endif
1889
1890   for (yy=0; yy<7; yy++) {
1891     for (xx=0; xx<7; xx++) {
1892       mxx = pos_list[xx];
1893       myy = pos_list[yy];
1894
1895       if (mxx == 0 || myy == 0)
1896         continue;
1897
1898       if (dst[myy][mxx] != -1)
1899         continue;
1900
1901       for (dyy = -2; dyy <= 2; dyy++) {
1902         for (dxx= -2; dxx <= 2; dxx++) {
1903           if (dyy == -2 
1904           ||  dxx == -2
1905           ||  dyy == 2
1906           ||  dxx == 2
1907           ||  (dyy == 0 && dxx == 0)) {
1908             assert(myy+dyy >= 0);
1909             assert(myy+dyy < module_count);
1910             dst[myy+dyy][mxx+dxx] = '1';
1911           }
1912           else {
1913             assert(myy+dyy >= 0);
1914             assert(myy+dyy < module_count);
1915             dst[myy+dyy][mxx+dxx] = '0';
1916           }
1917         }
1918       }
1919     }
1920   }
1921
1922 #ifdef QR_CODE_DEBUG
1923   DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
1924 #endif
1925 }
1926
1927 static void
1928 s_setup_type_info(qr_code_t *qrcode, char *dst[], qr_mask_pattern_t pat)
1929 {
1930   int module_count = v_module_count_table[qrcode->version];
1931   int typedata;
1932   int sdata;
1933   int gf;
1934   int gfmask;
1935   int xx;
1936   int yy;
1937
1938 #ifdef QR_CODE_DEBUG
1939   request_rec *r = qrcode->r;
1940   DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
1941   DBG(r,"REQ[%X] module_count[%d]", TO_ADDR(r),module_count);
1942 #endif
1943
1944   /* ·Á¼°¾ðÊó¥Ç¡¼¥¿¤ÎÀ¸À® */
1945   switch(qrcode->level) {
1946   case QR_LEVEL_L: sdata = 0x01 << 3; break;
1947   case QR_LEVEL_M: sdata = 0x00 << 3; break;
1948   case QR_LEVEL_Q: sdata = 0x03 << 3; break;
1949   case QR_LEVEL_H: sdata = 0x02 << 3; break;
1950   default:
1951     sdata = 0;
1952     break;
1953   }
1954   sdata |= pat;
1955
1956   /* G(x)=x10+x8+x5+x4+x2+x+1 */
1957   gf = (0x01 << 10) 
1958      | (0x01 << 8) 
1959      | (0x01 << 5) 
1960      | (0x01 << 4) 
1961      | (0x01 << 2) 
1962      | (0x01 << 1)
1963      | (0x01 << 0);
1964
1965   gfmask = (0x01 << 14) | (0x01 << 12) | (0x01 << 10)  | (0x01 << 4) | (0x01 << 1);
1966
1967
1968   typedata = sdata << 10;
1969   while (s_get_bit_count(typedata) - s_get_bit_count(gf) >= 0) {
1970     typedata ^= (gf << (s_get_bit_count(typedata) - s_get_bit_count(gf)));
1971   }
1972   typedata = (sdata << 10 | typedata);
1973
1974   typedata ^= gfmask;
1975
1976   for (yy=0; yy<6; yy++) {
1977     assert(yy >= 0 && yy < module_count);
1978     dst[yy][8] = (((typedata >> yy) & 0x01) == 1) ?  '1' : '0';
1979   }
1980
1981   for (; yy<8; yy++) {
1982     assert(yy + 1 >= 0 && yy + 1 < module_count);
1983     dst[yy+1][8] = (((typedata >> yy) & 0x01) == 1) ?  '1' : '0';
1984   }
1985
1986   for (; yy<15; yy++) {
1987     assert(module_count - 15 + yy >= 0 && module_count - 15 + yy < module_count);
1988     dst[module_count - 15 + yy][8] = (((typedata >> yy) & 0x01) == 1) ?  '1' : '0';
1989   }
1990
1991   for (xx = 0; xx < 8; xx++) {
1992     assert(module_count - xx -1 >= 0 && module_count - xx - 1 < module_count);
1993     dst[8][module_count - xx - 1] = (((typedata >> xx) & 0x01) == 1) ? '1' : '0';
1994   }
1995
1996   for (; xx<9; xx++) {
1997     assert(15 - xx >= 0 && 15 - xx < module_count);
1998     dst[8][15 - xx] = (((typedata >> xx) & 0x01) == 1) ? '1' : '0';
1999   }
2000
2001   for (; xx<15; xx++) {
2002     assert(15 - xx - 1 >= 0 && 15 - xx - 1 < module_count);
2003     dst[8][15 - xx - 1] = (((typedata >> xx) & 0x01) == 1) ? '1' : '0';
2004   }
2005  
2006   assert(module_count - 8 >= 0 && module_count - 8 < module_count);
2007   dst[module_count - 8][8] = '1';
2008
2009 #ifdef QR_CODE_DEBUG
2010   DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
2011 #endif
2012 }
2013
2014 static int
2015 s_get_bit_count(int data)
2016 {
2017   int count = 0;
2018
2019   while (data != 0) {
2020     count++;
2021     data >>= 1;
2022   }
2023
2024   return count;
2025 }
2026
2027
2028 static void
2029 s_setup_version_info(qr_code_t *qrcode, char *dst[])
2030 {
2031   char *bits = v_version_info_table[qrcode->version].bits;
2032   int module_count = v_module_count_table[qrcode->version];
2033   size_t xx;
2034   size_t yy;
2035
2036   if (bits == NULL) {
2037     /* ¥Ð¡¼¥¸¥ç¥ó£·°Ê¹ß¤Î¤ßÂоݠ*/
2038     return;
2039   }
2040
2041   /* ±¦¾å¤Î·¿ÈÖ¾ðÊó¤ÎÇÛÃÖ */
2042   for (xx=0; xx<strlen(bits); xx++) {
2043     dst[(int)((double)xx/3.0)][(xx % 3) + module_count - 8 - 3] = bits[xx];
2044   }
2045   
2046   /* º¸²¼¤Î·¿ÈÖ¾ðÊó¤ÎÇÛÃÖ */
2047   for (yy=0; yy<strlen(bits); yy++) {
2048     dst[(yy % 3) + module_count - 8 - 3][(int)((double)yy/3.0)] = bits[yy];
2049   } 
2050 }
2051
2052 static void 
2053 s_map_data(qr_code_t     *qrcode,
2054            char          *dst[], 
2055            unsigned char *indata, int data_count,qr_mask_pattern_t pat)
2056 {
2057   int module_count = v_module_count_table[qrcode->version];
2058   int inc_yy_flag = -1;
2059
2060   int xx, yy;
2061   int ii;
2062   int in_pos;
2063   int now_bit;
2064   int mask;
2065
2066 #ifdef QR_CODE_DEBUG
2067   request_rec *r = qrcode->r;
2068   DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
2069   DBG(r,"REQ[%X] module_count[%d]",TO_ADDR(r),module_count);
2070   DBG(r,"REQ[%X] data_count[%d]",TO_ADDR(r),data_count);
2071 #endif
2072   in_pos = 0;
2073   now_bit = 7;
2074
2075   yy = module_count - 1;
2076
2077   for (xx = module_count - 1; xx > 0 ; xx -= 2) {
2078     if (xx == 6) {
2079       xx--;
2080     }
2081     do {
2082       for (ii=0; ii < 2; ii++) {
2083         if (dst[yy][xx - ii] != -1)
2084           /* ´û¤ËÇÛÃÖ¤µ¤ì¤Æ¤¤¤ë¾ì¹ç */
2085           continue;
2086
2087         if (in_pos < data_count) {
2088           assert(yy >= 0 && yy < module_count);
2089           assert(xx-ii >= 0 && xx-ii < module_count);
2090
2091           dst[yy][xx-ii] = (((indata[in_pos] >> now_bit) & 0x01) == 1) ? '1' : '0';
2092           mask = s_get_mask(pat, yy, xx - ii);
2093           if (mask == 1) {
2094             if (dst[yy][xx-ii] == '1') {
2095               dst[yy][xx-ii] = '0';
2096             }
2097             else {
2098               dst[yy][xx-ii] = '1';
2099             }
2100           }
2101         } 
2102         if (--now_bit < 0) {
2103           in_pos++;
2104           now_bit = 7;
2105         }
2106           
2107       }
2108       yy += inc_yy_flag;
2109     } while (yy >= 0 && yy < module_count) ;
2110
2111     yy -= inc_yy_flag;
2112     inc_yy_flag = -inc_yy_flag;
2113   } 
2114
2115   /* ??? */
2116   for (yy=0; yy<module_count; yy++) {
2117     for (xx=0; xx<module_count; xx++) {
2118       if (dst[yy][xx] == -1) {
2119         dst[yy][xx] = '0';
2120         mask = s_get_mask(pat, yy, xx);
2121         if (mask == 1) {
2122           dst[yy][xx] = '1';
2123         }
2124       }
2125     }
2126   }
2127 #ifdef QR_CODE_DEBUG
2128   DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
2129 #endif
2130 }
2131
2132 static int
2133 s_get_mask(qr_mask_pattern_t pat, int yy, int xx)
2134 {
2135   switch (pat) {
2136   case QR_MASK_1: return ((yy + xx) % 2 == 0) ? 1 : 0;
2137   case QR_MASK_2: return (yy % 2 == 0) ? 1 : 0;
2138   case QR_MASK_3: return (xx % 3 == 0) ? 1 : 0;
2139   case QR_MASK_4: return ((yy + xx) % 3 == 0) ? 1 : 0;
2140   case QR_MASK_5: return (((yy / 2) + (xx / 3)) % 2 == 0) ? 1 : 0;
2141   case QR_MASK_6: return ((yy * xx) % 2 + (yy * xx) % 3 == 0) ? 1 : 0;
2142   case QR_MASK_7: return (((yy * xx) % 2 + (yy * xx) % 3) % 2 == 0) ? 1 : 0;
2143   case QR_MASK_8: return (((yy * xx) % 3 + (yy + xx) % 2) % 2 == 0) ? 1 : 0;
2144   default:
2145     return 0;
2146   }
2147 }
2148
2149
2150 static int
2151 s_calc_lost_point(qr_code_t *qrcode, char *dst[])
2152 {
2153   int point = 0;
2154
2155   /* Æ±¿§¤Î¹Ô¡¿Îó¤ÎÎÙÀܥ⥸¥å¡¼¥ë ¥â¥¸¥å¡¼¥ë¿ô=(5+i) 3+i  */
2156   point = s_count_same_module(qrcode->version,dst);
2157
2158   /* Æ±¿§¤Î¥â¥¸¥å¡¼¥ë¥Ö¥í¥Ã¥¯ ¥Ö¥í¥Ã¥¯¥µ¥¤¥º 2¡ß2 3  */
2159   point += s_count_same_block(qrcode->version, dst);
2160
2161   /* ¹Ô¡¿Îó¤Ë¤ª¤±¤ë1:1:3:1:1(°Å:ÌÀ:°Å:ÌÀ:°Å)¤Î¥Ñ¥¿¡¼¥ó */
2162   point += s_count_11311_pattern(qrcode->version, dst);
2163
2164   /* Á´ÂΤËÀê¤á¤ë°Å¥â¥¸¥å¡¼¥ë¤Î³ä¹ç 50¡Þ(5+k)%¡Á50¡Þ(5+(k+1))% 10¡ßk */
2165   point += s_count_dark_ratio(qrcode->version, dst);
2166
2167   return point;
2168 }
2169
2170 /* 
2171  * Æ±¿§¤Î¹Ô¡¿Îó¤ÎÎÙÀܥ⥸¥å¡¼¥ë ¥â¥¸¥å¡¼¥ë¿ô=(5+i) 
2172  * ¼ºÅÀ=3+i
2173  */
2174 static int
2175 s_count_same_module(qr_ver_t ver, char *dst[])
2176 {
2177   int module_count;
2178   int point;
2179   int xx, yy;
2180   int same_count;
2181   int prev_value;
2182
2183   module_count = v_module_count_table[ver];
2184   point        = 0;
2185   same_count   = 0;
2186   prev_value   = -1;
2187
2188   /* ²£Êý¸þ¤Ç¥Á¥§¥Ã¥¯ */
2189   for (yy=0; yy<module_count; yy++) {
2190     prev_value = -1;
2191     same_count = 0;
2192     for (xx=0; xx<module_count; xx++) {
2193       if (prev_value == (unsigned char)dst[yy][xx])
2194         same_count++;
2195       else
2196         same_count = 0;
2197
2198       if (same_count >= 5) 
2199         point++;
2200
2201       prev_value = (unsigned char)dst[yy][xx];
2202     }
2203   }
2204
2205   /* ½ÄÊý¸þ¤Ç¥Á¥§¥Ã¥¯ */
2206   for (xx=0; xx<module_count; xx++) {
2207     prev_value = -1;
2208     same_count = 0;
2209
2210     for (yy=0; yy<module_count; yy++) {
2211       if (prev_value == (unsigned char)dst[yy][xx]) {
2212         same_count++;
2213       }
2214       else {
2215         same_count = 0;
2216       }
2217
2218       if (same_count >= 5)
2219         point++;
2220
2221       prev_value = (unsigned char)dst[yy][xx];
2222     }
2223   }
2224
2225   if (point > 0)
2226     point += 3;
2227
2228   return point;
2229 }
2230
2231 /* 
2232  * Æ±¿§¤Î¥â¥¸¥å¡¼¥ë¥Ö¥í¥Ã¥¯ ¥Ö¥í¥Ã¥¯¥µ¥¤¥º 2¡ß2 
2233  */
2234 static int
2235 s_count_same_block(qr_ver_t ver, char *dst[])
2236 {
2237   int module_count;
2238   int point;
2239   int count;
2240   int xx,yy;
2241
2242   module_count = v_module_count_table[ver];
2243
2244   point = 0;
2245   for (yy = 0; yy < module_count-1; yy++) {
2246     for (xx = 0; xx < module_count-1; xx++) {
2247       count = 0;
2248       if (dst[yy+0][xx+0] == '1') count++;
2249       if (dst[yy+0][xx+1] == '1') count++;
2250       if (dst[yy+1][xx+0] == '1') count++;
2251       if (dst[yy+1][xx+1] == '1') count++;
2252       if (count == 0 || count == 4) {
2253         point += 3;
2254       }
2255     }
2256   }
2257   return point;
2258 }
2259
2260 static int
2261 s_count_11311_pattern(qr_ver_t ver, char *dst[])
2262 {
2263   int module_count;
2264   int point;
2265   int xx,yy;
2266
2267   module_count = v_module_count_table[ver];
2268
2269   point = 0;
2270   /* ²£Êý¸þ¤Î¥Á¥§¥Ã¥¯ */
2271   for (yy = 0; yy < module_count; yy++) {
2272     for (xx = 0; xx < module_count-6; xx++) {
2273       if (dst[yy][xx+0] == '1'
2274       &&  dst[yy][xx+1] == '0'
2275       &&  dst[yy][xx+2] == '1'
2276       &&  dst[yy][xx+3] == '1'
2277       &&  dst[yy][xx+4] == '1'
2278       &&  dst[yy][xx+5] == '0'
2279       &&  dst[yy][xx+6] == '1') {
2280         point += 40;
2281       }
2282     }
2283   }
2284
2285   /* ½ÄÊý¸þ¤Î¥Á¥§¥Ã¥¯ */
2286   for (xx = 0; xx < module_count; xx++) {
2287     for (yy = 0; yy < module_count-6; yy++) {
2288       if (dst[yy+0][xx] == '1'
2289       &&  dst[yy+1][xx] == '0'
2290       &&  dst[yy+2][xx] == '1'
2291       &&  dst[yy+3][xx] == '1'
2292       &&  dst[yy+4][xx] == '1'
2293       &&  dst[yy+5][xx] == '0'
2294       &&  dst[yy+6][xx] == '1') {
2295         point += 40;
2296       }
2297     }
2298   }
2299
2300   return point;
2301 }
2302
2303
2304 /* 
2305  * Á´ÂΤËÀê¤á¤ë°Å¥â¥¸¥å¡¼¥ë¤Î³ä¹ç 50¡Þ(5+k)%¡Á50¡Þ(5+(k+1))% 
2306  */
2307 static int
2308 s_count_dark_ratio(qr_ver_t ver, char *dst[])
2309 {
2310   int module_count;
2311   int dark_count;
2312   int xx, yy;
2313   int ratio;
2314   int all_count;
2315
2316   module_count = v_module_count_table[ver];
2317   dark_count   = 0;
2318   all_count    = module_count * module_count;
2319
2320   /* Ã±¤Ë"°Å"¤ò¥«¥¦¥ó¥È¤¹¤ë */
2321   for (yy=0; yy<module_count; yy++) {
2322     for (xx=0; xx < module_count; xx++)
2323       if (dst[yy][xx] == '1')
2324         dark_count++;
2325   }
2326
2327   /* % ¤òµá¤á¡¢50¡ó¤«¤é¤Îº¹¤òµá¤á¤ë¡£ */
2328   ratio = (int)(((double)(dark_count) / (double)all_count)*100.0 - 50.0);
2329   ratio = abs(ratio);
2330
2331   /* 5%Ëè¤Ë10ÅÀ¸ºÅÀ¤Ê¤Î¤Ç¡¢¤Þ¤º5¤Ç³ä¤ë */
2332   ratio = ratio / 5;
2333   return ratio * 10;
2334 }
2335
2336
2337
2338 /*
2339  * vim:ts=2 et
2340  */