单军华
2017-07-12 20d1260d26b028897f3c0935c12fc35aa37b2e93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * Copyright 2012 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#import "ZXBitArray.h"
#import "ZXBitMatrix.h"
#import "ZXBoolArray.h"
#import "ZXIntArray.h"
 
@interface ZXBitMatrix ()
 
@property (nonatomic, assign, readonly) int bitsSize;
 
@end
 
@implementation ZXBitMatrix
 
- (id)initWithDimension:(int)dimension {
  return [self initWithWidth:dimension height:dimension];
}
 
- (id)initWithWidth:(int)width height:(int)height {
  if (self = [super init]) {
    if (width < 1 || height < 1) {
      @throw [NSException exceptionWithName:NSInvalidArgumentException
                                     reason:@"Both dimensions must be greater than 0"
                                   userInfo:nil];
    }
    _width = width;
    _height = height;
    _rowSize = (_width + 31) / 32;
    _bitsSize = _rowSize * _height;
    _bits = (int32_t *)malloc(_bitsSize * sizeof(int32_t));
    [self clear];
  }
 
  return self;
}
 
- (id)initWithWidth:(int)width height:(int)height rowSize:(int)rowSize bits:(int32_t *)bits {
  if (self = [super init]) {
    _width = width;
    _height = height;
    _rowSize = rowSize;
    _bitsSize = _rowSize * _height;
    _bits = (int32_t *)malloc(_bitsSize * sizeof(int32_t));
    memcpy(_bits, bits, _bitsSize * sizeof(int32_t));
  }
 
  return self;
}
 
- (void)dealloc {
  if (_bits != NULL) {
    free(_bits);
    _bits = NULL;
  }
}
 
+ (ZXBitMatrix *)parse:(NSString *)stringRepresentation
             setString:(NSString *)setString
           unsetString:(NSString *)unsetString {
  if (!stringRepresentation) {
    @throw [NSException exceptionWithName:@"IllegalArgumentException"
                                   reason:@"stringRepresentation is required"
                                 userInfo:nil];
  }
 
  ZXBoolArray *bits = [[ZXBoolArray alloc] initWithLength:(unsigned int)stringRepresentation.length];
  int bitsPos = 0;
  int rowStartPos = 0;
  int rowLength = -1;
  int nRows = 0;
  int pos = 0;
  while (pos < stringRepresentation.length) {
    if ([stringRepresentation characterAtIndex:pos] == '\n' ||
        [stringRepresentation characterAtIndex:pos] == '\r') {
      if (bitsPos > rowStartPos) {
        if(rowLength == -1) {
          rowLength = bitsPos - rowStartPos;
        } else if (bitsPos - rowStartPos != rowLength) {
          @throw [NSException exceptionWithName:@"IllegalArgumentException"
                                         reason:@"row lengths do not match"
                                       userInfo:nil];
        }
        rowStartPos = bitsPos;
        nRows++;
      }
      pos++;
    } else if ([[stringRepresentation substringWithRange:NSMakeRange(pos, setString.length)] isEqualToString:setString]) {
      pos += setString.length;
      bits.array[bitsPos] = YES;
      bitsPos++;
    } else if ([[stringRepresentation substringWithRange:NSMakeRange(pos, unsetString.length)] isEqualToString:unsetString]) {
      pos += unsetString.length;
      bits.array[bitsPos] = NO;
      bitsPos++;
    } else {
      @throw [NSException exceptionWithName:@"IllegalArgumentException"
                                     reason:[NSString stringWithFormat:@"illegal character encountered: %@", [stringRepresentation substringFromIndex:pos]]
                                   userInfo:nil];
    }
  }
 
  // no EOL at end?
  if (bitsPos > rowStartPos) {
    if (rowLength == -1) {
      rowLength = bitsPos - rowStartPos;
    } else if (bitsPos - rowStartPos != rowLength) {
      @throw [NSException exceptionWithName:@"IllegalArgumentException"
                                     reason:@"row lengths do not match"
                                   userInfo:nil];
    }
    nRows++;
  }
 
  ZXBitMatrix *matrix = [[ZXBitMatrix alloc] initWithWidth:rowLength height:nRows];
  for (int i = 0; i < bitsPos; i++) {
    if (bits.array[i]) {
      [matrix setX:i % rowLength y:i / rowLength];
    }
  }
  return matrix;
}
 
