Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8afb177

Browse files
RamRaj Seshasankaran
authored andcommitted
GLKTexture load files without assuming it's PNG (#2106)
* GLKTexture load files without assuming it's PNG * update others * intro of a new function * update name
1 parent a01904f commit 8afb177

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

Frameworks/CoreGraphics/CGImage.mm

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ inline WICPixelFormatGUID PixelFormat() const {
225225
}
226226

227227
~__CGImage() {
228-
for (auto listener: _imageDestructionListeners) {
228+
for (auto listener : _imageDestructionListeners) {
229229
listener(this);
230230
}
231231
}
@@ -366,7 +366,9 @@ CGDataProviderRef CGImageGetDataProvider(CGImageRef img) {
366366
RETURN_NULL_IF_FAILED(img->ImageSource()->CopyPixels(nullptr, stride, size, buffer.get()));
367367

368368
CGDataProviderRef dataProvider =
369-
CGDataProviderCreateWithData(nullptr, buffer.release(), size, [](void* info, const void* data, size_t size) { IwFree(const_cast<void*>(data)); });
369+
CGDataProviderCreateWithData(nullptr, buffer.release(), size, [](void* info, const void* data, size_t size) {
370+
IwFree(const_cast<void*>(data));
371+
});
370372
CFAutorelease(dataProvider);
371373
return dataProvider;
372374
}
@@ -650,6 +652,12 @@ CGImageRef _CGImageCreateCopyWithPixelFormat(CGImageRef image, WICPixelFormatGUI
650652
return imageRef;
651653
}
652654

655+
CGImageRef _CGImageCreateFromDataProvider(CGDataProviderRef provider) {
656+
RETURN_NULL_IF(!provider);
657+
unsigned char* dataBytes = static_cast<unsigned char*>(const_cast<void*>(_CGDataProviderGetData(provider)));
658+
return _CGImageGetImageFromData(dataBytes, _CGDataProviderGetSize(provider));
659+
}
660+
653661
CGImageRef _CGImageGetImageFromData(void* data, int length) {
654662
return _CGImageLoadImageWithWICDecoder(GUID_NULL, data, length);
655663
}
@@ -753,7 +761,6 @@ size_t _CGImageImputeBitsPerPixelFromFormat(CGColorSpaceRef colorSpace, size_t b
753761

754762
HRESULT _CGImageGetWICPixelFormatFromImageProperties(
755763
unsigned int bitsPerComponent, unsigned int bitsPerPixel, CGColorSpaceRef colorSpace, CGBitmapInfo bitmapInfo, GUID* pixelFormat) {
756-
757764
// clang-format off
758765
static std::map<uint32_t, WICPixelFormatGUID> s_CGWICFormatMap{
759766
{ CG_FORMAT_KEY(kCGColorSpaceModelRGB , 24, kCGBitmapByteOrderDefault, kCGImageAlphaNone), GUID_WICPixelFormat24bppRGB },

Frameworks/GLKit/GLKTexture.mm

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//******************************************************************************
22
//
3-
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
3+
// Copyright (c) Microsoft. All rights reserved.
44
//
55
// This code is licensed under the MIT License (MIT).
66
//
@@ -25,6 +25,7 @@
2525
#import <GLKit/GLKitExport.h>
2626
#import <GLKit/GLKTexture.h>
2727
#import "NSLogging.h"
28+
#import "CGImageInternal.h"
2829

2930
static const wchar_t* TAG = L"GLKTexture";
3031

@@ -277,7 +278,7 @@ @implementation GLKTextureLoader {
277278
*/
278279
+ (GLKTextureInfo*)textureWithContentsOfFile:(NSString*)fname options:(NSDictionary*)opts error:(NSError**)err {
279280
CGDataProviderRef provider = CGDataProviderCreateWithFilename([fname UTF8String]);
280-
CGImageRef img = CGImageCreateWithPNGDataProvider(provider, NULL, NO, kCGRenderingIntentDefault);
281+
CGImageRef img = _CGImageCreateFromDataProvider(provider);
281282

282283
GLKTextureInfo* res = [self textureWithCGImage:img options:opts error:err];
283284

@@ -395,7 +396,8 @@ + (GLKTextureInfo*)cubeMapWithContentsOfFile:(NSString*)fname options:(NSDiction
395396
if (!provider) {
396397
return nil;
397398
}
398-
CGImageRef img = CGImageCreateWithPNGDataProvider(provider, NULL, NO, kCGRenderingIntentDefault);
399+
400+
CGImageRef img = _CGImageCreateFromDataProvider(provider);
399401
if (!img) {
400402
CGDataProviderRelease(provider);
401403
return nil;
@@ -541,7 +543,8 @@ + (GLKTextureInfo*)cubeMapWithContentsOfFiles:(NSArray*)fnames options:(NSDictio
541543
curSide++;
542544
continue;
543545
}
544-
CGImageRef img = CGImageCreateWithPNGDataProvider(provider, NULL, NO, kCGRenderingIntentDefault);
546+
547+
CGImageRef img = _CGImageCreateFromDataProvider(provider);
545548
if (!img) {
546549
CGDataProviderRelease(provider);
547550
NSTraceWarning(TAG, @"Unable to create image from cube side texture %@", fn);

Frameworks/include/CGImageInternal.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ struct GuidPixelLess : public std::binary_function<GUID, GUID, bool> {
4949
}
5050
};
5151

52-
static const std::map<GUID, int, GuidPixelLess> s_ValidRenderTargetPixelFormat = { { GUID_WICPixelFormat8bppAlpha, 1 }, // A8 Straight
53-
{ GUID_WICPixelFormat32bppRGB, 1 }, // RGBX
54-
{ GUID_WICPixelFormat32bppPRGBA, 1 }, // RGBA Premultiplied
55-
{ GUID_WICPixelFormat32bppBGR, 1 }, // BGRX
56-
{ GUID_WICPixelFormat32bppPBGRA, 1 } }; // BGRX Premultiplied
52+
static const std::map<GUID, int, GuidPixelLess> s_ValidRenderTargetPixelFormat = { { GUID_WICPixelFormat8bppAlpha, 1 }, // A8 Straight
53+
{ GUID_WICPixelFormat32bppRGB, 1 }, // RGBX
54+
{ GUID_WICPixelFormat32bppPRGBA,
55+
1 }, // RGBA Premultiplied
56+
{ GUID_WICPixelFormat32bppBGR, 1 }, // BGRX
57+
{ GUID_WICPixelFormat32bppPBGRA,
58+
1 } }; // BGRX Premultiplied
5759

5860
static const std::map<GUID, __CGImagePixelProperties, GuidPixelLess> s_PixelFormats = {
5961
// RGB(A) formats
@@ -189,15 +191,14 @@ COREGRAPHICS_EXPORT HRESULT _CGImageGetWICImageSource(CGImageRef image, IWICBitm
189191
// Obtain a direct pointer to the data.
190192
COREGRAPHICS_EXPORT void* _CGImageGetRawBytes(CGImageRef image);
191193

194+
COREGRAPHICS_EXPORT CGImageRef _CGImageCreateFromDataProvider(CGDataProviderRef provider);
195+
192196
// Obtain the associated DisplayTexture
193197
__declspec(dllexport) std::shared_ptr<IDisplayTexture> _CGImageGetDisplayTexture(CGImageRef image);
194198

195199
size_t _CGImageImputeBitsPerPixelFromFormat(CGColorSpaceRef colorSpace, size_t bitsPerComponent, CGBitmapInfo bitmapInfo);
196-
HRESULT _CGImageGetWICPixelFormatFromImageProperties(unsigned int bitsPerComponent,
197-
unsigned int bitsPerPixel,
198-
CGColorSpaceRef colorSpace,
199-
CGBitmapInfo bitmapInfo,
200-
GUID* pixelFormat);
200+
HRESULT _CGImageGetWICPixelFormatFromImageProperties(
201+
unsigned int bitsPerComponent, unsigned int bitsPerPixel, CGColorSpaceRef colorSpace, CGBitmapInfo bitmapInfo, GUID* pixelFormat);
201202

202203
// If the image is of the same format, the image is retained and returned.
203204
COREGRAPHICS_EXPORT CGImageRef _CGImageCreateCopyWithPixelFormat(CGImageRef image, WICPixelFormatGUID pixelFormat);

build/CoreGraphics/dll/CoreGraphics.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ LIBRARY CoreGraphics
381381
; CGImage private exports
382382
_CGImageLoadImageWithWICDecoder
383383
_CGImageGetImageFromData
384+
_CGImageCreateFromDataProvider
384385
_CGImagePNGRepresentation
385386
_CGImageJPEGRepresentation
386387
_CGImageRepresentation

0 commit comments

Comments
 (0)