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.

ZXInvertedLuminanceSource.m 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. @interface ZXInvertedLuminanceSource ()
  19. @property (nonatomic, weak, readonly) ZXLuminanceSource *delegate;
  20. @end
  21. @implementation ZXInvertedLuminanceSource
  22. - (id)initWithDelegate:(ZXLuminanceSource *)delegate {
  23. self = [super initWithWidth:delegate.width height:delegate.height];
  24. if (self) {
  25. _delegate = delegate;
  26. }
  27. return self;
  28. }
  29. - (ZXByteArray *)rowAtY:(int)y row:(ZXByteArray *)row {
  30. row = [self.delegate rowAtY:y row:row];
  31. int width = self.width;
  32. int8_t *rowArray = row.array;
  33. for (int i = 0; i < width; i++) {
  34. rowArray[i] = (int8_t) (255 - (rowArray[i] & 0xFF));
  35. }
  36. return row;
  37. }
  38. - (ZXByteArray *)matrix {
  39. ZXByteArray *matrix = [self.delegate matrix];
  40. int length = self.width * self.height;
  41. ZXByteArray *invertedMatrix = [[ZXByteArray alloc] initWithLength:length];
  42. int8_t *invertedMatrixArray = invertedMatrix.array;
  43. int8_t *matrixArray = matrix.array;
  44. for (int i = 0; i < length; i++) {
  45. invertedMatrixArray[i] = (int8_t) (255 - (matrixArray[i] & 0xFF));
  46. }
  47. return invertedMatrix;
  48. }
  49. - (BOOL)cropSupported {
  50. return self.delegate.cropSupported;
  51. }
  52. - (ZXLuminanceSource *)crop:(int)left top:(int)top width:(int)aWidth height:(int)aHeight {
  53. return [[ZXInvertedLuminanceSource alloc] initWithDelegate:[self.delegate crop:left top:top width:aWidth height:aHeight]];
  54. }
  55. - (BOOL)rotateSupported {
  56. return self.delegate.rotateSupported;
  57. }
  58. /**
  59. * @return original delegate ZXLuminanceSource since invert undoes itself
  60. */
  61. - (ZXLuminanceSource *)invert {
  62. return self.delegate;
  63. }
  64. - (ZXLuminanceSource *)rotateCounterClockwise {
  65. return [[ZXInvertedLuminanceSource alloc] initWithDelegate:[self.delegate rotateCounterClockwise]];
  66. }
  67. - (ZXLuminanceSource *)rotateCounterClockwise45 {
  68. return [[ZXInvertedLuminanceSource alloc] initWithDelegate:[self.delegate rotateCounterClockwise45]];
  69. }
  70. @end