- (BOOL)getX:(int)x y:(int)y {
  NSInteger offset = y * self.rowSize + (x / 32);
  return ((_bits[offset] >> (x & 0x1f)) & 1) != 0;
}
 
- (void)setX:(int)x y:(int)y {
  NSInteger offset = y * self.rowSize + (x / 32);
  _bits[offset] |= 1 << (x & 0x1f);
}
 
- (void)unsetX:(int)x y:(int)y {
  int offset = y * self.rowSize + (x / 32);
  _bits[offset] &= ~(1 << (x & 0x1f));
}
 
- (void)flipX:(int)x y:(int)y {
  NSUInteger offset = y * self.rowSize + (x / 32);
  _bits[offset] ^= 1 << (x & 0x1f);
}
 
- (void)xor:(ZXBitMatrix *)mask {
  if (self.width != mask.width || self.height != mask.height
      || self.rowSize != mask.rowSize) {
    @throw [NSException exceptionWithName:NSInvalidArgumentException
                                   reason:@"input matrix dimensions do not match"
                                 userInfo:nil];
  }
  ZXBitArray *rowArray = [[ZXBitArray alloc] initWithSize:self.width];
  for (int y = 0; y < self.height; y++) {
    int offset = y * self.rowSize;
    int32_t *row = [mask rowAtY:y row:rowArray].bits;
    for (int x = 0; x < self.rowSize; x++) {
      self.bits[offset + x] ^= row[x];
    }
  }
}
 
- (void)clear {
  NSInteger max = self.bitsSize;
  memset(_bits, 0, max * sizeof(int32_t));
}
 
- (void)setRegionAtLeft:(int)left top:(int)top width:(int)aWidth height:(int)aHeight {
  if (aHeight < 1 || aWidth < 1) {
    @throw [NSException exceptionWithName:NSInvalidArgumentException
                                   reason:@"Height and width must be at least 1"
                                 userInfo:nil];
  }
  NSUInteger right = left + aWidth;
  NSUInteger bottom = top + aHeight;
  if (bottom > self.height || right > self.width) {
    @throw [NSException exceptionWithName:NSInvalidArgumentException
                                   reason:@"The region must fit inside the matrix"
                                 userInfo:nil];
  }
  for (NSUInteger y = top; y < bottom; y++) {
    NSUInteger offset = y * self.rowSize;
    for (NSInteger x = left; x < right; x++) {
      _bits[offset + (x / 32)] |= 1 << (x & 0x1f);
    }
  }
}
 
- (ZXBitArray *)rowAtY:(int)y row:(ZXBitArray *)row {
  if (row == nil || [row size] < self.width) {
    row = [[ZXBitArray alloc] initWithSize:self.width];
  } else {
    [row clear];
  }
  int offset = y * self.rowSize;
  for (int x = 0; x < self.rowSize; x++) {
    [row setBulk:x * 32 newBits:_bits[offset + x]];
  }
 
  return row;
}
 
- (void)setRowAtY:(int)y row:(ZXBitArray *)row {
  for (NSUInteger i = 0; i < self.rowSize; i++) {
    _bits[(y * self.rowSize) + i] = row.bits[i];
  }
}
 
- (void)rotate180 {
  int width = self.width;
  int height = self.height;
  ZXBitArray *topRow = [[ZXBitArray alloc] initWithSize:width];
  ZXBitArray *bottomRow = [[ZXBitArray alloc] initWithSize:width];
  for (int i = 0; i < (height+1) / 2; i++) {
    topRow = [self rowAtY:i row:topRow];
    bottomRow = [self rowAtY:height - 1 - i row:bottomRow];
    [topRow reverse];
    [bottomRow reverse];
    [self setRowAtY:i row:bottomRow];
    [self setRowAtY:height - 1 - i row:topRow];
  }
}
 
