aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorS. Davilla <davilla@4pi.com>2011-04-03 19:38:33 -0400
committerS. Davilla <davilla@4pi.com>2011-04-03 19:38:33 -0400
commitbd95d076d068a80a998703ef1a5a43d51b956995 (patch)
tree216d158180dbde24ccebc573d0b26ca8f012f014
parent62d31c3c004f5a62a4d7880f634c2688da5dbc0b (diff)
[ios] fixed, image rotation, thxs Memphiz
-rw-r--r--xbmc/guilib/Texture.cpp41
-rw-r--r--xbmc/osx/DarwinUtils.h3
-rw-r--r--xbmc/osx/DarwinUtils.mm29
3 files changed, 70 insertions, 3 deletions
diff --git a/xbmc/guilib/Texture.cpp b/xbmc/guilib/Texture.cpp
index 7e5f1f6d42..d6f984f498 100644
--- a/xbmc/guilib/Texture.cpp
+++ b/xbmc/guilib/Texture.cpp
@@ -29,6 +29,7 @@
#if defined(__APPLE__) && defined(__arm__)
#include <ImageIO/ImageIO.h>
#include "filesystem/File.h"
+#include "osx/DarwinUtils.h"
#endif
/************************************************************************/
@@ -213,6 +214,43 @@ bool CBaseTexture::LoadFromFile(const CStdString& texturePath, unsigned int maxW
}
CGImageRef image = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
+
+ //get the orientation of the image for displaying it correctly
+ CFDictionaryRef imagePropertiesDictionary = CGImageSourceCopyPropertiesAtIndex(imageSource,0, NULL);
+ if (imagePropertiesDictionary != nil)
+ {
+ CFNumberRef orientation = (CFNumberRef)CFDictionaryGetValue(imagePropertiesDictionary, kCGImagePropertyOrientation);
+ if (orientation != nil)
+ {
+ int switchValue = 0;
+ int rotationAngle = 0;
+ CFNumberGetValue(orientation, kCFNumberIntType, &switchValue);
+ // possible values taken from
+ // http://developer.apple.com/library/mac/#samplecode/ImageApp/Listings/ImageDoc_m.html
+ switch(switchValue)
+ {
+ case 3: //rotated 180°
+ rotationAngle = 180;
+ break;
+ case 6: //rotated 90°
+ rotationAngle = 270;
+ break;
+ case 8: //rotated 270°
+ rotationAngle = 90;
+ break;
+ }
+
+ //rotate the image if needed
+ if (rotationAngle != 0)
+ {
+ CLog::Log(LOGDEBUG,"Rotating the image about %i degrees", rotationAngle);
+ CGImageRef rotatedImage = CGImageCreateRotatedByAngle(image, (float)rotationAngle);
+ CFRelease(image);
+ image = rotatedImage;
+ }
+ }
+ }
+
CFRelease(imageSource);
unsigned int width = CGImageGetWidth(image);
@@ -220,9 +258,6 @@ bool CBaseTexture::LoadFromFile(const CStdString& texturePath, unsigned int maxW
m_hasAlpha = (CGImageGetAlphaInfo(image) != kCGImageAlphaNone);
-// not sure what to do here :)
-// if (autoRotate && image.exifInfo.Orientation)
-// m_orientation = image.exifInfo.Orientation - 1;
if (originalWidth)
*originalWidth = width;
if (originalHeight)
diff --git a/xbmc/osx/DarwinUtils.h b/xbmc/osx/DarwinUtils.h
index daab73da22..faf6952a4d 100644
--- a/xbmc/osx/DarwinUtils.h
+++ b/xbmc/osx/DarwinUtils.h
@@ -32,6 +32,9 @@ extern "C"
int GetDarwinExecutablePath(char* path, uint32_t *pathsize);
bool DarwinHasVideoToolboxDecoder(void);
+
+ typedef struct CGImage *CGImageRef;
+ CGImageRef CGImageCreateRotatedByAngle(CGImageRef imgRef, float angle);
#ifdef __cplusplus
}
#endif
diff --git a/xbmc/osx/DarwinUtils.mm b/xbmc/osx/DarwinUtils.mm
index a9121065ca..0094c29347 100644
--- a/xbmc/osx/DarwinUtils.mm
+++ b/xbmc/osx/DarwinUtils.mm
@@ -196,4 +196,33 @@ bool DarwinHasVideoToolboxDecoder(void)
return bDecoderAvailable;
}
+
+CGImageRef CGImageCreateRotatedByAngle(CGImageRef imgRef, float angle)
+{
+ // image rotation function taken from
+ // https://gist.github.com/585377
+ float angleInRadians = angle * (M_PI / 180);
+ float width = CGImageGetWidth(imgRef);
+ float height = CGImageGetHeight(imgRef);
+
+ CGRect imgRect = CGRectMake(0, 0, width, height);
+ CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);
+ CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);
+
+ CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
+ CGContextRef bmContext = CGBitmapContextCreate(NULL,rotatedRect.size.width, rotatedRect.size.height,
+ 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
+ CGContextSetAllowsAntialiasing(bmContext, 1);
+ CGContextSetInterpolationQuality(bmContext, kCGInterpolationHigh);
+ CGColorSpaceRelease(colorSpace);
+ CGContextTranslateCTM(bmContext, +(rotatedRect.size.width/2), +(rotatedRect.size.height/2));
+ CGContextRotateCTM(bmContext, angleInRadians);
+ CGContextDrawImage(bmContext, CGRectMake(-width/2, -height/2, width, height), imgRef);
+
+ CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext);
+ CFRelease(bmContext);
+
+ return rotatedImage;
+}
+
#endif