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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 <CoreVideo/CoreVideo.h>
  17. #import "ZXByteArray.h"
  18. #import "ZXCGImageLuminanceSource.h"
  19. #import "ZXImage.h"
  20. @interface ZXCGImageLuminanceSource ()
  21. @property (nonatomic, assign, readonly) CGImageRef image;
  22. @property (nonatomic, assign, readonly) int8_t *data;
  23. @property (nonatomic, assign, readonly) size_t left;
  24. @property (nonatomic, assign, readonly) size_t top;
  25. @end
  26. @implementation ZXCGImageLuminanceSource
  27. + (CGImageRef)createImageFromBuffer:(CVImageBufferRef)buffer CF_RETURNS_RETAINED {
  28. return [self createImageFromBuffer:buffer
  29. left:0
  30. top:0
  31. width:CVPixelBufferGetWidth(buffer)
  32. height:CVPixelBufferGetHeight(buffer)];
  33. }
  34. + (CGImageRef)createImageFromBuffer:(CVImageBufferRef)buffer
  35. left:(size_t)left
  36. top:(size_t)top
  37. width:(size_t)width
  38. height:(size_t)height CF_RETURNS_RETAINED {
  39. size_t bytesPerRow = CVPixelBufferGetBytesPerRow(buffer);
  40. size_t dataWidth = CVPixelBufferGetWidth(buffer);
  41. size_t dataHeight = CVPixelBufferGetHeight(buffer);
  42. if (left + width > dataWidth ||
  43. top + height > dataHeight) {
  44. [NSException raise:NSInvalidArgumentException format:@"Crop rectangle does not fit within image data."];
  45. }
  46. size_t newBytesPerRow = ((width*4+0xf)>>4)<<4;
  47. CVPixelBufferLockBaseAddress(buffer,0);
  48. int8_t *baseAddress = (int8_t *)CVPixelBufferGetBaseAddress(buffer);
  49. size_t size = newBytesPerRow*height;
  50. int8_t *bytes = (int8_t *)malloc(size * sizeof(int8_t));
  51. if (newBytesPerRow == bytesPerRow) {
  52. memcpy(bytes, baseAddress+top*bytesPerRow, size * sizeof(int8_t));
  53. } else {
  54. for (int y=0; y<height; y++) {
  55. memcpy(bytes+y*newBytesPerRow,
  56. baseAddress+left*4+(top+y)*bytesPerRow,
  57. newBytesPerRow * sizeof(int8_t));
  58. }
  59. }
  60. CVPixelBufferUnlockBaseAddress(buffer, 0);
  61. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  62. CGContextRef newContext = CGBitmapContextCreate(bytes,
  63. width,
  64. height,
  65. 8,
  66. newBytesPerRow,
  67. colorSpace,
  68. kCGBitmapByteOrder32Little|
  69. kCGImageAlphaNoneSkipFirst);
  70. CGColorSpaceRelease(colorSpace);
  71. CGImageRef result = CGBitmapContextCreateImage(newContext);
  72. CGContextRelease(newContext);
  73. free(bytes);
  74. return result;
  75. }
  76. - (id)initWithZXImage:(ZXImage *)image
  77. left:(size_t)left
  78. top:(size_t)top
  79. width:(size_t)width
  80. height:(size_t)height {
  81. return [self initWithCGImage:image.cgimage left:left top:top width:width height:height];
  82. }
  83. - (id)initWithZXImage:(ZXImage *)image {
  84. return [self initWithCGImage:image.cgimage];
  85. }
  86. - (id)initWithCGImage:(CGImageRef)image
  87. left:(size_t)left
  88. top:(size_t)top
  89. width:(size_t)width
  90. height:(size_t)height {
  91. if (self = [super initWithWidth:(int)width height:(int)height]) {
  92. [self initializeWithImage:image left:left top:top width:width height:height];
  93. }
  94. return self;
  95. }
  96. - (id)initWithCGImage:(CGImageRef)image {
  97. return [self initWithCGImage:image left:0 top:0 width:CGImageGetWidth(image) height:CGImageGetHeight(image)];
  98. }
  99. - (id)initWithBuffer:(CVPixelBufferRef)buffer
  100. left:(size_t)left
  101. top:(size_t)top
  102. width:(size_t)width
  103. height:(size_t)height {
  104. CGImageRef image = [ZXCGImageLuminanceSource createImageFromBuffer:buffer left:left top:top width:width height:height];
  105. self = [self initWithCGImage:image];
  106. CGImageRelease(image);
  107. return self;
  108. }
  109. - (id)initWithBuffer:(CVPixelBufferRef)buffer {
  110. CGImageRef image = [ZXCGImageLuminanceSource createImageFromBuffer:buffer];
  111. self = [self initWithCGImage:image];
  112. CGImageRelease(image);
  113. return self;
  114. }
  115. - (void)dealloc {
  116. if (_image) {
  117. CGImageRelease(_image);
  118. }
  119. if (_data) {
  120. free(_data);
  121. }
  122. }
  123. - (ZXByteArray *)rowAtY:(int)y row:(ZXByteArray *)row {
  124. if (y < 0 || y >= self.height) {
  125. [NSException raise:NSInvalidArgumentException format:@"Requested row is outside the image: %d", y];
  126. }
  127. if (!row || row.length < self.width) {
  128. row = [[ZXByteArray alloc] initWithLength:self.width];
  129. }
  130. int offset = y * self.width;
  131. memcpy(row.array, self.data + offset, self.width * sizeof(int8_t));
  132. return row;
  133. }
  134. - (ZXByteArray *)matrix {
  135. int area = self.width * self.height;
  136. ZXByteArray *matrix = [[ZXByteArray alloc] initWithLength:area];
  137. memcpy(matrix.array, self.data, area * sizeof(int8_t));
  138. return matrix;
  139. }
  140. - (void)initializeWithImage:(CGImageRef)cgimage left:(size_t)left top:(size_t)top width:(size_t)width height:(size_t)height {
  141. _data = 0;
  142. _image = CGImageRetain(cgimage);
  143. _left = left;
  144. _top = top;
  145. size_t sourceWidth = CGImageGetWidth(cgimage);
  146. size_t sourceHeight = CGImageGetHeight(cgimage);
  147. size_t selfWidth = self.width;
  148. size_t selfHeight= self.height;
  149. if (left + selfWidth > sourceWidth ||
  150. top + selfHeight > sourceHeight) {
  151. [NSException raise:NSInvalidArgumentException format:@"Crop rectangle does not fit within image data."];
  152. }
  153. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  154. CGContextRef context = CGBitmapContextCreate(NULL, selfWidth, selfHeight, 8, selfWidth * 4, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
  155. CGColorSpaceRelease(colorSpace);
  156. CGContextSetAllowsAntialiasing(context, FALSE);
  157. CGContextSetInterpolationQuality(context, kCGInterpolationNone);
  158. if (top || left) {
  159. CGContextClipToRect(context, CGRectMake(0, 0, selfWidth, selfHeight));
  160. }
  161. CGContextDrawImage(context, CGRectMake(-left, -top, selfWidth, selfHeight), self.image);
  162. uint32_t *pixelData = CGBitmapContextGetData(context);
  163. _data = (int8_t *)malloc(selfWidth * selfHeight * sizeof(int8_t));
  164. dispatch_apply(selfHeight, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^(size_t idx) {
  165. size_t stripe_start = idx * selfWidth;
  166. size_t stripe_stop = stripe_start + selfWidth;
  167. for (size_t i = stripe_start; i < stripe_stop; i++) {
  168. uint32_t rgbPixelIn = pixelData[i];
  169. uint32_t rgbPixelOut = 0;
  170. uint32_t red = (rgbPixelIn >> 24) & 0xFF;
  171. uint32_t green = (rgbPixelIn >> 16) & 0xFF;
  172. uint32_t blue = (rgbPixelIn >> 8) & 0xFF;
  173. uint32_t alpha = (rgbPixelIn & 0xFF);
  174. // ImageIO premultiplies all PNGs, so we have to "un-premultiply them":
  175. // http://code.google.com/p/cocos2d-iphone/issues/detail?id=697#c26
  176. if (alpha != 0xFF) {
  177. red = red > 0 ? ((red << 20) / (alpha << 2)) >> 10 : 0;
  178. green = green > 0 ? ((green << 20) / (alpha << 2)) >> 10 : 0;
  179. blue = blue > 0 ? ((blue << 20) / (alpha << 2)) >> 10 : 0;
  180. }
  181. if (red == green && green == blue) {
  182. rgbPixelOut = red;
  183. } else {
  184. rgbPixelOut = (306 * red +
  185. 601 * green +
  186. 117 * blue +
  187. (0x200)) >> 10; // 0x200 = 1<<9, half an lsb of the result to force rounding
  188. }
  189. if (rgbPixelOut > 255) {
  190. rgbPixelOut = 255;
  191. }
  192. _data[i] = rgbPixelOut;
  193. }
  194. });
  195. CGContextRelease(context);
  196. _top = top;
  197. _left = left;
  198. }
  199. - (BOOL)rotateSupported {
  200. return YES;
  201. }
  202. - (ZXLuminanceSource *)rotateCounterClockwise {
  203. double radians = 270.0f * M_PI / 180;
  204. #if TARGET_OS_EMBEDDED || TARGET_IPHONE_SIMULATOR
  205. radians = -1 * radians;
  206. #endif
  207. int sourceWidth = self.width;
  208. int sourceHeight = self.height;
  209. CGRect imgRect = CGRectMake(0, 0, sourceWidth, sourceHeight);
  210. CGAffineTransform transform = CGAffineTransformMakeRotation(radians);
  211. CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);
  212. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  213. CGContextRef context = CGBitmapContextCreate(NULL,
  214. rotatedRect.size.width,
  215. rotatedRect.size.height,
  216. 8,
  217. 0,
  218. colorSpace,
  219. kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedFirst);
  220. CGContextSetAllowsAntialiasing(context, FALSE);
  221. CGContextSetInterpolationQuality(context, kCGInterpolationNone);
  222. CGColorSpaceRelease(colorSpace);
  223. CGContextTranslateCTM(context,
  224. +(rotatedRect.size.width/2),
  225. +(rotatedRect.size.height/2));
  226. CGContextRotateCTM(context, radians);
  227. CGContextDrawImage(context, CGRectMake(-imgRect.size.width/2,
  228. -imgRect.size.height/2,
  229. imgRect.size.width,
  230. imgRect.size.height),
  231. self.image);
  232. CGImageRef rotatedImage = CGBitmapContextCreateImage(context);
  233. CFRelease(context);
  234. ZXCGImageLuminanceSource *result = [[ZXCGImageLuminanceSource alloc] initWithCGImage:rotatedImage
  235. left:self.top
  236. top:sourceWidth - (self.left + self.width)
  237. width:self.height
  238. height:self.width];
  239. CGImageRelease(rotatedImage);
  240. return result;
  241. }
  242. - (ZXLuminanceSource *)crop:(int)left top:(int)top width:(int)width height:(int)height {
  243. CGImageRef croppedImageRef = CGImageCreateWithImageInRect(self.image, CGRectMake(left, top, width, height));
  244. ZXCGImageLuminanceSource *result = [[ZXCGImageLuminanceSource alloc] initWithCGImage:croppedImageRef];
  245. CGImageRelease(croppedImageRef);
  246. return result;
  247. }
  248. @end