QR_Encode.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129
  1. // QR_Encode.cpp : CQR_Encode クラス インプリメンテーション ファイル
  2. // Date 2006/05/17 Ver. 1.22 Psytec Inc.
  3. #include "stdafx.h"
  4. #include "QR_Encode.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. /////////////////////////////////////////////////////////////////////////////
  11. // QRcode bar约翰(型号)信息
  12. static QR_VERSIONINFO QR_VersonInfo[] =
  13. {
  14. {0}, // (假的∶Ver.0)
  15. { 1, // Ver.1
  16. 26, 19, 16, 13, 9,
  17. 0, 0, 0, 0, 0, 0, 0,
  18. 1, 26, 19,
  19. 1, 26, 16,
  20. 1, 26, 13,
  21. 1, 26, 9,
  22. 0, 0, 0,
  23. 0, 0, 0,
  24. 0, 0, 0,
  25. 0, 0, 0},
  26. { 2, // Ver.2
  27. 44, 34, 28, 22, 16,
  28. 1, 18, 0, 0, 0, 0, 0,
  29. 1, 44, 34,
  30. 1, 44, 28,
  31. 1, 44, 22,
  32. 1, 44, 16,
  33. 0, 0, 0,
  34. 0, 0, 0,
  35. 0, 0, 0,
  36. 0, 0, 0},
  37. { 3, // Ver.3
  38. 70, 55, 44, 34, 26,
  39. 1, 22, 0, 0, 0, 0, 0,
  40. 1, 70, 55,
  41. 1, 70, 44,
  42. 2, 35, 17,
  43. 2, 35, 13,
  44. 0, 0, 0,
  45. 0, 0, 0,
  46. 0, 0, 0,
  47. 0, 0, 0},
  48. { 4, // Ver.4
  49. 100, 80, 64, 48, 36,
  50. 1, 26, 0, 0, 0, 0, 0,
  51. 1, 100, 80,
  52. 2, 50, 32,
  53. 2, 50, 24,
  54. 4, 25, 9,
  55. 0, 0, 0,
  56. 0, 0, 0,
  57. 0, 0, 0,
  58. 0, 0, 0},
  59. { 5, // Ver.5
  60. 134, 108, 86, 62, 46,
  61. 1, 30, 0, 0, 0, 0, 0,
  62. 1, 134, 108,
  63. 2, 67, 43,
  64. 2, 33, 15,
  65. 2, 33, 11,
  66. 0, 0, 0,
  67. 0, 0, 0,
  68. 2, 34, 16,
  69. 2, 34, 12},
  70. { 6, // Ver.6
  71. 172, 136, 108, 76, 60,
  72. 1, 34, 0, 0, 0, 0, 0,
  73. 2, 86, 68,
  74. 4, 43, 27,
  75. 4, 43, 19,
  76. 4, 43, 15,
  77. 0, 0, 0,
  78. 0, 0, 0,
  79. 0, 0, 0,
  80. 0, 0, 0},
  81. { 7, // Ver.7
  82. 196, 156, 124, 88, 66,
  83. 2, 22, 38, 0, 0, 0, 0,
  84. 2, 98, 78,
  85. 4, 49, 31,
  86. 2, 32, 14,
  87. 4, 39, 13,
  88. 0, 0, 0,
  89. 0, 0, 0,
  90. 4, 33, 15,
  91. 1, 40, 14},
  92. { 8, // Ver.8
  93. 242, 194, 154, 110, 86,
  94. 2, 24, 42, 0, 0, 0, 0,
  95. 2, 121, 97,
  96. 2, 60, 38,
  97. 4, 40, 18,
  98. 4, 40, 14,
  99. 0, 0, 0,
  100. 2, 61, 39,
  101. 2, 41, 19,
  102. 2, 41, 15},
  103. { 9, // Ver.9
  104. 292, 232, 182, 132, 100,
  105. 2, 26, 46, 0, 0, 0, 0,
  106. 2, 146, 116,
  107. 3, 58, 36,
  108. 4, 36, 16,
  109. 4, 36, 12,
  110. 0, 0, 0,
  111. 2, 59, 37,
  112. 4, 37, 17,
  113. 4, 37, 13},
  114. {10, // Ver.10
  115. 346, 274, 216, 154, 122,
  116. 2, 28, 50, 0, 0, 0, 0,
  117. 2, 86, 68,
  118. 4, 69, 43,
  119. 6, 43, 19,
  120. 6, 43, 15,
  121. 2, 87, 69,
  122. 1, 70, 44,
  123. 2, 44, 20,
  124. 2, 44, 16},
  125. {11, // Ver.11
  126. 404, 324, 254, 180, 140,
  127. 2, 30, 54, 0, 0, 0, 0,
  128. 4, 101, 81,
  129. 1, 80, 50,
  130. 4, 50, 22,
  131. 3, 36, 12,
  132. 0, 0, 0,
  133. 4, 81, 51,
  134. 4, 51, 23,
  135. 8, 37, 13},
  136. {12, // Ver.12
  137. 466, 370, 290, 206, 158,
  138. 2, 32, 58, 0, 0, 0, 0,
  139. 2, 116, 92,
  140. 6, 58, 36,
  141. 4, 46, 20,
  142. 7, 42, 14,
  143. 2, 117, 93,
  144. 2, 59, 37,
  145. 6, 47, 21,
  146. 4, 43, 15},
  147. {13, // Ver.13
  148. 532, 428, 334, 244, 180,
  149. 2, 34, 62, 0, 0, 0, 0,
  150. 4, 133, 107,
  151. 8, 59, 37,
  152. 8, 44, 20,
  153. 12, 33, 11,
  154. 0, 0, 0,
  155. 1, 60, 38,
  156. 4, 45, 21,
  157. 4, 34, 12},
  158. {14, // Ver.14
  159. 581, 461, 365, 261, 197,
  160. 3, 26, 46, 66, 0, 0, 0,
  161. 3, 145, 115,
  162. 4, 64, 40,
  163. 11, 36, 16,
  164. 11, 36, 12,
  165. 1, 146, 116,
  166. 5, 65, 41,
  167. 5, 37, 17,
  168. 5, 37, 13},
  169. {15, // Ver.15
  170. 655, 523, 415, 295, 223,
  171. 3, 26, 48, 70, 0, 0, 0,
  172. 5, 109, 87,
  173. 5, 65, 41,
  174. 5, 54, 24,
  175. 11, 36, 12,
  176. 1, 110, 88,
  177. 5, 66, 42,
  178. 7, 55, 25,
  179. 7, 37, 13},
  180. {16, // Ver.16
  181. 733, 589, 453, 325, 253,
  182. 3, 26, 50, 74, 0, 0, 0,
  183. 5, 122, 98,
  184. 7, 73, 45,
  185. 15, 43, 19,
  186. 3, 45, 15,
  187. 1, 123, 99,
  188. 3, 74, 46,
  189. 2, 44, 20,
  190. 13, 46, 16},
  191. {17, // Ver.17
  192. 815, 647, 507, 367, 283,
  193. 3, 30, 54, 78, 0, 0, 0,
  194. 1, 135, 107,
  195. 10, 74, 46,
  196. 1, 50, 22,
  197. 2, 42, 14,
  198. 5, 136, 108,
  199. 1, 75, 47,
  200. 15, 51, 23,
  201. 17, 43, 15},
  202. {18, // Ver.18
  203. 901, 721, 563, 397, 313,
  204. 3, 30, 56, 82, 0, 0, 0,
  205. 5, 150, 120,
  206. 9, 69, 43,
  207. 17, 50, 22,
  208. 2, 42, 14,
  209. 1, 151, 121,
  210. 4, 70, 44,
  211. 1, 51, 23,
  212. 19, 43, 15},
  213. {19, // Ver.19
  214. 991, 795, 627, 445, 341,
  215. 3, 30, 58, 86, 0, 0, 0,
  216. 3, 141, 113,
  217. 3, 70, 44,
  218. 17, 47, 21,
  219. 9, 39, 13,
  220. 4, 142, 114,
  221. 11, 71, 45,
  222. 4, 48, 22,
  223. 16, 40, 14},
  224. {20, // Ver.20
  225. 1085, 861, 669, 485, 385,
  226. 3, 34, 62, 90, 0, 0, 0,
  227. 3, 135, 107,
  228. 3, 67, 41,
  229. 15, 54, 24,
  230. 15, 43, 15,
  231. 5, 136, 108,
  232. 13, 68, 42,
  233. 5, 55, 25,
  234. 10, 44, 16},
  235. {21, // Ver.21
  236. 1156, 932, 714, 512, 406,
  237. 4, 28, 50, 72, 94, 0, 0,
  238. 4, 144, 116,
  239. 17, 68, 42,
  240. 17, 50, 22,
  241. 19, 46, 16,
  242. 4, 145, 117,
  243. 0, 0, 0,
  244. 6, 51, 23,
  245. 6, 47, 17},
  246. {22, // Ver.22
  247. 1258, 1006, 782, 568, 442,
  248. 4, 26, 50, 74, 98, 0, 0,
  249. 2, 139, 111,
  250. 17, 74, 46,
  251. 7, 54, 24,
  252. 34, 37, 13,
  253. 7, 140, 112,
  254. 0, 0, 0,
  255. 16, 55, 25,
  256. 0, 0, 0},
  257. {23, // Ver.23
  258. 1364, 1094, 860, 614, 464,
  259. 4, 30, 54, 78, 102, 0, 0,
  260. 4, 151, 121,
  261. 4, 75, 47,
  262. 11, 54, 24,
  263. 16, 45, 15,
  264. 5, 152, 122,
  265. 14, 76, 48,
  266. 14, 55, 25,
  267. 14, 46, 16},
  268. {24, // Ver.24
  269. 1474, 1174, 914, 664, 514,
  270. 4, 28, 54, 80, 106, 0, 0,
  271. 6, 147, 117,
  272. 6, 73, 45,
  273. 11, 54, 24,
  274. 30, 46, 16,
  275. 4, 148, 118,
  276. 14, 74, 46,
  277. 16, 55, 25,
  278. 2, 47, 17},
  279. {25, // Ver.25
  280. 1588, 1276, 1000, 718, 538,
  281. 4, 32, 58, 84, 110, 0, 0,
  282. 8, 132, 106,
  283. 8, 75, 47,
  284. 7, 54, 24,
  285. 22, 45, 15,
  286. 4, 133, 107,
  287. 13, 76, 48,
  288. 22, 55, 25,
  289. 13, 46, 16},
  290. {26, // Ver.26
  291. 1706, 1370, 1062, 754, 596,
  292. 4, 30, 58, 86, 114, 0, 0,
  293. 10, 142, 114,
  294. 19, 74, 46,
  295. 28, 50, 22,
  296. 33, 46, 16,
  297. 2, 143, 115,
  298. 4, 75, 47,
  299. 6, 51, 23,
  300. 4, 47, 17},
  301. {27, // Ver.27
  302. 1828, 1468, 1128, 808, 628,
  303. 4, 34, 62, 90, 118, 0, 0,
  304. 8, 152, 122,
  305. 22, 73, 45,
  306. 8, 53, 23,
  307. 12, 45, 15,
  308. 4, 153, 123,
  309. 3, 74, 46,
  310. 26, 54, 24,
  311. 28, 46, 16},
  312. {28, // Ver.28
  313. 1921, 1531, 1193, 871, 661,
  314. 5, 26, 50, 74, 98, 122, 0,
  315. 3, 147, 117,
  316. 3, 73, 45,
  317. 4, 54, 24,
  318. 11, 45, 15,
  319. 10, 148, 118,
  320. 23, 74, 46,
  321. 31, 55, 25,
  322. 31, 46, 16},
  323. {29, // Ver.29
  324. 2051, 1631, 1267, 911, 701,
  325. 5, 30, 54, 78, 102, 126, 0,
  326. 7, 146, 116,
  327. 21, 73, 45,
  328. 1, 53, 23,
  329. 19, 45, 15,
  330. 7, 147, 117,
  331. 7, 74, 46,
  332. 37, 54, 24,
  333. 26, 46, 16},
  334. {30, // Ver.30
  335. 2185, 1735, 1373, 985, 745,
  336. 5, 26, 52, 78, 104, 130, 0,
  337. 5, 145, 115,
  338. 19, 75, 47,
  339. 15, 54, 24,
  340. 23, 45, 15,
  341. 10, 146, 116,
  342. 10, 76, 48,
  343. 25, 55, 25,
  344. 25, 46, 16},
  345. {31, // Ver.31
  346. 2323, 1843, 1455, 1033, 793,
  347. 5, 30, 56, 82, 108, 134, 0,
  348. 13, 145, 115,
  349. 2, 74, 46,
  350. 42, 54, 24,
  351. 23, 45, 15,
  352. 3, 146, 116,
  353. 29, 75, 47,
  354. 1, 55, 25,
  355. 28, 46, 16},
  356. {32, // Ver.32
  357. 2465, 1955, 1541, 1115, 845,
  358. 5, 34, 60, 86, 112, 138, 0,
  359. 17, 145, 115,
  360. 10, 74, 46,
  361. 10, 54, 24,
  362. 19, 45, 15,
  363. 0, 0, 0,
  364. 23, 75, 47,
  365. 35, 55, 25,
  366. 35, 46, 16},
  367. {33, // Ver.33
  368. 2611, 2071, 1631, 1171, 901,
  369. 5, 30, 58, 86, 114, 142, 0,
  370. 17, 145, 115,
  371. 14, 74, 46,
  372. 29, 54, 24,
  373. 11, 45, 15,
  374. 1, 146, 116,
  375. 21, 75, 47,
  376. 19, 55, 25,
  377. 46, 46, 16},
  378. {34, // Ver.34
  379. 2761, 2191, 1725, 1231, 961,
  380. 5, 34, 62, 90, 118, 146, 0,
  381. 13, 145, 115,
  382. 14, 74, 46,
  383. 44, 54, 24,
  384. 59, 46, 16,
  385. 6, 146, 116,
  386. 23, 75, 47,
  387. 7, 55, 25,
  388. 1, 47, 17},
  389. {35, // Ver.35
  390. 2876, 2306, 1812, 1286, 986,
  391. 6, 30, 54, 78, 102, 126, 150,
  392. 12, 151, 121,
  393. 12, 75, 47,
  394. 39, 54, 24,
  395. 22, 45, 15,
  396. 7, 152, 122,
  397. 26, 76, 48,
  398. 14, 55, 25,
  399. 41, 46, 16},
  400. {36, // Ver.36
  401. 3034, 2434, 1914, 1354, 1054,
  402. 6, 24, 50, 76, 102, 128, 154,
  403. 6, 151, 121,
  404. 6, 75, 47,
  405. 46, 54, 24,
  406. 2, 45, 15,
  407. 14, 152, 122,
  408. 34, 76, 48,
  409. 10, 55, 25,
  410. 64, 46, 16},
  411. {37, // Ver.37
  412. 3196, 2566, 1992, 1426, 1096,
  413. 6, 28, 54, 80, 106, 132, 158,
  414. 17, 152, 122,
  415. 29, 74, 46,
  416. 49, 54, 24,
  417. 24, 45, 15,
  418. 4, 153, 123,
  419. 14, 75, 47,
  420. 10, 55, 25,
  421. 46, 46, 16},
  422. {38, // Ver.38
  423. 3362, 2702, 2102, 1502, 1142,
  424. 6, 32, 58, 84, 110, 136, 162,
  425. 4, 152, 122,
  426. 13, 74, 46,
  427. 48, 54, 24,
  428. 42, 45, 15,
  429. 18, 153, 123,
  430. 32, 75, 47,
  431. 14, 55, 25,
  432. 32, 46, 16},
  433. {39, // Ver.39
  434. 3532, 2812, 2216, 1582, 1222,
  435. 6, 26, 54, 82, 110, 138, 166,
  436. 20, 147, 117,
  437. 40, 75, 47,
  438. 43, 54, 24,
  439. 10, 45, 15,
  440. 4, 148, 118,
  441. 7, 76, 48,
  442. 22, 55, 25,
  443. 67, 46, 16},
  444. {40, // Ver.40
  445. 3706, 2956, 2334, 1666, 1276,
  446. 6, 30, 58, 86, 114, 142, 170,
  447. 19, 148, 118,
  448. 18, 75, 47,
  449. 34, 54, 24,
  450. 20, 45, 15,
  451. 6, 149, 119,
  452. 31, 76, 48,
  453. 34, 55, 25,
  454. 61, 46, 16}
  455. };
  456. /////////////////////////////////////////////////////////////////////////////
  457. //GF(2^8)α指数→整数变换桌子
  458. static BYTE byExpToInt[] = { 1, 2, 4, 8, 16, 32, 64, 128, 29, 58, 116, 232, 205, 135, 19, 38,
  459. 76, 152, 45, 90, 180, 117, 234, 201, 143, 3, 6, 12, 24, 48, 96, 192,
  460. 157, 39, 78, 156, 37, 74, 148, 53, 106, 212, 181, 119, 238, 193, 159, 35,
  461. 70, 140, 5, 10, 20, 40, 80, 160, 93, 186, 105, 210, 185, 111, 222, 161,
  462. 95, 190, 97, 194, 153, 47, 94, 188, 101, 202, 137, 15, 30, 60, 120, 240,
  463. 253, 231, 211, 187, 107, 214, 177, 127, 254, 225, 223, 163, 91, 182, 113, 226,
  464. 217, 175, 67, 134, 17, 34, 68, 136, 13, 26, 52, 104, 208, 189, 103, 206,
  465. 129, 31, 62, 124, 248, 237, 199, 147, 59, 118, 236, 197, 151, 51, 102, 204,
  466. 133, 23, 46, 92, 184, 109, 218, 169, 79, 158, 33, 66, 132, 21, 42, 84,
  467. 168, 77, 154, 41, 82, 164, 85, 170, 73, 146, 57, 114, 228, 213, 183, 115,
  468. 230, 209, 191, 99, 198, 145, 63, 126, 252, 229, 215, 179, 123, 246, 241, 255,
  469. 227, 219, 171, 75, 150, 49, 98, 196, 149, 55, 110, 220, 165, 87, 174, 65,
  470. 130, 25, 50, 100, 200, 141, 7, 14, 28, 56, 112, 224, 221, 167, 83, 166,
  471. 81, 162, 89, 178, 121, 242, 249, 239, 195, 155, 43, 86, 172, 69, 138, 9,
  472. 18, 36, 72, 144, 61, 122, 244, 245, 247, 243, 251, 235, 203, 139, 11, 22,
  473. 44, 88, 176, 125, 250, 233, 207, 131, 27, 54, 108, 216, 173, 71, 142, 1};
  474. /////////////////////////////////////////////////////////////////////////////
  475. // GF(2^8)α整数→指数变换桌子
  476. static BYTE byIntToExp[] = { 0, 0, 1, 25, 2, 50, 26, 198, 3, 223, 51, 238, 27, 104, 199, 75,
  477. 4, 100, 224, 14, 52, 141, 239, 129, 28, 193, 105, 248, 200, 8, 76, 113,
  478. 5, 138, 101, 47, 225, 36, 15, 33, 53, 147, 142, 218, 240, 18, 130, 69,
  479. 29, 181, 194, 125, 106, 39, 249, 185, 201, 154, 9, 120, 77, 228, 114, 166,
  480. 6, 191, 139, 98, 102, 221, 48, 253, 226, 152, 37, 179, 16, 145, 34, 136,
  481. 54, 208, 148, 206, 143, 150, 219, 189, 241, 210, 19, 92, 131, 56, 70, 64,
  482. 30, 66, 182, 163, 195, 72, 126, 110, 107, 58, 40, 84, 250, 133, 186, 61,
  483. 202, 94, 155, 159, 10, 21, 121, 43, 78, 212, 229, 172, 115, 243, 167, 87,
  484. 7, 112, 192, 247, 140, 128, 99, 13, 103, 74, 222, 237, 49, 197, 254, 24,
  485. 227, 165, 153, 119, 38, 184, 180, 124, 17, 68, 146, 217, 35, 32, 137, 46,
  486. 55, 63, 209, 91, 149, 188, 207, 205, 144, 135, 151, 178, 220, 252, 190, 97,
  487. 242, 86, 211, 171, 20, 42, 93, 158, 132, 60, 57, 83, 71, 109, 65, 162,
  488. 31, 45, 67, 216, 183, 123, 164, 118, 196, 23, 73, 236, 127, 12, 111, 246,
  489. 108, 161, 59, 82, 41, 157, 85, 170, 251, 96, 134, 177, 187, 204, 62, 90,
  490. 203, 89, 95, 176, 156, 169, 160, 81, 11, 245, 22, 235, 122, 117, 44, 215,
  491. 79, 174, 213, 233, 230, 231, 173, 232, 116, 214, 244, 234, 168, 80, 88, 175};
  492. /////////////////////////////////////////////////////////////////////////////
  493. // 错误订正生成多项式α系数;
  494. static BYTE byRSExp7[] = {87, 229, 146, 149, 238, 102, 21};
  495. static BYTE byRSExp10[] = {251, 67, 46, 61, 118, 70, 64, 94, 32, 45};
  496. static BYTE byRSExp13[] = { 74, 152, 176, 100, 86, 100, 106, 104, 130, 218, 206, 140, 78};
  497. static BYTE byRSExp15[] = { 8, 183, 61, 91, 202, 37, 51, 58, 58, 237, 140, 124, 5, 99, 105};
  498. static BYTE byRSExp16[] = {120, 104, 107, 109, 102, 161, 76, 3, 91, 191, 147, 169, 182, 194, 225, 120};
  499. static BYTE byRSExp17[] = { 43, 139, 206, 78, 43, 239, 123, 206, 214, 147, 24, 99, 150, 39, 243, 163, 136};
  500. static BYTE byRSExp18[] = {215, 234, 158, 94, 184, 97, 118, 170, 79, 187, 152, 148, 252, 179, 5, 98, 96, 153};
  501. static BYTE byRSExp20[] = { 17, 60, 79, 50, 61, 163, 26, 187, 202, 180, 221, 225, 83, 239, 156, 164, 212, 212, 188, 190};
  502. static BYTE byRSExp22[] = {210, 171, 247, 242, 93, 230, 14, 109, 221, 53, 200, 74, 8, 172, 98, 80, 219, 134, 160, 105,
  503. 165, 231};
  504. static BYTE byRSExp24[] = {229, 121, 135, 48, 211, 117, 251, 126, 159, 180, 169, 152, 192, 226, 228, 218, 111, 0, 117, 232,
  505. 87, 96, 227, 21};
  506. static BYTE byRSExp26[] = {173, 125, 158, 2, 103, 182, 118, 17, 145, 201, 111, 28, 165, 53, 161, 21, 245, 142, 13, 102,
  507. 48, 227, 153, 145, 218, 70};
  508. static BYTE byRSExp28[] = {168, 223, 200, 104, 224, 234, 108, 180, 110, 190, 195, 147, 205, 27, 232, 201, 21, 43, 245, 87,
  509. 42, 195, 212, 119, 242, 37, 9, 123};
  510. static BYTE byRSExp30[] = { 41, 173, 145, 152, 216, 31, 179, 182, 50, 48, 110, 86, 239, 96, 222, 125, 42, 173, 226, 193,
  511. 224, 130, 156, 37, 251, 216, 238, 40, 192, 180};
  512. static BYTE byRSExp32[] = { 10, 6, 106, 190, 249, 167, 4, 67, 209, 138, 138, 32, 242, 123, 89, 27, 120, 185, 80, 156,
  513. 38, 69, 171, 60, 28, 222, 80, 52, 254, 185, 220, 241};
  514. static BYTE byRSExp34[] = {111, 77, 146, 94, 26, 21, 108, 19, 105, 94, 113, 193, 86, 140, 163, 125, 58, 158, 229, 239,
  515. 218, 103, 56, 70, 114, 61, 183, 129, 167, 13, 98, 62, 129, 51};
  516. static BYTE byRSExp36[] = {200, 183, 98, 16, 172, 31, 246, 234, 60, 152, 115, 0, 167, 152, 113, 248, 238, 107, 18, 63,
  517. 218, 37, 87, 210, 105, 177, 120, 74, 121, 196, 117, 251, 113, 233, 30, 120};
  518. static BYTE byRSExp38[] = {159, 34, 38, 228, 230, 59, 243, 95, 49, 218, 176, 164, 20, 65, 45, 111, 39, 81, 49, 118,
  519. 113, 222, 193, 250, 242, 168, 217, 41, 164, 247, 177, 30, 238, 18, 120, 153, 60, 193};
  520. static BYTE byRSExp40[] = { 59, 116, 79, 161, 252, 98, 128, 205, 128, 161, 247, 57, 163, 56, 235, 106, 53, 26, 187, 174,
  521. 226, 104, 170, 7, 175, 35, 181, 114, 88, 41, 47, 163, 125, 134, 72, 20, 232, 53, 35, 15};
  522. static BYTE byRSExp42[] = {250, 103, 221, 230, 25, 18, 137, 231, 0, 3, 58, 242, 221, 191, 110, 84, 230, 8, 188, 106,
  523. 96, 147, 15, 131, 139, 34, 101, 223, 39, 101, 213, 199, 237, 254, 201, 123, 171, 162, 194, 117,
  524. 50, 96};
  525. static BYTE byRSExp44[] = {190, 7, 61, 121, 71, 246, 69, 55, 168, 188, 89, 243, 191, 25, 72, 123, 9, 145, 14, 247,
  526. 1, 238, 44, 78, 143, 62, 224, 126, 118, 114, 68, 163, 52, 194, 217, 147, 204, 169, 37, 130,
  527. 113, 102, 73, 181};
  528. static BYTE byRSExp46[] = {112, 94, 88, 112, 253, 224, 202, 115, 187, 99, 89, 5, 54, 113, 129, 44, 58, 16, 135, 216,
  529. 169, 211, 36, 1, 4, 96, 60, 241, 73, 104, 234, 8, 249, 245, 119, 174, 52, 25, 157, 224,
  530. 43, 202, 223, 19, 82, 15};
  531. static BYTE byRSExp48[] = {228, 25, 196, 130, 211, 146, 60, 24, 251, 90, 39, 102, 240, 61, 178, 63, 46, 123, 115, 18,
  532. 221, 111, 135, 160, 182, 205, 107, 206, 95, 150, 120, 184, 91, 21, 247, 156, 140, 238, 191, 11,
  533. 94, 227, 84, 50, 163, 39, 34, 108};
  534. static BYTE byRSExp50[] = {232, 125, 157, 161, 164, 9, 118, 46, 209, 99, 203, 193, 35, 3, 209, 111, 195, 242, 203, 225,
  535. 46, 13, 32, 160, 126, 209, 130, 160, 242, 215, 242, 75, 77, 42, 189, 32, 113, 65, 124, 69,
  536. 228, 114, 235, 175, 124, 170, 215, 232, 133, 205};
  537. static BYTE byRSExp52[] = {116, 50, 86, 186, 50, 220, 251, 89, 192, 46, 86, 127, 124, 19, 184, 233, 151, 215, 22, 14,
  538. 59, 145, 37, 242, 203, 134, 254, 89, 190, 94, 59, 65, 124, 113, 100, 233, 235, 121, 22, 76,
  539. 86, 97, 39, 242, 200, 220, 101, 33, 239, 254, 116, 51};
  540. static BYTE byRSExp54[] = {183, 26, 201, 87, 210, 221, 113, 21, 46, 65, 45, 50, 238, 184, 249, 225, 102, 58, 209, 218,
  541. 109, 165, 26, 95, 184, 192, 52, 245, 35, 254, 238, 175, 172, 79, 123, 25, 122, 43, 120, 108,
  542. 215, 80, 128, 201, 235, 8, 153, 59, 101, 31, 198, 76, 31, 156};
  543. static BYTE byRSExp56[] = {106, 120, 107, 157, 164, 216, 112, 116, 2, 91, 248, 163, 36, 201, 202, 229, 6, 144, 254, 155,
  544. 135, 208, 170, 209, 12, 139, 127, 142, 182, 249, 177, 174, 190, 28, 10, 85, 239, 184, 101, 124,
  545. 152, 206, 96, 23, 163, 61, 27, 196, 247, 151, 154, 202, 207, 20, 61, 10};
  546. static BYTE byRSExp58[] = { 82, 116, 26, 247, 66, 27, 62, 107, 252, 182, 200, 185, 235, 55, 251, 242, 210, 144, 154, 237,
  547. 176, 141, 192, 248, 152, 249, 206, 85, 253, 142, 65, 165, 125, 23, 24, 30, 122, 240, 214, 6,
  548. 129, 218, 29, 145, 127, 134, 206, 245, 117, 29, 41, 63, 159, 142, 233, 125, 148, 123};
  549. static BYTE byRSExp60[] = {107, 140, 26, 12, 9, 141, 243, 197, 226, 197, 219, 45, 211, 101, 219, 120, 28, 181, 127, 6,
  550. 100, 247, 2, 205, 198, 57, 115, 219, 101, 109, 160, 82, 37, 38, 238, 49, 160, 209, 121, 86,
  551. 11, 124, 30, 181, 84, 25, 194, 87, 65, 102, 190, 220, 70, 27, 209, 16, 89, 7, 33, 240};
  552. static BYTE byRSExp62[] = { 65, 202, 113, 98, 71, 223, 248, 118, 214, 94, 0, 122, 37, 23, 2, 228, 58, 121, 7, 105,
  553. 135, 78, 243, 118, 70, 76, 223, 89, 72, 50, 70, 111, 194, 17, 212, 126, 181, 35, 221, 117,
  554. 235, 11, 229, 149, 147, 123, 213, 40, 115, 6, 200, 100, 26, 246, 182, 218, 127, 215, 36, 186,
  555. 110, 106};
  556. static BYTE byRSExp64[] = { 45, 51, 175, 9, 7, 158, 159, 49, 68, 119, 92, 123, 177, 204, 187, 254, 200, 78, 141, 149,
  557. 119, 26, 127, 53, 160, 93, 199, 212, 29, 24, 145, 156, 208, 150, 218, 209, 4, 216, 91, 47,
  558. 184, 146, 47, 140, 195, 195, 125, 242, 238, 63, 99, 108, 140, 230, 242, 31, 204, 11, 178, 243,
  559. 217, 156, 213, 231};
  560. static BYTE byRSExp66[] = { 5, 118, 222, 180, 136, 136, 162, 51, 46, 117, 13, 215, 81, 17, 139, 247, 197, 171, 95, 173,
  561. 65, 137, 178, 68, 111, 95, 101, 41, 72, 214, 169, 197, 95, 7, 44, 154, 77, 111, 236, 40,
  562. 121, 143, 63, 87, 80, 253, 240, 126, 217, 77, 34, 232, 106, 50, 168, 82, 76, 146, 67, 106,
  563. 171, 25, 132, 93, 45, 105};
  564. static BYTE byRSExp68[] = {247, 159, 223, 33, 224, 93, 77, 70, 90, 160, 32, 254, 43, 150, 84, 101, 190, 205, 133, 52,
  565. 60, 202, 165, 220, 203, 151, 93, 84, 15, 84, 253, 173, 160, 89, 227, 52, 199, 97, 95, 231,
  566. 52, 177, 41, 125, 137, 241, 166, 225, 118, 2, 54, 32, 82, 215, 175, 198, 43, 238, 235, 27,
  567. 101, 184, 127, 3, 5, 8, 163, 238};
  568. static LPBYTE byRSExp[] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, byRSExp7, NULL, NULL,
  569. byRSExp10, NULL, NULL, byRSExp13, NULL, byRSExp15, byRSExp16, byRSExp17, byRSExp18, NULL,
  570. byRSExp20, NULL, byRSExp22, NULL, byRSExp24, NULL, byRSExp26, NULL, byRSExp28, NULL,
  571. byRSExp30, NULL, byRSExp32, NULL, byRSExp34, NULL, byRSExp36, NULL, byRSExp38, NULL,
  572. byRSExp40, NULL, byRSExp42, NULL, byRSExp44, NULL, byRSExp46, NULL, byRSExp48, NULL,
  573. byRSExp50, NULL, byRSExp52, NULL, byRSExp54, NULL, byRSExp56, NULL, byRSExp58, NULL,
  574. byRSExp60, NULL, byRSExp62, NULL, byRSExp64, NULL, byRSExp66, NULL, byRSExp68};
  575. // 文字数indicator位长(按版本小组,{S, M, L})
  576. static int nIndicatorLenNumeral[] = {10, 12, 14};
  577. static int nIndicatorLenAlphabet[] = { 9, 11, 13};
  578. static int nIndicatorLen8Bit[] = { 8, 16, 16};
  579. static int nIndicatorLenKanji[] = { 8, 10, 12};
  580. /////////////////////////////////////////////////////////////////////////////
  581. CQR_Encode::CQR_Encode()
  582. {
  583. }
  584. CQR_Encode::~CQR_Encode()
  585. {
  586. }
  587. /////////////////////////////////////////////////////////////////////////////
  588. // CQR_Encode::EncodeData
  589. //事情途∶数据编码
  590. //引数∶错误订正水平,型号(0=自动),型号自动扩张标志,掩蔽号码(-1=自动),编码数据,编码数据长
  591. //返回值∶编码成功时=TRUE,没有数据,或容量超时=FALSE
  592. BOOL CQR_Encode::EncodeData(int nLevel, int nVersion, BOOL bAutoExtent, int nMaskingNo, LPCSTR lpsSource, int ncSource)
  593. {
  594. int i, j;
  595. m_nLevel = nLevel;
  596. m_nMaskingNo = nMaskingNo;
  597. // 数据长没被指定的情况由于 lstrlen 取得
  598. int ncLength = ncSource > 0 ? ncSource : lstrlen(lpsSource);
  599. if (ncLength == 0)
  600. return FALSE; // 没有数据
  601. // 版本(型号)检查
  602. int nEncodeVersion = GetEncodeVersion(nVersion, lpsSource, ncLength);
  603. if (nEncodeVersion == 0)
  604. return FALSE; // 容量超
  605. if (nVersion == 0)
  606. {
  607. // 型号自动
  608. m_nVersion = nEncodeVersion;
  609. }
  610. else
  611. {
  612. if (nEncodeVersion <= nVersion)
  613. {
  614. m_nVersion = nVersion;
  615. }
  616. else
  617. {
  618. if (bAutoExtent)
  619. m_nVersion = nEncodeVersion; // 版本(型号)自动扩张
  620. else
  621. return FALSE; // 容量超
  622. }
  623. }
  624. // 终结者编码"0000"补充
  625. int ncDataCodeWord = QR_VersonInfo[m_nVersion].ncDataCodeWord[nLevel];
  626. int ncTerminater = min(4, (ncDataCodeWord * 8) - m_ncDataCodeWordBit);
  627. if (ncTerminater > 0)
  628. m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, 0, ncTerminater);
  629. // 填充编码"11101100,00010001"补充
  630. BYTE byPaddingCode = 0xec;
  631. for (i = (m_ncDataCodeWordBit + 7) / 8; i < ncDataCodeWord; ++i)
  632. {
  633. m_byDataCodeWord[i] = byPaddingCode;
  634. byPaddingCode = (BYTE)(byPaddingCode == 0xec ? 0x11 : 0xec);
  635. }
  636. //总codeword算出区域清除
  637. m_ncAllCodeWord = QR_VersonInfo[m_nVersion].ncAllCodeWord;
  638. ZeroMemory(m_byAllCodeWord, m_ncAllCodeWord);
  639. int nDataCwIndex = 0; // data codeWord处理位置
  640. // 数据块分割数
  641. int ncBlock1 = QR_VersonInfo[m_nVersion].RS_BlockInfo1[nLevel].ncRSBlock;
  642. int ncBlock2 = QR_VersonInfo[m_nVersion].RS_BlockInfo2[nLevel].ncRSBlock;
  643. int ncBlockSum = ncBlock1 + ncBlock2;
  644. int nBlockNo = 0; // 处理中,块号码
  645. // 按块data code存储量
  646. int ncDataCw1 = QR_VersonInfo[m_nVersion].RS_BlockInfo1[nLevel].ncDataCodeWord;
  647. int ncDataCw2 = QR_VersonInfo[m_nVersion].RS_BlockInfo2[nLevel].ncDataCodeWord;
  648. // data codeWordinterleave配置
  649. for (i = 0; i < ncBlock1; ++i)
  650. {
  651. for (j = 0; j < ncDataCw1; ++j)
  652. {
  653. m_byAllCodeWord[(ncBlockSum * j) + nBlockNo] = m_byDataCodeWord[nDataCwIndex++];
  654. }
  655. ++nBlockNo;
  656. }
  657. for (i = 0; i < ncBlock2; ++i)
  658. {
  659. for (j = 0; j < ncDataCw2; ++j)
  660. {
  661. if (j < ncDataCw1)
  662. {
  663. m_byAllCodeWord[(ncBlockSum * j) + nBlockNo] = m_byDataCodeWord[nDataCwIndex++];
  664. }
  665. else
  666. {
  667. // 2种块零数份儿配置
  668. m_byAllCodeWord[(ncBlockSum * ncDataCw1) + i] = m_byDataCodeWord[nDataCwIndex++];
  669. }
  670. }
  671. ++nBlockNo;
  672. }
  673. // 按块RScodeword数(※现状同数)
  674. int ncRSCw1 = QR_VersonInfo[m_nVersion].RS_BlockInfo1[nLevel].ncAllCodeWord - ncDataCw1;
  675. int ncRSCw2 = QR_VersonInfo[m_nVersion].RS_BlockInfo2[nLevel].ncAllCodeWord - ncDataCw2;
  676. /////////////////////////////////////////////////////////////////////////
  677. // RScodeword算出
  678. nDataCwIndex = 0;
  679. nBlockNo = 0;
  680. for (i = 0; i < ncBlock1; ++i)
  681. {
  682. ZeroMemory(m_byRSWork, sizeof(m_byRSWork));
  683. memmove(m_byRSWork, m_byDataCodeWord + nDataCwIndex, ncDataCw1);
  684. GetRSCodeWord(m_byRSWork, ncDataCw1, ncRSCw1);
  685. // RScodeword配置
  686. for (j = 0; j < ncRSCw1; ++j)
  687. {
  688. m_byAllCodeWord[ncDataCodeWord + (ncBlockSum * j) + nBlockNo] = m_byRSWork[j];
  689. }
  690. nDataCwIndex += ncDataCw1;
  691. ++nBlockNo;
  692. }
  693. for (i = 0; i < ncBlock2; ++i)
  694. {
  695. ZeroMemory(m_byRSWork, sizeof(m_byRSWork));
  696. memmove(m_byRSWork, m_byDataCodeWord + nDataCwIndex, ncDataCw2);
  697. GetRSCodeWord(m_byRSWork, ncDataCw2, ncRSCw2);
  698. // RScodeword配置
  699. for (j = 0; j < ncRSCw2; ++j)
  700. {
  701. m_byAllCodeWord[ncDataCodeWord + (ncBlockSum * j) + nBlockNo] = m_byRSWork[j];
  702. }
  703. nDataCwIndex += ncDataCw2;
  704. ++nBlockNo;
  705. }
  706. m_nSymbleSize = m_nVersion * 4 + 17;
  707. // 组件配置
  708. FormatModule();
  709. return TRUE;
  710. }
  711. /////////////////////////////////////////////////////////////////////////////
  712. // CQR_Encode::GetEncodeVersion
  713. //事情途∶编码时版本(型号)取得
  714. //引数∶调查开始版本,编码数据,编码数据长
  715. //返回值∶版本号码(容量超时=0)
  716. int CQR_Encode::GetEncodeVersion(int nVersion, LPCSTR lpsSource, int ncLength)
  717. {
  718. int nVerGroup = nVersion >= 27 ? QR_VRESION_L : (nVersion >= 10 ? QR_VRESION_M : QR_VRESION_S);
  719. int i, j;
  720. for (i = nVerGroup; i <= QR_VRESION_L; ++i)
  721. {
  722. if (EncodeSourceData(lpsSource, ncLength, i))
  723. {
  724. if (i == QR_VRESION_S)
  725. {
  726. for (j = 1; j <= 9; ++j)
  727. {
  728. if ((m_ncDataCodeWordBit + 7) / 8 <= QR_VersonInfo[j].ncDataCodeWord[m_nLevel])
  729. return j;
  730. }
  731. }
  732. else if (i == QR_VRESION_M)
  733. {
  734. for (j = 10; j <= 26; ++j)
  735. {
  736. if ((m_ncDataCodeWordBit + 7) / 8 <= QR_VersonInfo[j].ncDataCodeWord[m_nLevel])
  737. return j;
  738. }
  739. }
  740. else if (i == QR_VRESION_L)
  741. {
  742. for (j = 27; j <= 40; ++j)
  743. {
  744. if ((m_ncDataCodeWordBit + 7) / 8 <= QR_VersonInfo[j].ncDataCodeWord[m_nLevel])
  745. return j;
  746. }
  747. }
  748. }
  749. }
  750. return 0;
  751. }
  752. /////////////////////////////////////////////////////////////////////////////
  753. // CQR_Encode::EncodeSourceData
  754. //事情途∶输入数据编码
  755. //引数∶输入数据,输入数据长,版本(型号)小组
  756. //返回值∶编码成功时=TRUE
  757. BOOL CQR_Encode::EncodeSourceData(LPCSTR lpsSource, int ncLength, int nVerGroup)
  758. {
  759. ZeroMemory(m_nBlockLength, sizeof(m_nBlockLength));
  760. int i, j;
  761. // 哪个模式几个字调查(字节)继续着
  762. for (m_ncDataBlock = i = 0; i < ncLength; ++i)
  763. {
  764. BYTE byMode;
  765. if (i < ncLength - 1 && IsKanjiData(lpsSource[i], lpsSource[i + 1]))
  766. byMode = QR_MODE_KANJI;
  767. else if (IsNumeralData(lpsSource[i]))
  768. byMode = QR_MODE_NUMERAL;
  769. else if (IsAlphabetData(lpsSource[i]))
  770. byMode = QR_MODE_ALPHABET;
  771. else
  772. byMode = QR_MODE_8BIT;
  773. if (i == 0)
  774. m_byBlockMode[0] = byMode;
  775. if (m_byBlockMode[m_ncDataBlock] != byMode)
  776. m_byBlockMode[++m_ncDataBlock] = byMode;
  777. ++m_nBlockLength[m_ncDataBlock];
  778. if (byMode == QR_MODE_KANJI)
  779. {
  780. //汉字不是文字数以数记录
  781. ++m_nBlockLength[m_ncDataBlock];
  782. ++i;
  783. }
  784. }
  785. ++m_ncDataBlock;
  786. /////////////////////////////////////////////////////////////////////////
  787. // 根据条件结合邻接的英数字模式块和数字模式块的排列wo
  788. int ncSrcBits, ncDstBits; // 与原来的位长单一的英数字模式块化做的情况的位长
  789. int nBlock = 0;
  790. while (nBlock < m_ncDataBlock - 1)
  791. {
  792. int ncJoinFront, ncJoinBehind; //前后8比特字节方式块和结合的情况的位长
  793. int nJoinPosition = 0; // 与8比特字节方式块的结合∶与-1=前结合,0=不结合,1=后面和结合
  794. // 「数字-英数字」或「英数字-数字」的排列
  795. if ((m_byBlockMode[nBlock] == QR_MODE_NUMERAL && m_byBlockMode[nBlock + 1] == QR_MODE_ALPHABET) ||
  796. (m_byBlockMode[nBlock] == QR_MODE_ALPHABET && m_byBlockMode[nBlock + 1] == QR_MODE_NUMERAL))
  797. {
  798. //比较与原来的位长单一的英数字模式块化做的情况的位长
  799. ncSrcBits = GetBitLength(m_byBlockMode[nBlock], m_nBlockLength[nBlock], nVerGroup) +
  800. GetBitLength(m_byBlockMode[nBlock + 1], m_nBlockLength[nBlock + 1], nVerGroup);
  801. ncDstBits = GetBitLength(QR_MODE_ALPHABET, m_nBlockLength[nBlock] + m_nBlockLength[nBlock + 1], nVerGroup);
  802. if (ncSrcBits > ncDstBits)
  803. {
  804. // 如果前后有8比特字节方式块,检查与那些的结合是不是有利
  805. if (nBlock >= 1 && m_byBlockMode[nBlock - 1] == QR_MODE_8BIT)
  806. {
  807. // 前有8比特字节方式块
  808. ncJoinFront = GetBitLength(QR_MODE_8BIT, m_nBlockLength[nBlock - 1] + m_nBlockLength[nBlock], nVerGroup) +
  809. GetBitLength(m_byBlockMode[nBlock + 1], m_nBlockLength[nBlock + 1], nVerGroup);
  810. if (ncJoinFront > ncDstBits + GetBitLength(QR_MODE_8BIT, m_nBlockLength[nBlock - 1], nVerGroup))
  811. ncJoinFront = 0; // 与8比特字节方式块不结合
  812. }
  813. else
  814. ncJoinFront = 0;
  815. if (nBlock < m_ncDataBlock - 2 && m_byBlockMode[nBlock + 2] == QR_MODE_8BIT)
  816. {
  817. //后面有8比特字节方式块
  818. ncJoinBehind = GetBitLength(m_byBlockMode[nBlock], m_nBlockLength[nBlock], nVerGroup) +
  819. GetBitLength(QR_MODE_8BIT, m_nBlockLength[nBlock + 1] + m_nBlockLength[nBlock + 2], nVerGroup);
  820. if (ncJoinBehind > ncDstBits + GetBitLength(QR_MODE_8BIT, m_nBlockLength[nBlock + 2], nVerGroup))
  821. ncJoinBehind = 0; // 与8比特字节方式块不结合
  822. }
  823. else
  824. ncJoinBehind = 0;
  825. if (ncJoinFront != 0 && ncJoinBehind != 0)
  826. {
  827. // 如果前后双方有8比特字节方式块优先数据长变得短
  828. nJoinPosition = (ncJoinFront < ncJoinBehind) ? -1 : 1;
  829. }
  830. else
  831. {
  832. nJoinPosition = (ncJoinFront != 0) ? -1 : ((ncJoinBehind != 0) ? 1 : 0);
  833. }
  834. if (nJoinPosition != 0)
  835. {
  836. // 与8比特字节方式块的结合
  837. if (nJoinPosition == -1)
  838. {
  839. m_nBlockLength[nBlock - 1] += m_nBlockLength[nBlock];
  840. // 移动后续
  841. for (i = nBlock; i < m_ncDataBlock - 1; ++i)
  842. {
  843. m_byBlockMode[i] = m_byBlockMode[i + 1];
  844. m_nBlockLength[i] = m_nBlockLength[i + 1];
  845. }
  846. }
  847. else
  848. {
  849. m_byBlockMode[nBlock + 1] = QR_MODE_8BIT;
  850. m_nBlockLength[nBlock + 1] += m_nBlockLength[nBlock + 2];
  851. // 移动后续
  852. for (i = nBlock + 2; i < m_ncDataBlock - 1; ++i)
  853. {
  854. m_byBlockMode[i] = m_byBlockMode[i + 1];
  855. m_nBlockLength[i] = m_nBlockLength[i + 1];
  856. }
  857. }
  858. --m_ncDataBlock;
  859. }
  860. else
  861. {
  862. // 为单一的英数字模式块合并英数字和数字的排列
  863. if (nBlock < m_ncDataBlock - 2 && m_byBlockMode[nBlock + 2] == QR_MODE_ALPHABET)
  864. {
  865. // 结合接连到打算结合的块后面的英数字模式块
  866. m_nBlockLength[nBlock + 1] += m_nBlockLength[nBlock + 2];
  867. // 移动后续
  868. for (i = nBlock + 2; i < m_ncDataBlock - 1; ++i)
  869. {
  870. m_byBlockMode[i] = m_byBlockMode[i + 1];
  871. m_nBlockLength[i] = m_nBlockLength[i + 1];
  872. }
  873. --m_ncDataBlock;
  874. }
  875. m_byBlockMode[nBlock] = QR_MODE_ALPHABET;
  876. m_nBlockLength[nBlock] += m_nBlockLength[nBlock + 1];
  877. // 移动后续
  878. for (i = nBlock + 1; i < m_ncDataBlock - 1; ++i)
  879. {
  880. m_byBlockMode[i] = m_byBlockMode[i + 1];
  881. m_nBlockLength[i] = m_nBlockLength[i + 1];
  882. }
  883. --m_ncDataBlock;
  884. if (nBlock >= 1 && m_byBlockMode[nBlock - 1] == QR_MODE_ALPHABET)
  885. {
  886. //结合结合的块前面的英数字模式块
  887. m_nBlockLength[nBlock - 1] += m_nBlockLength[nBlock];
  888. // 移动后续
  889. for (i = nBlock; i < m_ncDataBlock - 1; ++i)
  890. {
  891. m_byBlockMode[i] = m_byBlockMode[i + 1];
  892. m_nBlockLength[i] = m_nBlockLength[i + 1];
  893. }
  894. --m_ncDataBlock;
  895. }
  896. }
  897. continue; // 重新调查现在位置的块
  898. }
  899. }
  900. ++nBlock; // 调查下面块
  901. }
  902. /////////////////////////////////////////////////////////////////////////
  903. // 接连的8比特字节方式块化短的模式块
  904. nBlock = 0;
  905. while (nBlock < m_ncDataBlock - 1)
  906. {
  907. ncSrcBits = GetBitLength(m_byBlockMode[nBlock], m_nBlockLength[nBlock], nVerGroup)
  908. + GetBitLength(m_byBlockMode[nBlock + 1], m_nBlockLength[nBlock + 1], nVerGroup);
  909. ncDstBits = GetBitLength(QR_MODE_8BIT, m_nBlockLength[nBlock] + m_nBlockLength[nBlock + 1], nVerGroup);
  910. // 如果前有8比特字节方式块,减法重复的indicator份儿
  911. if (nBlock >= 1 && m_byBlockMode[nBlock - 1] == QR_MODE_8BIT)
  912. ncDstBits -= (4 + nIndicatorLen8Bit[nVerGroup]);
  913. // 如果后面有8比特字节方式块,减法重复的indicator份儿
  914. if (nBlock < m_ncDataBlock - 2 && m_byBlockMode[nBlock + 2] == QR_MODE_8BIT)
  915. ncDstBits -= (4 + nIndicatorLen8Bit[nVerGroup]);
  916. if (ncSrcBits > ncDstBits)
  917. {
  918. if (nBlock >= 1 && m_byBlockMode[nBlock - 1] == QR_MODE_8BIT)
  919. {
  920. // 结合结合的块前有的8比特字节方式块
  921. m_nBlockLength[nBlock - 1] += m_nBlockLength[nBlock];
  922. // 移动后续
  923. for (i = nBlock; i < m_ncDataBlock - 1; ++i)
  924. {
  925. m_byBlockMode[i] = m_byBlockMode[i + 1];
  926. m_nBlockLength[i] = m_nBlockLength[i + 1];
  927. }
  928. --m_ncDataBlock;
  929. --nBlock;
  930. }
  931. if (nBlock < m_ncDataBlock - 2 && m_byBlockMode[nBlock + 2] == QR_MODE_8BIT)
  932. {
  933. // 结合结合的块后面有的8比特字节方式块
  934. m_nBlockLength[nBlock + 1] += m_nBlockLength[nBlock + 2];
  935. // 移动后续
  936. for (i = nBlock + 2; i < m_ncDataBlock - 1; ++i)
  937. {
  938. m_byBlockMode[i] = m_byBlockMode[i + 1];
  939. m_nBlockLength[i] = m_nBlockLength[i + 1];
  940. }
  941. --m_ncDataBlock;
  942. }
  943. m_byBlockMode[nBlock] = QR_MODE_8BIT;
  944. m_nBlockLength[nBlock] += m_nBlockLength[nBlock + 1];
  945. // 移动后续
  946. for (i = nBlock + 1; i < m_ncDataBlock - 1; ++i)
  947. {
  948. m_byBlockMode[i] = m_byBlockMode[i + 1];
  949. m_nBlockLength[i] = m_nBlockLength[i + 1];
  950. }
  951. --m_ncDataBlock;
  952. // 从结合的块前开始重新调查
  953. if (nBlock >= 1)
  954. --nBlock;
  955. continue;
  956. }
  957. ++nBlock; // 调查下面块
  958. }
  959. /////////////////////////////////////////////////////////////////////////
  960. // 比特排列化
  961. int ncComplete = 0; //处理完毕数据计数器
  962. WORD wBinCode;
  963. m_ncDataCodeWordBit = 0; // 比特单位处理柜台
  964. ZeroMemory(m_byDataCodeWord, MAX_DATACODEWORD);
  965. for (i = 0; i < m_ncDataBlock && m_ncDataCodeWordBit != -1; ++i)
  966. {
  967. if (m_byBlockMode[i] == QR_MODE_NUMERAL)
  968. {
  969. /////////////////////////////////////////////////////////////////
  970. // 数字模式
  971. //indicator(0001b)
  972. m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, 1, 4);
  973. // 文字数套
  974. m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, (WORD)m_nBlockLength[i], nIndicatorLenNumeral[nVerGroup]);
  975. //比特队列保存
  976. for (j = 0; j < m_nBlockLength[i]; j += 3)
  977. {
  978. if (j < m_nBlockLength[i] - 2)
  979. {
  980. wBinCode = (WORD)(((lpsSource[ncComplete + j] - '0') * 100) +
  981. ((lpsSource[ncComplete + j + 1] - '0') * 10) +
  982. (lpsSource[ncComplete + j + 2] - '0'));
  983. m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, wBinCode, 10);
  984. }
  985. else if (j == m_nBlockLength[i] - 2)
  986. {
  987. // 零数2字节
  988. wBinCode = (WORD)(((lpsSource[ncComplete + j] - '0') * 10) +
  989. (lpsSource[ncComplete + j + 1] - '0'));
  990. m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, wBinCode, 7);
  991. }
  992. else if (j == m_nBlockLength[i] - 1)
  993. {
  994. // 零数1字节
  995. wBinCode = (WORD)(lpsSource[ncComplete + j] - '0');
  996. m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, wBinCode, 4);
  997. }
  998. }
  999. ncComplete += m_nBlockLength[i];
  1000. }
  1001. else if (m_byBlockMode[i] == QR_MODE_ALPHABET)
  1002. {
  1003. /////////////////////////////////////////////////////////////////
  1004. //英数字模式
  1005. // 模式indicator(0010b)
  1006. m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, 2, 4);
  1007. // 文字数套
  1008. m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, (WORD)m_nBlockLength[i], nIndicatorLenAlphabet[nVerGroup]);
  1009. // 比特队列保存
  1010. for (j = 0; j < m_nBlockLength[i]; j += 2)
  1011. {
  1012. if (j < m_nBlockLength[i] - 1)
  1013. {
  1014. wBinCode = (WORD)((AlphabetToBinaly(lpsSource[ncComplete + j]) * 45) +
  1015. AlphabetToBinaly(lpsSource[ncComplete + j + 1]));
  1016. m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, wBinCode, 11);
  1017. }
  1018. else
  1019. {
  1020. // 零数1字节
  1021. wBinCode = (WORD)AlphabetToBinaly(lpsSource[ncComplete + j]);
  1022. m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, wBinCode, 6);
  1023. }
  1024. }
  1025. ncComplete += m_nBlockLength[i];
  1026. }
  1027. else if (m_byBlockMode[i] == QR_MODE_8BIT)
  1028. {
  1029. /////////////////////////////////////////////////////////////////
  1030. // 8比特字节方式
  1031. //模式indicator(0100b)
  1032. m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, 4, 4);
  1033. // 文字数套
  1034. m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, (WORD)m_nBlockLength[i], nIndicatorLen8Bit[nVerGroup]);
  1035. // 比特队列保存
  1036. for (j = 0; j < m_nBlockLength[i]; ++j)
  1037. {
  1038. m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, (WORD)lpsSource[ncComplete + j], 8);
  1039. }
  1040. ncComplete += m_nBlockLength[i];
  1041. }
  1042. else // m_byBlockMode[i] == QR_MODE_KANJI
  1043. {
  1044. /////////////////////////////////////////////////////////////////
  1045. // 汉字模式
  1046. // 模式indicator(1000b)
  1047. m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, 8, 4);
  1048. // 文字数套
  1049. m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, (WORD)(m_nBlockLength[i] / 2), nIndicatorLenKanji[nVerGroup]);
  1050. // 以汉字模式比特队列保存
  1051. for (j = 0; j < m_nBlockLength[i] / 2; ++j)
  1052. {
  1053. WORD wBinCode = KanjiToBinaly((WORD)(((BYTE)lpsSource[ncComplete + (j * 2)] << 8) + (BYTE)lpsSource[ncComplete + (j * 2) + 1]));
  1054. m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, wBinCode, 13);
  1055. }
  1056. ncComplete += m_nBlockLength[i];
  1057. }
  1058. }
  1059. return (m_ncDataCodeWordBit != -1);
  1060. }
  1061. /////////////////////////////////////////////////////////////////////////////
  1062. // CQR_Encode::GetBitLength
  1063. //事情途∶位长取得
  1064. //引数∶数据模式类别,数据长,版本(型号)小组
  1065. //返回值∶data bit长
  1066. //准备 考虑∶在汉字模式的数据长引数不是文字数字节数
  1067. int CQR_Encode::GetBitLength(BYTE nMode, int ncData, int nVerGroup)
  1068. {
  1069. int ncBits = 0;
  1070. switch (nMode)
  1071. {
  1072. case QR_MODE_NUMERAL:
  1073. ncBits = 4 + nIndicatorLenNumeral[nVerGroup] + (10 * (ncData / 3));
  1074. switch (ncData % 3)
  1075. {
  1076. case 1:
  1077. ncBits += 4;
  1078. break;
  1079. case 2:
  1080. ncBits += 7;
  1081. break;
  1082. default: // case 0:
  1083. break;
  1084. }
  1085. break;
  1086. case QR_MODE_ALPHABET:
  1087. ncBits = 4 + nIndicatorLenAlphabet[nVerGroup] + (11 * (ncData / 2)) + (6 * (ncData % 2));
  1088. break;
  1089. case QR_MODE_8BIT:
  1090. ncBits = 4 + nIndicatorLen8Bit[nVerGroup] + (8 * ncData);
  1091. break;
  1092. default: // case QR_MODE_KANJI:
  1093. ncBits = 4 + nIndicatorLenKanji[nVerGroup] + (13 * (ncData / 2));
  1094. break;
  1095. }
  1096. return ncBits;
  1097. }
  1098. /////////////////////////////////////////////////////////////////////////////
  1099. // CQR_Encode::SetBitStream
  1100. //事情途∶比特组套
  1101. //引数∶插入位置,比特排列数据,data bit长(最大16)
  1102. //返回值∶下次插入位置(缓冲超时=-1)
  1103. //准备 考虑∶m_byDataCodeWord为组套(要零初始化)结果
  1104. int CQR_Encode::SetBitStream(int nIndex, WORD wData, int ncData)
  1105. {
  1106. int i;
  1107. if (nIndex == -1 || nIndex + ncData > MAX_DATACODEWORD * 8)
  1108. return -1;
  1109. for (i = 0; i < ncData; ++i)
  1110. {
  1111. if (wData & (1 << (ncData - i - 1)))
  1112. {
  1113. m_byDataCodeWord[(nIndex + i) / 8] |= 1 << (7 - ((nIndex + i) % 8));
  1114. }
  1115. }
  1116. return nIndex + ncData;
  1117. }
  1118. /////////////////////////////////////////////////////////////////////////////
  1119. // CQR_Encode::IsNumeralData
  1120. //事情途∶数字模式相关检查
  1121. //引数∶调查文字
  1122. //返回值∶相关时=TRUE
  1123. BOOL CQR_Encode::IsNumeralData(unsigned char c)
  1124. {
  1125. if (c >= '0' && c <= '9')
  1126. return TRUE;
  1127. return FALSE;
  1128. }
  1129. /////////////////////////////////////////////////////////////////////////////
  1130. // CQR_Encode::IsAlphabetData
  1131. //事情途∶英数字模式相关检查
  1132. //引数∶调查文字
  1133. //返回值∶相关时=TRUE
  1134. BOOL CQR_Encode::IsAlphabetData(unsigned char c)
  1135. {
  1136. if (c >= '0' && c <= '9')
  1137. return TRUE;
  1138. if (c >= 'A' && c <= 'Z')
  1139. return TRUE;
  1140. if (c == ' ' || c == '$' || c == '%' || c == '*' || c == '+' || c == '-' || c == '.' || c == '/' || c == ':')
  1141. return TRUE;
  1142. return FALSE;
  1143. }
  1144. /////////////////////////////////////////////////////////////////////////////
  1145. // CQR_Encode::IsKanjiData
  1146. //事情途∶汉字模式相关检查
  1147. //引数∶调查文字(16比特文字)
  1148. //返回值∶相关时=TRUE
  1149. //准备 考虑∶EBBFh 以后的 S-JIS 不适用
  1150. BOOL CQR_Encode::IsKanjiData(unsigned char c1, unsigned char c2)
  1151. {
  1152. if (((c1 >= 0x81 && c1 <= 0x9f) || (c1 >= 0xe0 && c1 <= 0xeb)) && (c2 >= 0x40))
  1153. {
  1154. if ((c1 == 0x9f && c2 > 0xfc) || (c1 == 0xeb && c2 > 0xbf))
  1155. return FALSE;
  1156. return TRUE;
  1157. }
  1158. return FALSE;
  1159. }
  1160. /////////////////////////////////////////////////////////////////////////////
  1161. // CQR_Encode::AlphabetToBinaly
  1162. //事情途∶英数字模式文字的二进制化
  1163. //引数∶对象文字
  1164. //返回值∶二进制价值
  1165. BYTE CQR_Encode::AlphabetToBinaly(unsigned char c)
  1166. {
  1167. if (c >= '0' && c <= '9') return (unsigned char)(c - '0');
  1168. if (c >= 'A' && c <= 'Z') return (unsigned char)(c - 'A' + 10);
  1169. if (c == ' ') return 36;
  1170. if (c == '$') return 37;
  1171. if (c == '%') return 38;
  1172. if (c == '*') return 39;
  1173. if (c == '+') return 40;
  1174. if (c == '-') return 41;
  1175. if (c == '.') return 42;
  1176. if (c == '/') return 43;
  1177. return 44; // c == ':'
  1178. }
  1179. /////////////////////////////////////////////////////////////////////////////
  1180. // CQR_Encode::KanjiToBinaly
  1181. // CQR_Encode::KanjiToBinaly //事情途∶汉字模式文字的二进制化 //引数∶对象文字 //返回值∶二进制价值
  1182. WORD CQR_Encode::KanjiToBinaly(WORD wc)
  1183. {
  1184. if (wc >= 0x8140 && wc <= 0x9ffc)
  1185. wc -= 0x8140;
  1186. else // (wc >= 0xe040 && wc <= 0xebbf)
  1187. wc -= 0xc140;
  1188. return (WORD)(((wc >> 8) * 0xc0) + (wc & 0x00ff));
  1189. }
  1190. /////////////////////////////////////////////////////////////////////////////
  1191. // CQR_Encode::GetRSCodeWord
  1192. //事情途∶RS错误订正codeword取得
  1193. //引数∶data codeword address,data codeWord长,RScodeword长
  1194. //准备 考虑∶确保总codeword份儿的区域之后叫出
  1195. void CQR_Encode::GetRSCodeWord(LPBYTE lpbyRSWork, int ncDataCodeWord, int ncRSCodeWord)
  1196. {
  1197. int i, j;
  1198. for (i = 0; i < ncDataCodeWord ; ++i)
  1199. {
  1200. if (lpbyRSWork[0] != 0)
  1201. {
  1202. BYTE nExpFirst = byIntToExp[lpbyRSWork[0]]; // 比首次项系数乘数算出
  1203. for (j = 0; j < ncRSCodeWord; ++j)
  1204. {
  1205. // 为各项乘数加在一起算(%255→α^255=1)首次项乘数
  1206. BYTE nExpElement = (BYTE)(((int)(byRSExp[ncRSCodeWord][j] + nExpFirst)) % 255);
  1207. // 由异或的剩余算出
  1208. lpbyRSWork[j] = (BYTE)(lpbyRSWork[j + 1] ^ byExpToInt[nExpElement]);
  1209. }
  1210. // 移动残余横梁
  1211. for (j = ncRSCodeWord; j < ncDataCodeWord + ncRSCodeWord - 1; ++j)
  1212. lpbyRSWork[j] = lpbyRSWork[j + 1];
  1213. }
  1214. else
  1215. {
  1216. // 移动残余横梁
  1217. for (j = 0; j < ncDataCodeWord + ncRSCodeWord - 1; ++j)
  1218. lpbyRSWork[j] = lpbyRSWork[j + 1];
  1219. }
  1220. }
  1221. }
  1222. /////////////////////////////////////////////////////////////////////////////
  1223. // CQR_Encode::FormatModule
  1224. //事情途∶向组件的数据配置 //返回值∶一边的组件数
  1225. void CQR_Encode::FormatModule()
  1226. {
  1227. int i, j;
  1228. ZeroMemory(m_byModuleData, sizeof(m_byModuleData));
  1229. // 功能模块配置
  1230. SetFunctionModule();
  1231. // 数据图形配置
  1232. SetCodeWordPattern();
  1233. if (m_nMaskingNo == -1)
  1234. {
  1235. // 最适合掩蔽图形选择
  1236. m_nMaskingNo = 0;
  1237. SetMaskingPattern(m_nMaskingNo); //掩蔽
  1238. SetFormatInfoPattern(m_nMaskingNo); // 格式信息图形配置
  1239. int nMinPenalty = CountPenalty();
  1240. for (i = 1; i <= 7; ++i)
  1241. {
  1242. SetMaskingPattern(i); // 掩蔽
  1243. SetFormatInfoPattern(i); // 格式信息图形配置
  1244. int nPenalty = CountPenalty();
  1245. if (nPenalty < nMinPenalty)
  1246. {
  1247. nMinPenalty = nPenalty;
  1248. m_nMaskingNo = i;
  1249. }
  1250. }
  1251. }
  1252. SetMaskingPattern(m_nMaskingNo); // 掩蔽
  1253. SetFormatInfoPattern(m_nMaskingNo); // 格式信息图形配置
  1254. // 布尔价值组件图形变换
  1255. for (i = 0; i < m_nSymbleSize; ++i)
  1256. {
  1257. for (j = 0; j < m_nSymbleSize; ++j)
  1258. {
  1259. m_byModuleData[i][j] = (BYTE)((m_byModuleData[i][j] & 0x11) != 0);
  1260. }
  1261. }
  1262. }
  1263. /////////////////////////////////////////////////////////////////////////////
  1264. // CQR_Encode::SetFunctionModule
  1265. //事情途∶功能模块配置
  1266. //准备 考虑∶格式信息只功能模块登记(实在数据空白)
  1267. void CQR_Encode::SetFunctionModule()
  1268. {
  1269. int i, j;
  1270. // 位置查出图形
  1271. SetFinderPattern(0, 0);
  1272. SetFinderPattern(m_nSymbleSize - 7, 0);
  1273. SetFinderPattern(0, m_nSymbleSize - 7);
  1274. // 位置查出图形separator
  1275. for (i = 0; i < 8; ++i)
  1276. {
  1277. m_byModuleData[i][7] = m_byModuleData[7][i] = '\x20';
  1278. m_byModuleData[m_nSymbleSize - 8][i] = m_byModuleData[m_nSymbleSize - 8 + i][7] = '\x20';
  1279. m_byModuleData[i][m_nSymbleSize - 8] = m_byModuleData[7][m_nSymbleSize - 8 + i] = '\x20';
  1280. }
  1281. // 作为功能模块部登记格式信息记述位置
  1282. for (i = 0; i < 9; ++i)
  1283. {
  1284. m_byModuleData[i][8] = m_byModuleData[8][i] = '\x20';
  1285. }
  1286. for (i = 0; i < 8; ++i)
  1287. {
  1288. m_byModuleData[m_nSymbleSize - 8 + i][8] = m_byModuleData[8][m_nSymbleSize - 8 + i] = '\x20';
  1289. }
  1290. // 版本信息图形
  1291. SetVersionPattern();
  1292. //位置合在一起图形
  1293. for (i = 0; i < QR_VersonInfo[m_nVersion].ncAlignPoint; ++i)
  1294. {
  1295. SetAlignmentPattern(QR_VersonInfo[m_nVersion].nAlignPoint[i], 6);
  1296. SetAlignmentPattern(6, QR_VersonInfo[m_nVersion].nAlignPoint[i]);
  1297. for (j = 0; j < QR_VersonInfo[m_nVersion].ncAlignPoint; ++j)
  1298. {
  1299. SetAlignmentPattern(QR_VersonInfo[m_nVersion].nAlignPoint[i], QR_VersonInfo[m_nVersion].nAlignPoint[j]);
  1300. }
  1301. }
  1302. // 定时图形
  1303. for (i = 8; i <= m_nSymbleSize - 9; ++i)
  1304. {
  1305. m_byModuleData[i][6] = (i % 2) == 0 ? '\x30' : '\x20';
  1306. m_byModuleData[6][i] = (i % 2) == 0 ? '\x30' : '\x20';
  1307. }
  1308. }
  1309. /////////////////////////////////////////////////////////////////////////////
  1310. // CQR_Encode::SetFinderPattern
  1311. //事情途∶位置查出图形配置 //引数∶配置左面上坐标
  1312. void CQR_Encode::SetFinderPattern(int x, int y)
  1313. {
  1314. static BYTE byPattern[] = {0x7f, // 1111111b
  1315. 0x41, // 1000001b
  1316. 0x5d, // 1011101b
  1317. 0x5d, // 1011101b
  1318. 0x5d, // 1011101b
  1319. 0x41, // 1000001b
  1320. 0x7f}; // 1111111b
  1321. int i, j;
  1322. for (i = 0; i < 7; ++i)
  1323. {
  1324. for (j = 0; j < 7; ++j)
  1325. {
  1326. m_byModuleData[x + j][y + i] = (byPattern[i] & (1 << (6 - j))) ? '\x30' : '\x20';
  1327. }
  1328. }
  1329. }
  1330. /////////////////////////////////////////////////////////////////////////////
  1331. // CQR_Encode::SetAlignmentPattern
  1332. //事情途∶位置合在一起图形配置 //引数∶配置中央坐标
  1333. void CQR_Encode::SetAlignmentPattern(int x, int y)
  1334. {
  1335. static BYTE byPattern[] = {0x1f, // 11111b
  1336. 0x11, // 10001b
  1337. 0x15, // 10101b
  1338. 0x11, // 10001b
  1339. 0x1f}; // 11111b
  1340. int i, j;
  1341. if (m_byModuleData[x][y] & 0x20)
  1342. return; // 为了与功能模块重复除外
  1343. x -= 2; y -= 2; // 左面上在角落坐标变换
  1344. for (i = 0; i < 5; ++i)
  1345. {
  1346. for (j = 0; j < 5; ++j)
  1347. {
  1348. m_byModuleData[x + j][y + i] = (byPattern[i] & (1 << (4 - j))) ? '\x30' : '\x20';
  1349. }
  1350. }
  1351. }
  1352. /////////////////////////////////////////////////////////////////////////////
  1353. // CQR_Encode::SetVersionPattern
  1354. //事情途∶版本(型号)信息图形配置
  1355. //准备 考虑∶在扩张BCH(18,6)符号上犯错误作为订正使用
  1356. void CQR_Encode::SetVersionPattern()
  1357. {
  1358. int i, j;
  1359. if (m_nVersion <= 6)
  1360. return;
  1361. int nVerData = m_nVersion << 12;
  1362. // 剩余比特算出
  1363. for (i = 0; i < 6; ++i)
  1364. {
  1365. if (nVerData & (1 << (17 - i)))
  1366. {
  1367. nVerData ^= (0x1f25 << (5 - i));
  1368. }
  1369. }
  1370. nVerData += m_nVersion << 12;
  1371. for (i = 0; i < 6; ++i)
  1372. {
  1373. for (j = 0; j < 3; ++j)
  1374. {
  1375. m_byModuleData[m_nSymbleSize - 11 + j][i] = m_byModuleData[i][m_nSymbleSize - 11 + j] =
  1376. (nVerData & (1 << (i * 3 + j))) ? '\x30' : '\x20';
  1377. }
  1378. }
  1379. }
  1380. /////////////////////////////////////////////////////////////////////////////
  1381. // CQR_Encode::SetCodeWordPattern
  1382. //事情途∶数据图形配置
  1383. void CQR_Encode::SetCodeWordPattern()
  1384. {
  1385. int x = m_nSymbleSize;
  1386. int y = m_nSymbleSize - 1;
  1387. int nCoef_x = 1; // X轴配置
  1388. int nCoef_y = 1; // y轴配置
  1389. int i, j;
  1390. for (i = 0; i < m_ncAllCodeWord; ++i)
  1391. {
  1392. for (j = 0; j < 8; ++j)
  1393. {
  1394. do
  1395. {
  1396. x += nCoef_x;
  1397. nCoef_x *= -1;
  1398. if (nCoef_x < 0)
  1399. {
  1400. y += nCoef_y;
  1401. if (y < 0 || y == m_nSymbleSize)
  1402. {
  1403. y = (y < 0) ? 0 : m_nSymbleSize - 1;
  1404. nCoef_y *= -1;
  1405. x -= 2;
  1406. if (x == 6) //定时图形
  1407. --x;
  1408. }
  1409. }
  1410. }
  1411. while (m_byModuleData[x][y] & 0x20); // 除外功能模块
  1412. m_byModuleData[x][y] = (m_byAllCodeWord[i] & (1 << (7 - j))) ? '\x02' : '\x00';
  1413. }
  1414. }
  1415. }
  1416. /////////////////////////////////////////////////////////////////////////////
  1417. // CQR_Encode::SetMaskingPattern
  1418. //事情途∶掩蔽图形配置 //引数∶掩蔽图形号码
  1419. void CQR_Encode::SetMaskingPattern(int nPatternNo)
  1420. {
  1421. int i, j;
  1422. for (i = 0; i < m_nSymbleSize; ++i)
  1423. {
  1424. for (j = 0; j < m_nSymbleSize; ++j)
  1425. {
  1426. if (! (m_byModuleData[j][i] & 0x20)) // 除外功能模块
  1427. {
  1428. BOOL bMask;
  1429. switch (nPatternNo)
  1430. {
  1431. case 0:
  1432. bMask = ((i + j) % 2 == 0);
  1433. break;
  1434. case 1:
  1435. bMask = (i % 2 == 0);
  1436. break;
  1437. case 2:
  1438. bMask = (j % 3 == 0);
  1439. break;
  1440. case 3:
  1441. bMask = ((i + j) % 3 == 0);
  1442. break;
  1443. case 4:
  1444. bMask = (((i / 2) + (j / 3)) % 2 == 0);
  1445. break;
  1446. case 5:
  1447. bMask = (((i * j) % 2) + ((i * j) % 3) == 0);
  1448. break;
  1449. case 6:
  1450. bMask = ((((i * j) % 2) + ((i * j) % 3)) % 2 == 0);
  1451. break;
  1452. default: // case 7:
  1453. bMask = ((((i * j) % 3) + ((i + j) % 2)) % 2 == 0);
  1454. break;
  1455. }
  1456. m_byModuleData[j][i] = (BYTE)((m_byModuleData[j][i] & 0xfe) | (((m_byModuleData[j][i] & 0x02) > 1) ^ bMask));
  1457. }
  1458. }
  1459. }
  1460. }
  1461. /////////////////////////////////////////////////////////////////////////////
  1462. // CQR_Encode::SetFormatInfoPattern
  1463. //事情途∶格式信息配置 //引数∶掩蔽图形号码
  1464. void CQR_Encode::SetFormatInfoPattern(int nPatternNo)
  1465. {
  1466. int nFormatInfo;
  1467. int i;
  1468. switch (m_nLevel)
  1469. {
  1470. case QR_LEVEL_M:
  1471. nFormatInfo = 0x00; // 00nnnb
  1472. break;
  1473. case QR_LEVEL_L:
  1474. nFormatInfo = 0x08; // 01nnnb
  1475. break;
  1476. case QR_LEVEL_Q:
  1477. nFormatInfo = 0x18; // 11nnnb
  1478. break;
  1479. default: // case QR_LEVEL_H:
  1480. nFormatInfo = 0x10; // 10nnnb
  1481. break;
  1482. }
  1483. nFormatInfo += nPatternNo;
  1484. int nFormatData = nFormatInfo << 10;
  1485. // 剩余比特算出
  1486. for (i = 0; i < 5; ++i)
  1487. {
  1488. if (nFormatData & (1 << (14 - i)))
  1489. {
  1490. nFormatData ^= (0x0537 << (4 - i)); // 10100110111b
  1491. }
  1492. }
  1493. nFormatData += nFormatInfo << 10;
  1494. //掩蔽
  1495. nFormatData ^= 0x5412; // 101010000010010b
  1496. // 左面上位置查出图形周围配置
  1497. for (i = 0; i <= 5; ++i)
  1498. m_byModuleData[8][i] = (nFormatData & (1 << i)) ? '\x30' : '\x20';
  1499. m_byModuleData[8][7] = (nFormatData & (1 << 6)) ? '\x30' : '\x20';
  1500. m_byModuleData[8][8] = (nFormatData & (1 << 7)) ? '\x30' : '\x20';
  1501. m_byModuleData[7][8] = (nFormatData & (1 << 8)) ? '\x30' : '\x20';
  1502. for (i = 9; i <= 14; ++i)
  1503. m_byModuleData[14 - i][8] = (nFormatData & (1 << i)) ? '\x30' : '\x20';
  1504. // 右上位置查出图形下配置
  1505. for (i = 0; i <= 7; ++i)
  1506. m_byModuleData[m_nSymbleSize - 1 - i][8] = (nFormatData & (1 << i)) ? '\x30' : '\x20';
  1507. // 左下位置查出图形右配置
  1508. m_byModuleData[8][m_nSymbleSize - 8] = '\x30'; // 固定暗组件
  1509. for (i = 8; i <= 14; ++i)
  1510. m_byModuleData[8][m_nSymbleSize - 15 + i] = (nFormatData & (1 << i)) ? '\x30' : '\x20';
  1511. }
  1512. /////////////////////////////////////////////////////////////////////////////
  1513. // CQR_Encode::CountPenalty
  1514. //事情途∶面具后罚比分算出
  1515. int CQR_Encode::CountPenalty()
  1516. {
  1517. int nPenalty = 0;
  1518. int i, j, k;
  1519. // 同色的队列的邻接组件
  1520. for (i = 0; i < m_nSymbleSize; ++i)
  1521. {
  1522. for (j = 0; j < m_nSymbleSize - 4; ++j)
  1523. {
  1524. int nCount = 1;
  1525. for (k = j + 1; k < m_nSymbleSize; k++)
  1526. {
  1527. if (((m_byModuleData[i][j] & 0x11) == 0) == ((m_byModuleData[i][k] & 0x11) == 0))
  1528. ++nCount;
  1529. else
  1530. break;
  1531. }
  1532. if (nCount >= 5)
  1533. {
  1534. nPenalty += 3 + (nCount - 5);
  1535. }
  1536. j = k - 1;
  1537. }
  1538. }
  1539. // 同色的行的邻接组件
  1540. for (i = 0; i < m_nSymbleSize; ++i)
  1541. {
  1542. for (j = 0; j < m_nSymbleSize - 4; ++j)
  1543. {
  1544. int nCount = 1;
  1545. for (k = j + 1; k < m_nSymbleSize; k++)
  1546. {
  1547. if (((m_byModuleData[j][i] & 0x11) == 0) == ((m_byModuleData[k][i] & 0x11) == 0))
  1548. ++nCount;
  1549. else
  1550. break;
  1551. }
  1552. if (nCount >= 5)
  1553. {
  1554. nPenalty += 3 + (nCount - 5);
  1555. }
  1556. j = k - 1;
  1557. }
  1558. }
  1559. // 同色的组件块(2×2)
  1560. for (i = 0; i < m_nSymbleSize - 1; ++i)
  1561. {
  1562. for (j = 0; j < m_nSymbleSize - 1; ++j)
  1563. {
  1564. if ((((m_byModuleData[i][j] & 0x11) == 0) == ((m_byModuleData[i + 1][j] & 0x11) == 0)) &&
  1565. (((m_byModuleData[i][j] & 0x11) == 0) == ((m_byModuleData[i] [j + 1] & 0x11) == 0)) &&
  1566. (((m_byModuleData[i][j] & 0x11) == 0) == ((m_byModuleData[i + 1][j + 1] & 0x11) == 0)))
  1567. {
  1568. nPenalty += 3;
  1569. }
  1570. }
  1571. }
  1572. // 在该一列上(里)能放的 1:1:3:1:1比率(暗∶智惠∶暗∶智惠∶暗)的图形
  1573. for (i = 0; i < m_nSymbleSize; ++i)
  1574. {
  1575. for (j = 0; j < m_nSymbleSize - 6; ++j)
  1576. {
  1577. if (((j == 0) || (! (m_byModuleData[i][j - 1] & 0x11))) && // 智惠 或符号外
  1578. ( m_byModuleData[i][j] & 0x11) && // 暗-1
  1579. (! (m_byModuleData[i][j + 1] & 0x11)) && // 智惠-1
  1580. ( m_byModuleData[i][j + 2] & 0x11) && // 暗 ┐
  1581. ( m_byModuleData[i][j + 3] & 0x11) && // 暗 │3
  1582. ( m_byModuleData[i][j + 4] & 0x11) && // 暗 ┘
  1583. (! (m_byModuleData[i][j + 5] & 0x11)) && // 智惠 - 1
  1584. ( m_byModuleData[i][j + 6] & 0x11) && // 暗 - 1
  1585. ((j == m_nSymbleSize - 7) || (! (m_byModuleData[i][j + 7] & 0x11)))) // 智惠 或符号外
  1586. {
  1587. // 前或后边4以上的智惠图形
  1588. if (((j < 2 || ! (m_byModuleData[i][j - 2] & 0x11)) &&
  1589. (j < 3 || ! (m_byModuleData[i][j - 3] & 0x11)) &&
  1590. (j < 4 || ! (m_byModuleData[i][j - 4] & 0x11))) ||
  1591. ((j >= m_nSymbleSize - 8 || ! (m_byModuleData[i][j + 8] & 0x11)) &&
  1592. (j >= m_nSymbleSize - 9 || ! (m_byModuleData[i][j + 9] & 0x11)) &&
  1593. (j >= m_nSymbleSize - 10 || ! (m_byModuleData[i][j + 10] & 0x11))))
  1594. {
  1595. nPenalty += 40;
  1596. }
  1597. }
  1598. }
  1599. }
  1600. // 在前往同样能放的 1:1:3:1:1比率(暗∶智惠∶暗∶智惠∶暗)的图形
  1601. for (i = 0; i < m_nSymbleSize; ++i)
  1602. {
  1603. for (j = 0; j < m_nSymbleSize - 6; ++j)
  1604. {
  1605. if (((j == 0) || (! (m_byModuleData[j - 1][i] & 0x11))) && //智惠 或符号外
  1606. ( m_byModuleData[j] [i] & 0x11) && // 暗-1
  1607. (! (m_byModuleData[j + 1][i] & 0x11)) && // 智惠 - 1
  1608. ( m_byModuleData[j + 2][i] & 0x11) && // 暗 ┐
  1609. ( m_byModuleData[j + 3][i] & 0x11) && // 暗 │3
  1610. ( m_byModuleData[j + 4][i] & 0x11) && // 暗 ┘
  1611. (! (m_byModuleData[j + 5][i] & 0x11)) && // 智惠 - 1
  1612. ( m_byModuleData[j + 6][i] & 0x11) && // 暗 - 1
  1613. ((j == m_nSymbleSize - 7) || (! (m_byModuleData[j + 7][i] & 0x11)))) // 智惠 或符号外
  1614. {
  1615. // 前或后边4以上的智惠图形
  1616. if (((j < 2 || ! (m_byModuleData[j - 2][i] & 0x11)) &&
  1617. (j < 3 || ! (m_byModuleData[j - 3][i] & 0x11)) &&
  1618. (j < 4 || ! (m_byModuleData[j - 4][i] & 0x11))) ||
  1619. ((j >= m_nSymbleSize - 8 || ! (m_byModuleData[j + 8][i] & 0x11)) &&
  1620. (j >= m_nSymbleSize - 9 || ! (m_byModuleData[j + 9][i] & 0x11)) &&
  1621. (j >= m_nSymbleSize - 10 || ! (m_byModuleData[j + 10][i] & 0x11))))
  1622. {
  1623. nPenalty += 40;
  1624. }
  1625. }
  1626. }
  1627. }
  1628. // 对全部的暗组件占的比例
  1629. int nCount = 0;
  1630. for (i = 0; i < m_nSymbleSize; ++i)
  1631. {
  1632. for (j = 0; j < m_nSymbleSize; ++j)
  1633. {
  1634. if (! (m_byModuleData[i][j] & 0x11))
  1635. {
  1636. ++nCount;
  1637. }
  1638. }
  1639. }
  1640. nPenalty += (abs(50 - ((nCount * 100) / (m_nSymbleSize * m_nSymbleSize))) / 5) * 10;
  1641. return nPenalty;
  1642. }