<commit_before>//
//  Decoder.m
//  ZXing
//
//  Created by <NAME> on 31/03/2008.
//
/*
 * Copyright 2008 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 "Decoder.h"
#import "TwoDDecoderResult.h"
#import "FormatReader.h"

#include <zxing/BinaryBitmap.h>
#include <zxing/ReaderException.h>
#include <zxing/common/IllegalArgumentException.h>
#include <zxing/common/GlobalHistogramBinarizer.h>
#include <zxing/common/GreyscaleLuminanceSource.h>

using namespace zxing;

@implementation Decoder

@synthesize image;
@synthesize cropRect;
@synthesize subsetImage;
@synthesize subsetData;
@synthesize subsetWidth;
@synthesize subsetHeight;
@synthesize subsetBytesPerRow;
@synthesize delegate;
@synthesize operationQueue;
@synthesize readers;


- (id)init {
  if (self = [super init]) {
    NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
    self.operationQueue = opQueue;
    [opQueue release];
  }
  return self;
}
- (void)willDecodeImage {
  if ([self.delegate respondsToSelector:@selector(decoder:willDecodeImage:usingSubset:)]) {
    [self.delegate decoder:self willDecodeImage:self.image usingSubset:self.subsetImage];
  }
}

- (void)progressDecodingImage:(NSString *)progress {
  if ([self.delegate respondsToSelector:@selector(decoder:decodingImage:usingSubset:progress:)]) {
    [self.delegate decoder:self decodingImage:self.image usingSubset:self.subsetImage progress:progress];
  }
}

- (void)didDecodeImage:(TwoDDecoderResult *)result {

  if ([self.delegate respondsToSelector:@selector(decoder:didDecodeImage:usingSubset:withResult:)]) {
    [self.delegate decoder:self didDecodeImage:self.image usingSubset:self.subsetImage withResult:result];
  }
	
  [result release];
}

- (void)failedToDecodeImage:(NSString *)reason {

  if ([self.delegate respondsToSelector:@selector(decoder:failedToDecodeImage:usingSubset:reason:)]) {
    [self.delegate decoder:self failedToDecodeImage:self.image usingSubset:self.subsetImage reason:reason];
  }
}

#define SUBSET_SIZE 320.0
- (void) prepareSubset {
  CGSize size = [image size];
#ifdef DEBUG
  NSLog(@"decoding: image is (%.1f x %.1f), cropRect is (%.1f,%.1f)x(%.1f,%.1f)", size.width, size.height,
      cropRect.origin.x, cropRect.origin.y, cropRect.size.width, cropRect.size.height);
#endif
  float scale = fminf(1.0f, fmaxf(SUBSET_SIZE / cropRect.size.width, SUBSET_SIZE / cropRect.size.height));
  CGPoint offset = CGPointMake(-cropRect.origin.x, -cropRect.origin.y);
#ifdef DEBUG
  NSLog(@"  offset = (%.1f, %.1f), scale = %.3f", offset.x, offset.y, scale);
#endif
  
  subsetWidth = cropRect.size.width * scale;
  subsetHeight = cropRect.size.height * scale;
  
  subsetBytesPerRow = ((subsetWidth + 0xf) >> 4) << 4;
#ifdef DEBUG
  NSLog(@"decoding: image to decode is (%d x %d) (%d bytes/row)", subsetWidth, subsetHeight, subsetBytesPerRow);
#endif
  
  subsetData = (unsigned char *)malloc(subsetBytesPerRow * subsetHeight);
#ifdef DEBUG
  NSLog(@"allocated %d bytes of memory", subsetBytesPerRow * subsetHeight);
#endif
  
  CGColorSpaceRef grayColorSpace = CGColorSpaceCreateDeviceGray();
  
  CGContextRef ctx = 
  CGBitmapContextCreate(subsetData, subsetWidth, subsetHeight, 
              8, subsetBytesPerRow, grayColorSpace, 
              kCGImageAlphaNone);
  CGColorSpaceRelease(grayColorSpace);
  CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
  CGContextSetAllowsAntialiasing(ctx, false);
  // adjust the coordinate system
  CGContextTranslateCTM(ctx, 0.0, subsetHeight);
  CGContextScaleCTM(ctx, 1.0, -1.0);  
  
#ifdef DEBUG
  NSLog(@"created %dx%d bitmap context", subsetWidth, subsetHeight);
#endif
  
  UIGraphicsPushContext(ctx);
  CGRect rect = CGRectMake(offset.x * scale, offset.y * scale, scale * size.width, scale * size.height);
#ifdef DEBUG
  NSLog(@"rect for image = (%.1f,%.1f)x(%.1f,%.1f)", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
#endif
  [image drawInRect:rect];
  UIGraphicsPopContext();
  
#ifdef DEBUG
  NSLog(@"drew image into %d(%d)x%d  bitmap context", subsetWidth, subsetBytesPerRow, subsetHeight);
#endif
  CGContextFlush(ctx);
#ifdef DEBUG
  NSLog(@"flushed context");
#endif
    
  CGImageRef subsetImageRef = CGBitmapContextCreateImage(ctx);
#ifdef DEBUG
  NSLog(@"created CGImage from context");
#endif
  
  self.subsetImage = [UIImage imageWithCGImage:subsetImageRef];
  CGImageRelease(subsetImageRef);
  
  CGContextRelease(ctx);
#ifdef DEBUG
  NSLog(@"released context");  
#endif
}  

- (void)decode:(id)arg {
  NSAutoreleasePool* mainpool = [[NSAutoreleasePool alloc] init];
  { 
    //NSSet *formatReaders = [FormatReader formatReaders];
    NSSet *formatReaders = self.readers;
    Ref<LuminanceSource> source 
           (new GreyscaleLuminanceSource(subsetData, subsetBytesPerRow, subsetHeight, 0, 0, subsetWidth, subsetHeight));
    Ref<Binarizer> binarizer (new GlobalHistogramBinarizer(source));
    Ref<BinaryBitmap> grayImage (new BinaryBitmap(binarizer));
#ifdef DEBUG
    NSLog(@"created GrayBytesMonochromeBitmapSource", subsetWidth, subsetHeight);
    NSLog(@"grayImage count = %d", grayImage->count());
#endif
    
    TwoDDecoderResult *decoderResult = nil;
    
#ifdef TRY_ROTATIONS
    for (int i = 0; !decoderResult && i < 4; i++) {
#endif
      for (FormatReader *reader in formatReaders) {
        NSAutoreleasePool *secondarypool = [[NSAutoreleasePool alloc] init];
        try {
  #ifdef DEBUG
          NSLog(@"decoding gray image");
  #endif  
          Ref<Result> result([reader decode:grayImage]);<commit_msg>fix log to reflect reality<commit_after>//
//  Decoder.m
//  ZXing
//
//  Created by <NAME> on 31/03/2008.
//
/*
 * Copyright 2008 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 "Decoder.h"
#import "TwoDDecoderResult.h"
#import "FormatReader.h"

#include <zxing/BinaryBitmap.h>
#include <zxing/ReaderException.h>
#include <zxing/common/IllegalArgumentException.h>
#include <zxing/common/GlobalHistogramBinarizer.h>
#include <zxing/common/GreyscaleLuminanceSource.h>

using namespace zxing;

@implementation Decoder

@synthesize image;
@synthesize cropRect;
@synthesize subsetImage;
@synthesize subsetData;
@synthesize subsetWidth;
@synthesize subsetHeight;
@synthesize subsetBytesPerRow;
@synthesize delegate;
@synthesize operationQueue;
@synthesize readers;


- (id)init {
  if (self = [super init]) {
    NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
    self.operationQueue = opQueue;
    [opQueue release];
  }
  return self;
}
- (void)willDecodeImage {
  if ([self.delegate respondsToSelector:@selector(decoder:willDecodeImage:usingSubset:)]) {
    [self.delegate decoder:self willDecodeImage:self.image usingSubset:self.subsetImage];
  }
}

- (void)progressDecodingImage:(NSString *)progress {
  if ([self.delegate respondsToSelector:@selector(decoder:decodingImage:usingSubset:progress:)]) {
    [self.delegate decoder:self decodingImage:self.image usingSubset:self.subsetImage progress:progress];
  }
}

- (void)didDecodeImage:(TwoDDecoderResult *)result {

  if ([self.delegate respondsToSelector:@selector(decoder:didDecodeImage:usingSubset:withResult:)]) {
    [self.delegate decoder:self didDecodeImage:self.image usingSubset:self.subsetImage withResult:result];
  }
	
  [result release];
}

- (void)failedToDecodeImage:(NSString *)reason {

  if ([self.delegate respondsToSelector:@selector(decoder:failedToDecodeImage:usingSubset:reason:)]) {
    [self.delegate decoder:self failedToDecodeImage:self.image usingSubset:self.subsetImage reason:reason];
  }
}

#define SUBSET_SIZE 320.0
- (void) prepareSubset {
  CGSize size = [image size];
#ifdef DEBUG
  NSLog(@"decoding: image is (%.1f x %.1f), cropRect is (%.1f,%.1f)x(%.1f,%.1f)", size.width, size.height,
      cropRect.origin.x, cropRect.origin.y, cropRect.size.width, cropRect.size.height);
#endif
  float scale = fminf(1.0f, fmaxf(SUBSET_SIZE / cropRect.size.width, SUBSET_SIZE / cropRect.size.height));
  CGPoint offset = CGPointMake(-cropRect.origin.x, -cropRect.origin.y);
#ifdef DEBUG
  NSLog(@"  offset = (%.1f, %.1f), scale = %.3f", offset.x, offset.y, scale);
#endif
  
  subsetWidth = cropRect.size.width * scale;
  subsetHeight = cropRect.size.height * scale;
  
  subsetBytesPerRow = ((subsetWidth + 0xf) >> 4) << 4;
#ifdef DEBUG
  NSLog(@"decoding: image to decode is (%d x %d) (%d bytes/row)", subsetWidth, subsetHeight, subsetBytesPerRow);
#endif
  
  subsetData = (unsigned char *)malloc(subsetBytesPerRow * subsetHeight);
#ifdef DEBUG
  NSLog(@"allocated %d bytes of memory", subsetBytesPerRow * subsetHeight);
#endif
  
  CGColorSpaceRef grayColorSpace = CGColorSpaceCreateDeviceGray();
  
  CGContextRef ctx = 
  CGBitmapContextCreate(subsetData, subsetWidth, subsetHeight, 
              8, subsetBytesPerRow, grayColorSpace, 
              kCGImageAlphaNone);
  CGColorSpaceRelease(grayColorSpace);
  CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
  CGContextSetAllowsAntialiasing(ctx, false);
  // adjust the coordinate system
  CGContextTranslateCTM(ctx, 0.0, subsetHeight);
  CGContextScaleCTM(ctx, 1.0, -1.0);  
  
#ifdef DEBUG
  NSLog(@"created %dx%d bitmap context", subsetWidth, subsetHeight);
#endif
  
  UIGraphicsPushContext(ctx);
  CGRect rect = CGRectMake(offset.x * scale, offset.y * scale, scale * size.width, scale * size.height);
#ifdef DEBUG
  NSLog(@"rect for image = (%.1f,%.1f)x(%.1f,%.1f)", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
#endif
  [image drawInRect:rect];
  UIGraphicsPopContext();
  
#ifdef DEBUG
  NSLog(@"drew image into %d(%d)x%d  bitmap context", subsetWidth, subsetBytesPerRow, subsetHeight);
#endif
  CGContextFlush(ctx);
#ifdef DEBUG
  NSLog(@"flushed context");
#endif
    
  CGImageRef subsetImageRef = CGBitmapContextCreateImage(ctx);
#ifdef DEBUG
  NSLog(@"created CGImage from context");
#endif
  
  self.subsetImage = [UIImage imageWithCGImage:subsetImageRef];
  CGImageRelease(subsetImageRef);
  
  CGContextRelease(ctx);
#ifdef DEBUG
  NSLog(@"released context");  
#endif
}  

- (void)decode:(id)arg {
  NSAutoreleasePool* mainpool = [[NSAutoreleasePool alloc] init];
  { 
    //NSSet *formatReaders = [FormatReader formatReaders];
    NSSet *formatReaders = self.readers;
    Ref<LuminanceSource> source 
           (new GreyscaleLuminanceSource(subsetData, subsetBytesPerRow, subsetHeight, 0, 0, subsetWidth, subsetHeight));
    Ref<Binarizer> binarizer (new GlobalHistogramBinarizer(source));
    Ref<BinaryBitmap> grayImage (new BinaryBitmap(binarizer));
#ifdef DEBUG
    NSLog(@"created GreyscaleLuminanceSource", subsetWidth, subsetHeight);
    NSLog(@"grayImage count = %d", grayImage->count());
#endif
    
    TwoDDecoderResult *decoderResult = nil;
    
#ifdef TRY_ROTATIONS
    for (int i = 0; !decoderResult && i < 4; i++) {
#endif
      for (FormatReader *reader in formatReaders) {
        NSAutoreleasePool *secondarypool = [[NSAutoreleasePool alloc] init];
        try {
  #ifdef DEBUG
          NSLog(@"decoding gray image");
  #endif  
          Ref<Result> result([reader decode:grayImage]);
{'fraction_non_alphanumeric': 0.12321960914210003, 'fraction_numerical': 0.008115269956939383, 'mean_word_length': 4.814636494944632, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5034099', 'n_tokens_mistral': 4052, 'n_tokens_neox': 3830, 'n_words': 911}