選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ZXImage.m 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "ZXBitMatrix.h"
  17. #import "ZXImage.h"
  18. #if TARGET_OS_EMBEDDED || TARGET_IPHONE_SIMULATOR
  19. #import <ImageIO/ImageIO.h>
  20. #endif
  21. @implementation ZXImage
  22. - (ZXImage *)initWithCGImageRef:(CGImageRef)image {
  23. if (self = [super init]) {
  24. _cgimage = CGImageRetain(image);
  25. }
  26. return self;
  27. }
  28. - (ZXImage *)initWithURL:(NSURL const *)url {
  29. if (self = [super init]) {
  30. CGDataProviderRef provider = CGDataProviderCreateWithURL((__bridge CFURLRef)url);
  31. if (provider) {
  32. CGImageSourceRef source = CGImageSourceCreateWithDataProvider(provider, 0);
  33. if (source) {
  34. _cgimage = CGImageSourceCreateImageAtIndex(source, 0, 0);
  35. CFRelease(source);
  36. }
  37. CGDataProviderRelease(provider);
  38. }
  39. }
  40. return self;
  41. }
  42. - (size_t)width {
  43. return CGImageGetWidth(self.cgimage);
  44. }
  45. - (size_t)height {
  46. return CGImageGetHeight(self.cgimage);
  47. }
  48. - (void)dealloc {
  49. if (_cgimage) {
  50. CGImageRelease(_cgimage);
  51. }
  52. }
  53. + (ZXImage *)imageWithMatrix:(ZXBitMatrix *)matrix {
  54. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
  55. CGFloat blackComponents[] = {0.0f, 1.0f};
  56. CGColorRef black = CGColorCreate(colorSpace, blackComponents);
  57. CGFloat whiteComponents[] = {1.0f, 1.0f};
  58. CGColorRef white = CGColorCreate(colorSpace, whiteComponents);
  59. CFRelease(colorSpace);
  60. ZXImage *result = [self imageWithMatrix:matrix onColor:black offColor:white];
  61. CGColorRelease(white);
  62. CGColorRelease(black);
  63. return result;
  64. }
  65. + (ZXImage *)imageWithMatrix:(ZXBitMatrix *)matrix onColor:(CGColorRef)onColor offColor:(CGColorRef)offColor {
  66. int8_t onIntensities[4], offIntensities[4];
  67. [self setColorIntensities:onIntensities color:onColor];
  68. [self setColorIntensities:offIntensities color:offColor];
  69. int width = matrix.width;
  70. int height = matrix.height;
  71. int8_t *bytes = (int8_t *)malloc(width * height * 4);
  72. for (int y = 0; y < height; y++) {
  73. for (int x = 0; x < width; x++) {
  74. BOOL bit = [matrix getX:x y:y];
  75. for (int i = 0; i < 4; i++) {
  76. int8_t intensity = bit ? onIntensities[i] : offIntensities[i];
  77. bytes[y * width * 4 + x * 4 + i] = intensity;
  78. }
  79. }
  80. }
  81. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  82. CGContextRef c = CGBitmapContextCreate(bytes, width, height, 8, 4 * width, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);
  83. CFRelease(colorSpace);
  84. CGImageRef image = CGBitmapContextCreateImage(c);
  85. CFRelease(c);
  86. free(bytes);
  87. ZXImage *zxImage = [[ZXImage alloc] initWithCGImageRef:image];
  88. CFRelease(image);
  89. return zxImage;
  90. }
  91. + (void)setColorIntensities:(int8_t *)intensities color:(CGColorRef)color {
  92. memset(intensities, 0, 4);
  93. size_t numberOfComponents = CGColorGetNumberOfComponents(color);
  94. const CGFloat *components = CGColorGetComponents(color);
  95. if (numberOfComponents == 4) {
  96. for (int i = 0; i < 4; i++) {
  97. intensities[i] = components[i] * 255;
  98. }
  99. } else if (numberOfComponents == 2) {
  100. for (int i = 0; i < 3; i++) {
  101. intensities[i] = components[0] * 255;
  102. }
  103. intensities[3] = components[1] * 255;
  104. }
  105. }
  106. @end