- (ZXIntArray *)enclosingRectangle {
  int left = self.width;
  int top = self.height;
  int right = -1;
  int bottom = -1;
 
  for (int y = 0; y < self.height; y++) {
    for (int x32 = 0; x32 < self.rowSize; x32++) {
      int32_t theBits = _bits[y * self.rowSize + x32];
      if (theBits != 0) {
        if (y < top) {
          top = y;
        }
        if (y > bottom) {
          bottom = y;
        }
        if (x32 * 32 < left) {
          int32_t bit = 0;
          while ((theBits << (31 - bit)) == 0) {
            bit++;
          }
          if ((x32 * 32 + bit) < left) {
            left = x32 * 32 + bit;
          }
        }
        if (x32 * 32 + 31 > right) {
          int bit = 31;
          while ((theBits >> bit) == 0) {
            bit--;
          }
          if ((x32 * 32 + bit) > right) {
            right = x32 * 32 + bit;
          }
        }
      }
    }
  }
 
  NSInteger width = right - left;
  NSInteger height = bottom - top;
 
  if (width < 0 || height < 0) {
    return nil;
  }
 
  return [[ZXIntArray alloc] initWithInts:left, top, width, height, -1];
}
 
- (ZXIntArray *)topLeftOnBit {
  int bitsOffset = 0;
  while (bitsOffset < self.bitsSize && _bits[bitsOffset] == 0) {
    bitsOffset++;
  }
  if (bitsOffset == self.bitsSize) {
    return nil;
  }
  int y = bitsOffset / self.rowSize;
  int x = (bitsOffset % self.rowSize) * 32;
 
  int32_t theBits = _bits[bitsOffset];
  int32_t bit = 0;
  while ((theBits << (31 - bit)) == 0) {
    bit++;
  }
  x += bit;
  return [[ZXIntArray alloc] initWithInts:x, y, -1];
}
 
- (ZXIntArray *)bottomRightOnBit {
  int bitsOffset = self.bitsSize - 1;
  while (bitsOffset >= 0 && _bits[bitsOffset] == 0) {
    bitsOffset--;
  }
  if (bitsOffset < 0) {
    return nil;
  }
 
  int y = bitsOffset / self.rowSize;
  int x = (bitsOffset % self.rowSize) * 32;
 
  int32_t theBits = _bits[bitsOffset];
  int32_t bit = 31;
  while ((theBits >> bit) == 0) {
    bit--;
  }
  x += bit;
 
  return [[ZXIntArray alloc] initWithInts:x, y, -1];
}
 
- (BOOL)isEqual:(NSObject *)o {
  if (!([o isKindOfClass:[ZXBitMatrix class]])) {
    return NO;
  }
  ZXBitMatrix *other = (ZXBitMatrix *)o;
  for (int i = 0; i < self.bitsSize; i++) {
    if (_bits[i] != other.bits[i]) {
      return NO;
    }
  }
  return self.width == other.width && self.height == other.height && self.rowSize == other.rowSize && self.bitsSize == other.bitsSize;
}
 
- (NSUInteger)hash {
  NSInteger hash = self.width;
  hash = 31 * hash + self.width;
  hash = 31 * hash + self.height;
  hash = 31 * hash + self.rowSize;
  for (NSUInteger i = 0; i < self.bitsSize; i++) {
    hash = 31 * hash + _bits[i];
  }
  return hash;
}
 
- (NSString *)description {
  return [self descriptionWithSetString:@"X " unsetString:@"  "];
}
 
- (NSString *)descriptionWithSetString:(NSString *)setString unsetString:(NSString *)unsetString {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  return [self descriptionWithSetString:setString unsetString:unsetString lineSeparator:@"\n"];
#pragma GCC diagnostic pop
}
 
- (NSString *)descriptionWithSetString:(NSString *)setString unsetString:(NSString *)unsetString
                         lineSeparator:(NSString *)lineSeparator {
  NSMutableString *result = [NSMutableString stringWithCapacity:self.height * (self.width + 1)];
  for (int y = 0; y < self.height; y++) {
    for (int x = 0; x < self.width; x++) {
      [result appendString:[self getX:x y:y] ? setString : unsetString];
    }
    [result appendString:lineSeparator];
  }
  return result;
}
 
- (id)copyWithZone:(NSZone *)zone {
  return [[ZXBitMatrix allocWithZone:zone] initWithWidth:self.width height:self.height rowSize:self.rowSize bits:self.bits];
}
 
@end