Skip to content

Commit

Permalink
Merge pull request gali8#205 from ws233/noRotation
Browse files Browse the repository at this point in the history
Removed need for image rotation in setImage.
  • Loading branch information
Kevin Conley committed Aug 15, 2015
2 parents a4899f3 + 259ff3a commit 982bd4f
Show file tree
Hide file tree
Showing 20 changed files with 201 additions and 97 deletions.
8 changes: 0 additions & 8 deletions Tesseract OCR iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
objects = {

/* Begin PBXBuildFile section */
413F03321A5FBBDD000C194B /* UIImage+G8FixOrientation.h in Headers */ = {isa = PBXBuildFile; fileRef = 413F03301A5FBBDD000C194B /* UIImage+G8FixOrientation.h */; };
413F03331A5FBBDD000C194B /* UIImage+G8FixOrientation.m in Sources */ = {isa = PBXBuildFile; fileRef = 413F03311A5FBBDD000C194B /* UIImage+G8FixOrientation.m */; };
73C0A7961A5932C400D823D4 /* G8Tesseract.h in Headers */ = {isa = PBXBuildFile; fileRef = 64A029D617307CD0002B12E7 /* G8Tesseract.h */; settings = {ATTRIBUTES = (Public, ); }; };
73C0A7971A5932C800D823D4 /* G8Tesseract.mm in Sources */ = {isa = PBXBuildFile; fileRef = 64A029D717307CD0002B12E7 /* G8Tesseract.mm */; };
73C0A7981A5932CC00D823D4 /* G8TesseractDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 4141211F1A4C578800583ED4 /* G8TesseractDelegate.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -331,8 +329,6 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
413F03301A5FBBDD000C194B /* UIImage+G8FixOrientation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+G8FixOrientation.h"; sourceTree = "<group>"; };
413F03311A5FBBDD000C194B /* UIImage+G8FixOrientation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+G8FixOrientation.m"; sourceTree = "<group>"; };
4141211F1A4C578800583ED4 /* G8TesseractDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = G8TesseractDelegate.h; sourceTree = "<group>"; };
418997A71A42CC8B00D6477C /* G8Constants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = G8Constants.h; sourceTree = "<group>"; };
41A95DE81A3AF39B0085093C /* G8TesseractParameters.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = G8TesseractParameters.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -747,8 +743,6 @@
418997A71A42CC8B00D6477C /* G8Constants.h */,
6490748A198A5A5600D728CC /* UIImage+G8Filters.h */,
6490748B198A5A5600D728CC /* UIImage+G8Filters.m */,
413F03301A5FBBDD000C194B /* UIImage+G8FixOrientation.h */,
413F03311A5FBBDD000C194B /* UIImage+G8FixOrientation.m */,
41C7E8231A3F0682000DC42B /* Core */,
41C7E8211A3F0650000DC42B /* Readme */,
64A0293017307C1D002B12E7 /* Supporting Files */,
Expand Down Expand Up @@ -1171,7 +1165,6 @@
F1A7AAA71B334C7200FE9650 /* edgblob.h in Headers */,
F1A7AAD61B334C7200FE9650 /* intfeaturespace.h in Headers */,
F1A7AAD91B334C7200FE9650 /* intproto.h in Headers */,
413F03321A5FBBDD000C194B /* UIImage+G8FixOrientation.h in Headers */,
F1A7AAB81B334C7200FE9650 /* fileerr.h in Headers */,
F1A7AB331B334C7300FE9650 /* scanedg.h in Headers */,
F1A7AB471B334C7300FE9650 /* stringrenderer.h in Headers */,
Expand Down Expand Up @@ -1539,7 +1532,6 @@
73C0A7A11A59330E00D823D4 /* UIImage+G8Filters.m in Sources */,
73C0A79C1A5932F500D823D4 /* G8RecognitionOperation.m in Sources */,
73C0A7971A5932C800D823D4 /* G8Tesseract.mm in Sources */,
413F03331A5FBBDD000C194B /* UIImage+G8FixOrientation.m in Sources */,
73C0A79E1A5932FD00D823D4 /* G8TesseractParameters.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
118 changes: 104 additions & 14 deletions TesseractOCR/G8Tesseract.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#import "G8Tesseract.h"

#import "UIImage+G8Filters.h"
#import "UIImage+G8FixOrientation.h"
#import "G8TesseractParameters.h"
#import "G8Constants.h"
#import "G8RecognizedBlock.h"
Expand Down Expand Up @@ -375,8 +374,6 @@ - (void)setImage:(UIImage *)image
return;
}

image = [image fixOrientation];

self.imageSize = image.size; //self.imageSize used in the characterBoxes method

Pix *pix = nullptr;
Expand Down Expand Up @@ -819,14 +816,19 @@ - (Pix *)pixForImage:(UIImage *)image
const UInt8 *pixels = CFDataGetBytePtr(imageData);

size_t bitsPerPixel = CGImageGetBitsPerPixel(cgImage);
size_t bytesPerPixel = bitsPerPixel / 8;
size_t bytesPerRow = CGImageGetBytesPerRow(cgImage);

int bpp = MAX(1, (int)bitsPerPixel);
Pix *pix = pixCreate(width, height, bpp == 24 ? 32 : bpp);
l_uint32 *data = pixGetData(pix);
int wpl = pixGetWpl(pix);

void (^copyBlock)(l_uint32 *toAddr, NSUInteger toOffset, const UInt8 *fromAddr, NSUInteger fromOffset) = nil;
switch (bpp) {

#if 0 // BPP1 start. Uncomment this if UIImage can support 1bpp someday
// Just a reference for the copyBlock
case 1:
for (int y = 0; y < height; ++y, data += wpl, pixels += bytesPerRow) {
for (int x = 0; x < width; ++x) {
Expand All @@ -840,15 +842,16 @@ - (Pix *)pixForImage:(UIImage *)image
}
break;
#endif // BPP1 end
case 8:
// Greyscale just copies the bytes in the right order.
for (int y = 0; y < height; ++y, data += wpl, pixels += bytesPerRow) {
for (int x = 0; x < width; ++x) {
SET_DATA_BYTE(data, x, pixels[x]);
}
}

case 8: {
copyBlock = ^(l_uint32 *toAddr, NSUInteger toOffset, const UInt8 *fromAddr, NSUInteger fromOffset) {
SET_DATA_BYTE(toAddr, toOffset, fromAddr[fromOffset]);
};
break;
}

#if 0 // BPP24 start. Uncomment this if UIImage can support 24bpp someday
// Just a reference for the copyBlock
case 24:
// Put the colors in the correct places in the line buffer.
for (int y = 0; y < height; ++y, pixels += bytesPerRow) {
Expand All @@ -860,19 +863,106 @@ - (Pix *)pixForImage:(UIImage *)image
}
break;
#endif // BPP24 end
case 32:

case 32: {
copyBlock = ^(l_uint32 *toAddr, NSUInteger toOffset, const UInt8 *fromAddr, NSUInteger fromOffset) {
toAddr[toOffset] = (fromAddr[fromOffset] << 24) | (fromAddr[fromOffset + 1] << 16) |
(fromAddr[fromOffset + 2] << 8) | fromAddr[fromOffset + 3];
};
break;
}

default:
NSLog(@"Cannot convert image to Pix with bpp = %d", bpp); // LCOV_EXCL_LINE
}

switch (image.imageOrientation) {
case UIImageOrientationUp:
// Maintain byte order consistency across different endianness.
for (int y = 0; y < height; ++y, pixels += bytesPerRow, data += wpl) {
for (int x = 0; x < width; ++x) {
data[x] = (pixels[x * 4] << 24) | (pixels[x * 4 + 1] << 16) |
(pixels[x * 4 + 2] << 8) | pixels[x * 4 + 3];
copyBlock(data, x, pixels, x * bytesPerPixel);
}
}
break;

case UIImageOrientationUpMirrored:
// Maintain byte order consistency across different endianness.
for (int y = 0; y < height; ++y, pixels += bytesPerRow, data += wpl) {
int maxX = width - 1;
for (int x = maxX; x >= 0; --x) {
copyBlock(data, maxX - x, pixels, x * bytesPerPixel);
}
}
break;

case UIImageOrientationDown:
// Maintain byte order consistency across different endianness.
pixels += (height - 1) * bytesPerRow;
for (int y = height - 1; y >= 0; --y, pixels -= bytesPerRow, data += wpl) {
int maxX = width - 1;
for (int x = maxX; x >= 0; --x) {
copyBlock(data, maxX - x, pixels, x * bytesPerPixel);
}
}
break;

case UIImageOrientationDownMirrored:
// Maintain byte order consistency across different endianness.
pixels += (height - 1) * bytesPerRow;
for (int y = height - 1; y >= 0; --y, pixels -= bytesPerRow, data += wpl) {
for (int x = 0; x < width; ++x) {
copyBlock(data, x, pixels, x * bytesPerPixel);
}
}
break;

case UIImageOrientationLeft:
// Maintain byte order consistency across different endianness.
for (int x = 0; x < height; ++x, data += wpl) {
int maxY = width - 1;
for (int y = maxY; y >= 0; --y) {
int x0 = y * (int)bytesPerRow + x * bytesPerPixel;
copyBlock(data, maxY - y, pixels, x0);
}
}
break;

case UIImageOrientationLeftMirrored:
// Maintain byte order consistency across different endianness.
for (int x = height - 1; x >= 0; --x, data += wpl) {
int maxY = width - 1;
for (int y = maxY; y >= 0; --y) {
int x0 = y * (int)bytesPerRow + x * bytesPerPixel;
copyBlock(data, maxY - y, pixels, x0);
}
}
break;

case UIImageOrientationRight:
// Maintain byte order consistency across different endianness.
for (int x = height - 1; x >=0; --x, data += wpl) {
for (int y = 0; y < width; ++y) {
int x0 = y * (int)bytesPerRow + x * bytesPerPixel;
copyBlock(data, y, pixels, x0);
}
}
break;

case UIImageOrientationRightMirrored:
// Maintain byte order consistency across different endianness.
for (int x = 0; x < height; ++x, data += wpl) {
for (int y = 0; y < width; ++y) {
int x0 = y * (int)bytesPerRow + x * bytesPerPixel;
copyBlock(data, y, pixels, x0);
}
}
break;

default:
NSLog(@"Cannot convert image to Pix with bpp = %d", bpp); // LCOV_EXCL_LINE
break; // LCOV_EXCL_LINE
}

pixSetYRes(pix, (l_int32)self.sourceResolution);

CFRelease(imageData);
Expand Down
19 changes: 0 additions & 19 deletions TesseractOCR/UIImage+G8FixOrientation.h

This file was deleted.

34 changes: 0 additions & 34 deletions TesseractOCR/UIImage+G8FixOrientation.m

This file was deleted.

Loading

0 comments on commit 982bd4f

Please sign in to comment.