Permalink
Browse files

+ Extract out orientation logic and add it to with_image_from_memory.…

… (bhenderson)

[git-p4: depot-paths = "//src/image_science/dev/": change = 7991]
  • Loading branch information...
1 parent c9e8dc2 commit b759dbc778c6e528e07d2494ed1b6967c1967685 @zenspider zenspider committed Nov 23, 2012
Showing with 37 additions and 16 deletions.
  1. +23 −16 lib/image_science.rb
  2. BIN test/portrait.jpg
  3. +14 −0 test/test_image_science.rb
View
@@ -149,6 +149,27 @@ def cropped_thumbnail(size) # :yields: image
}
END
+ builder.prefix <<-"END"
+ FIBITMAP* ReOrient(FIBITMAP *bitmap) {
+ FITAG *tagValue = NULL;
+ FreeImage_GetMetadata(FIMD_EXIF_MAIN, bitmap, "Orientation", &tagValue);
+ switch (tagValue == NULL ? 0 : *((short *) FreeImage_GetTagValue(tagValue))) {
+ case 6:
+ bitmap = FreeImage_RotateClassic(bitmap, 270);
+ break;
+ case 3:
+ bitmap = FreeImage_RotateClassic(bitmap, 180);
+ break;
+ case 8:
+ bitmap = FreeImage_RotateClassic(bitmap, 90);
+ break;
+ default:
+ break;
+ }
+ return bitmap;
+ }
+ END
+
builder.add_to_init "FreeImage_SetOutputMessage(FreeImageErrorHandler);"
builder.c_singleton <<-"END"
@@ -163,22 +184,7 @@ def cropped_thumbnail(size) # :yields: image
VALUE result = Qnil;
flags = fif == FIF_JPEG ? JPEG_ACCURATE : 0;
if ((bitmap = FreeImage_Load(fif, input, flags))) {
- FITAG *tagValue = NULL;
- FreeImage_GetMetadata(FIMD_EXIF_MAIN, bitmap, "Orientation", &tagValue);
- switch (tagValue == NULL ? 0 : *((short *) FreeImage_GetTagValue(tagValue))) {
- case 6:
- bitmap = FreeImage_RotateClassic(bitmap, 270);
- break;
- case 3:
- bitmap = FreeImage_RotateClassic(bitmap, 180);
- break;
- case 8:
- bitmap = FreeImage_RotateClassic(bitmap, 90);
- break;
- default:
- break;
- }
-
+ bitmap = ReOrient(bitmap);
result = wrap_and_yield(bitmap, self, fif);
}
return result;
@@ -216,6 +222,7 @@ def cropped_thumbnail(size) # :yields: image
bitmap = FreeImage_LoadFromMemory(fif, stream, flags);
FreeImage_CloseMemory(stream);
if (bitmap) {
+ bitmap = ReOrient(bitmap);
result = wrap_and_yield(bitmap, self, fif);
}
return result;
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -174,4 +174,18 @@ def test_thumbnail
end
end
+ def test_auto_rotate_from_file
+ ImageScience.with_image "test/portrait.jpg" do |img|
+ assert_equal 50, img.height
+ assert_equal 38, img.width
+ end
+ end
+
+ def test_auto_rotate_from_memory
+ data = File.new("test/portrait.jpg").binmode.read
+ ImageScience.with_image_from_memory data do |img|
+ assert_equal 50, img.height
+ assert_equal 38, img.width
+ end
+ end
end

0 comments on commit b759dbc

Please sign in to comment.