OSDN Git Service

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