You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ZXByteMatrix.m 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2012 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "ZXByteMatrix.h"
  17. @implementation ZXByteMatrix
  18. - (id)initWithWidth:(int)width height:(int)height {
  19. if (self = [super init]) {
  20. _width = width;
  21. _height = height;
  22. _array = (int8_t **)malloc(height * sizeof(int8_t *));
  23. for (int i = 0; i < height; i++) {
  24. _array[i] = (int8_t *)malloc(width * sizeof(int8_t));
  25. }
  26. [self clear:0];
  27. }
  28. return self;
  29. }
  30. - (void)dealloc {
  31. if (_array != NULL) {
  32. for (int i = 0; i < self.height; i++) {
  33. free(_array[i]);
  34. }
  35. free(_array);
  36. _array = NULL;
  37. }
  38. }
  39. - (int8_t)getX:(int)x y:(int)y {
  40. return self.array[y][x];
  41. }
  42. - (void)setX:(int)x y:(int)y byteValue:(int8_t)value {
  43. self.array[y][x] = value;
  44. }
  45. - (void)setX:(int)x y:(int)y intValue:(int)value {
  46. self.array[y][x] = (int8_t)value;
  47. }
  48. - (void)setX:(int)x y:(int)y boolValue:(BOOL)value {
  49. self.array[y][x] = (int8_t)value;
  50. }
  51. - (void)clear:(int8_t)value {
  52. for (int y = 0; y < self.height; ++y) {
  53. for (int x = 0; x < self.width; ++x) {
  54. self.array[y][x] = value;
  55. }
  56. }
  57. }
  58. - (NSString *)description {
  59. NSMutableString *result = [NSMutableString string];
  60. for (int y = 0; y < self.height; ++y) {
  61. for (int x = 0; x < self.width; ++x) {
  62. switch (self.array[y][x]) {
  63. case 0:
  64. [result appendString:@" 0"];
  65. break;
  66. case 1:
  67. [result appendString:@" 1"];
  68. break;
  69. default:
  70. [result appendString:@" "];
  71. break;
  72. }
  73. }
  74. [result appendString:@"\n"];
  75. }
  76. return result;
  77. }
  78. @end