Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. // ---------------------------------------------------------------------------
  2. //
  3. // QRGenerator
  4. //
  5. // Create: 15/05/2013
  6. // Last update: 15/05/2013
  7. //
  8. // Author: TWOTM
  9. //
  10. //
  11. // Note:
  12. //
  13. // /o ULTRAMUNDUM FOUNDATION - all rights reserved
  14. // ---------------------------------------------------------------------------
  15.  
  16.  
  17. // -------------------------------------------------------
  18. // Includes
  19. // -------------------------------------------------------
  20.  
  21. #include "stdafx.h"
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25.  
  26. #include "qrencode.h"
  27. // -------------------------------------------------------
  28.  
  29.  
  30. // -------------------------------------------------------
  31. // DEFines
  32. // -------------------------------------------------------
  33.  
  34. #define QRCODE_TEXT "WeAreAnonymous" // Text to encode into QRCode
  35. #define OUT_FILE "qrcode.bmp" // Output file name
  36. #define OUT_FILE_PIXEL_PRESCALER 8 // Prescaler (number of pixels in bmp file for each QRCode pixel, on each dimension)
  37.  
  38. #define PIXEL_COLOR_R 0 // Color of bmp pixels
  39. #define PIXEL_COLOR_G 0
  40. #define PIXEL_COLOR_B 0
  41.  
  42. // BMP defines
  43.  
  44. typedef unsigned short WORD;
  45. typedef unsigned long DWORD;
  46. typedef signed long LONG;
  47.  
  48. #define BI_RGB 0L
  49.  
  50. #pragma pack(push, 2)
  51.  
  52. typedef struct
  53. {
  54. WORD bfType;
  55. DWORD bfSize;
  56. WORD bfReserved1;
  57. WORD bfReserved2;
  58. DWORD bfOffBits;
  59. } BITMAPFILEHEADER;
  60.  
  61. typedef struct
  62. {
  63. DWORD biSize;
  64. LONG biWidth;
  65. LONG biHeight;
  66. WORD biPlanes;
  67. WORD biBitCount;
  68. DWORD biCompression;
  69. DWORD biSizeImage;
  70. LONG biXPelsPerMeter;
  71. LONG biYPelsPerMeter;
  72. DWORD biClrUsed;
  73. DWORD biClrImportant;
  74. } BITMAPINFOHEADER;
  75.  
  76. #pragma pack(pop)
  77. // -------------------------------------------------------
  78.  
  79.  
  80. // -------------------------------------------------------
  81. // Main
  82. // -------------------------------------------------------
  83.  
  84. int _tmain(int argc, _TCHAR* argv[])
  85. {
  86. char* szSourceSring = QRCODE_TEXT;
  87. unsigned int unWidth, x, y, l, n, unWidthAdjusted, unDataBytes;
  88. unsigned char* pRGBData, *pSourceData, *pDestData;
  89. QRcode* pQRC;
  90. FILE* f;
  91.  
  92. /*
  93. * Create a symbol from the string. The library automatically parses the input
  94. * string and encodes in a QR Code symbol.
  95. * @warning This function is THREAD UNSAFE when pthread is disabled.
  96. * @param string input string. It must be NUL terminated.
  97. * @param version version of the symbol. If 0, the library chooses the minimum
  98. * version for the given input data.
  99. * @param level error correction level.
  100. * @param hint tell the library how non-alphanumerical characters should be
  101. * encoded. If QR_MODE_KANJI is given, kanji characters will be
  102. * encoded as Shif-JIS characters. If QR_MODE_8 is given, all of
  103. * non-alphanumerical characters will be encoded as is. If you want
  104. * to embed UTF-8 string, choose this.
  105. * @param casesensitive case-sensitive(1) or not(0).
  106. * @return an instance of QRcode class. The version of the result QRcode may
  107. * be larger than the designated version. On error, NULL is returned,
  108. * and errno is set to indicate the error. See Exceptions for the
  109. * details.
  110. * @throw EINVAL invalid input object.
  111. * @throw ENOMEM unable to allocate memory for input objects.
  112. * @throw ERANGE input data is too large.
  113. */
  114.  
  115. // Compute QRCode
  116.  
  117. if (pQRC = QRcode_encodeString(szSourceSring, 0, QR_ECLEVEL_H, QR_MODE_8, 1))
  118. {
  119. unWidth = pQRC->width;
  120. unWidthAdjusted = unWidth * OUT_FILE_PIXEL_PRESCALER * 3;
  121. if (unWidthAdjusted % 4)
  122. unWidthAdjusted = (unWidthAdjusted / 4 + 1) * 4;
  123. unDataBytes = unWidthAdjusted * unWidth * OUT_FILE_PIXEL_PRESCALER;
  124.  
  125. // Allocate pixels buffer
  126.  
  127. if (!(pRGBData = (unsigned char*)malloc(unDataBytes)))
  128. {
  129. printf("Out of memory");
  130. exit(-1);
  131. }
  132.  
  133. // Preset to white
  134.  
  135. memset(pRGBData, 0xff, unDataBytes);
  136.  
  137.  
  138. // Prepare bmp headers
  139.  
  140. BITMAPFILEHEADER kFileHeader;
  141. kFileHeader.bfType = 0x4d42; // "BM"
  142. kFileHeader.bfSize = sizeof(BITMAPFILEHEADER) +
  143. sizeof(BITMAPINFOHEADER) +
  144. unDataBytes;
  145. kFileHeader.bfReserved1 = 0;
  146. kFileHeader.bfReserved2 = 0;
  147. kFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) +
  148. sizeof(BITMAPINFOHEADER);
  149.  
  150. BITMAPINFOHEADER kInfoHeader;
  151. kInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
  152. kInfoHeader.biWidth = unWidth * OUT_FILE_PIXEL_PRESCALER;
  153. kInfoHeader.biHeight = -((int)unWidth * OUT_FILE_PIXEL_PRESCALER);
  154. kInfoHeader.biPlanes = 1;
  155. kInfoHeader.biBitCount = 24;
  156. kInfoHeader.biCompression = BI_RGB;
  157. kInfoHeader.biSizeImage = 0;
  158. kInfoHeader.biXPelsPerMeter = 0;
  159. kInfoHeader.biYPelsPerMeter = 0;
  160. kInfoHeader.biClrUsed = 0;
  161. kInfoHeader.biClrImportant = 0;
  162.  
  163.  
  164. // Convert QrCode bits to bmp pixels
  165.  
  166. pSourceData = pQRC->data;
  167. for(y = 0; y < unWidth; y++)
  168. {
  169. pDestData = pRGBData + unWidthAdjusted * y * OUT_FILE_PIXEL_PRESCALER;
  170. for(x = 0; x < unWidth; x++)
  171. {
  172. if (*pSourceData & 1)
  173. {
  174. for(l = 0; l < OUT_FILE_PIXEL_PRESCALER; l++)
  175. {
  176. for(n = 0; n < OUT_FILE_PIXEL_PRESCALER; n++)
  177. {
  178. *(pDestData + n * 3 + unWidthAdjusted * l) = PIXEL_COLOR_B;
  179. *(pDestData + 1 + n * 3 + unWidthAdjusted * l) = PIXEL_COLOR_G;
  180. *(pDestData + 2 + n * 3 + unWidthAdjusted * l) = PIXEL_COLOR_R;
  181. }
  182. }
  183. }
  184. pDestData += 3 * OUT_FILE_PIXEL_PRESCALER;
  185. pSourceData++;
  186. }
  187. }
  188.  
  189.  
  190. // Output the bmp file
  191.  
  192. if (!(fopen_s(&f, OUT_FILE, "wb")))
  193. {
  194. fwrite(&kFileHeader, sizeof(BITMAPFILEHEADER), 1, f);
  195. fwrite(&kInfoHeader, sizeof(BITMAPINFOHEADER), 1, f);
  196. fwrite(pRGBData, sizeof(unsigned char), unDataBytes, f);
  197.  
  198. fclose(f);
  199. }
  200. else
  201. {
  202. printf("Unable to open file");
  203. exit(-1);
  204. }
  205.  
  206. // Free data
  207.  
  208. free(pRGBData);
  209. QRcode_free(pQRC);
  210. }
  211. else
  212. {
  213. printf("NULL returned");
  214. exit(-1);
  215. }
  216.  
  217. return 0;
  218. }
  219. // -------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement