qr_encode.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2010 Psytec Inc.
  3. * Copyright (c) 2012 Alexey Mednyy <swexru@gmail.com>
  4. * Copyright (c) 2012 Pavol Rusnak <stick@gk2.sk>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #ifndef __QR_ENCODE_H__
  25. #define __QR_ENCODE_H__
  26. #include <stdlib.h>
  27. #include <stdint.h>
  28. // Constants
  29. // Error correction level
  30. #define QR_LEVEL_L 0 // 7% of codewords can be restored
  31. #define QR_LEVEL_M 1 // 15% of codewords can be restored
  32. #define QR_LEVEL_Q 2 // 25% of codewords can be restored
  33. #define QR_LEVEL_H 3 // 30% of codewords can be restored
  34. // Data Mode
  35. #define QR_MODE_NUMERAL 0 // numbers
  36. #define QR_MODE_ALPHABET 1 // alphanumberic
  37. #define QR_MODE_8BIT 2 // rest
  38. // Group version (Model number)
  39. #define QR_VERSION_S 0 // 1 - 9 (module 21 - 53)
  40. #define QR_VERSION_M 1 // 10 - 26 (module 57 - 121)
  41. #define QR_VERSION_L 2 // 27 - 40 (module 125 - 177)
  42. #ifndef QR_MAX_VERSION
  43. #define QR_MAX_VERSION QR_VERSION_L
  44. #endif
  45. // Length constants
  46. #if QR_MAX_VERSION == QR_VERSION_S
  47. #define QR_MAX_MODULESIZE (9 * 4 + 17) // Maximum number of modules in a side
  48. #define QR_MAX_ALLCODEWORD 292 // Maximum total number of code words
  49. #define QR_MAX_DATACODEWORD 232 // Maximum data word code
  50. #endif
  51. #if QR_MAX_VERSION == QR_VERSION_M
  52. #define QR_MAX_MODULESIZE (26 * 4 + 17) // Maximum number of modules in a side
  53. #define QR_MAX_ALLCODEWORD 1706 // Maximum total number of code words
  54. #define QR_MAX_DATACODEWORD 1370 // Maximum data word code
  55. #endif
  56. #if QR_MAX_VERSION == QR_VERSION_L
  57. #define QR_MAX_MODULESIZE (40 * 4 + 17) // Maximum number of modules in a side
  58. #define QR_MAX_ALLCODEWORD 3706 // Maximum total number of code words
  59. #define QR_MAX_DATACODEWORD 2956 // Maximum data word code
  60. #endif
  61. #define QR_MAX_BITDATA ((QR_MAX_MODULESIZE * QR_MAX_MODULESIZE + 7) / 8) // Maximum size of bit data
  62. #define QR_MAX_CODEBLOCK 153 // Maximum number of block data code word (including RS code word)
  63. //
  64. // * level - error correction level, use QR_LEVEL_? macros
  65. // * version - version of the code (1-40), use 0 for autodetection
  66. // * source - source data
  67. // * source_len - length of the source data, use 0 when passing zero-terminated string
  68. // * result - array to write, writes to bits
  69. //
  70. // * function returns the size of the square side
  71. //
  72. int qr_encode(int level, int version, const char *source, size_t source_len, uint8_t *result);
  73. #endif