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.

ZXLuminanceSource.m 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 "ZXByteArray.h"
  17. #import "ZXInvertedLuminanceSource.h"
  18. #import "ZXLuminanceSource.h"
  19. @implementation ZXLuminanceSource
  20. - (id)initWithWidth:(int)width height:(int)height {
  21. if (self = [super init]) {
  22. _width = width;
  23. _height = height;
  24. _cropSupported = NO;
  25. _rotateSupported = NO;
  26. }
  27. return self;
  28. }
  29. - (ZXByteArray *)rowAtY:(int)y row:(ZXByteArray *)row {
  30. @throw [NSException exceptionWithName:NSInternalInconsistencyException
  31. reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
  32. userInfo:nil];
  33. }
  34. - (ZXByteArray *)matrix {
  35. @throw [NSException exceptionWithName:NSInternalInconsistencyException
  36. reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
  37. userInfo:nil];
  38. }
  39. - (ZXLuminanceSource *)crop:(int)left top:(int)top width:(int)width height:(int)height {
  40. @throw [NSException exceptionWithName:NSInternalInconsistencyException
  41. reason:@"This luminance source does not support cropping."
  42. userInfo:nil];
  43. }
  44. - (ZXLuminanceSource *)invert {
  45. return [[ZXInvertedLuminanceSource alloc] initWithDelegate:self];
  46. }
  47. - (ZXLuminanceSource *)rotateCounterClockwise {
  48. @throw [NSException exceptionWithName:NSInternalInconsistencyException
  49. reason:@"This luminance source does not support rotation by 90 degrees."
  50. userInfo:nil];
  51. }
  52. - (ZXLuminanceSource *)rotateCounterClockwise45 {
  53. @throw [NSException exceptionWithName:NSInternalInconsistencyException
  54. reason:@"This luminance source does not support rotation by 45 degrees."
  55. userInfo:nil];
  56. }
  57. - (NSString *)description {
  58. ZXByteArray *row = [[ZXByteArray alloc] initWithLength:self.width];
  59. NSMutableString *result = [NSMutableString stringWithCapacity:self.height * (self.width + 1)];
  60. for (int y = 0; y < self.height; y++) {
  61. row = [self rowAtY:y row:row];
  62. for (int x = 0; x < self.width; x++) {
  63. int luminance = row.array[x] & 0xFF;
  64. unichar c;
  65. if (luminance < 0x40) {
  66. c = '#';
  67. } else if (luminance < 0x80) {
  68. c = '+';
  69. } else if (luminance < 0xC0) {
  70. c = '.';
  71. } else {
  72. c = ' ';
  73. }
  74. [result appendFormat:@"%C", c];
  75. }
  76. [result appendString:@"\n"];
  77. }
  78. return result;
  79. }
  80. @end