OSDN Git Service

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