Skip to content

Commit 8f87788

Browse files
janherlingmetafacebook-github-bot
authored andcommitted
Added possibility to determine the image type in Media::Android::decodeImage()
Summary: The Android NDK does not provide this kind of information, so that we need to determine the type based on the buffer's magic number. Reviewed By: enpe Differential Revision: D79398813 Privacy Context Container: L1192943 fbshipit-source-id: 0e3afb75d063dac4a72255e68073866baa151a7c
1 parent 9ee8e25 commit 8f87788

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

impl/ocean/media/android/Image.cpp

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace Android
2828

2929
#if defined(__ANDROID_API__) && __ANDROID_API__ >= 30
3030

31-
Frame Image::decodeImage(const void* buffer, const size_t size, const std::string& imageBufferTypeIn, std::string* imageBufferTypeOut)
31+
Frame Image::decodeImage(const void* buffer, const size_t size, const std::string& /*imageBufferTypeIn*/, std::string* imageBufferTypeOut)
3232
{
3333
ocean_assert(buffer != nullptr && size != 0);
3434

@@ -40,7 +40,14 @@ Frame Image::decodeImage(const void* buffer, const size_t size, const std::strin
4040
ScopedAImageDecoder aImageDecoder;
4141
if (AImageDecoder_createFromBuffer(buffer, size, &aImageDecoder.resetObject()) == ANDROID_IMAGE_DECODER_SUCCESS)
4242
{
43-
return decodeImage(*aImageDecoder);
43+
Frame result = decodeImage(*aImageDecoder);
44+
45+
if (result.isValid() && imageBufferTypeOut != nullptr)
46+
{
47+
*imageBufferTypeOut = determineImageType(buffer, size);
48+
}
49+
50+
return result;
4451
}
4552

4653
return Frame();
@@ -86,6 +93,41 @@ Frame Image::readImage(const std::string& filename)
8693
return Frame();
8794
}
8895

96+
std::string Image::determineImageType(const void* buffer, const size_t size)
97+
{
98+
ocean_assert(buffer != nullptr && size != 0);
99+
100+
const uint8_t* data = (const uint8_t*)(buffer);
101+
102+
if (size >= 4)
103+
{
104+
if (data[0] == 0x89 && data[1] == 0x50 && data[2] == 0x4E && data[3] == 0x47)
105+
{
106+
return "png";
107+
}
108+
109+
if (data[0] == 0x47 && data[1] == 0x49 && data[2] == 0x46 && data[3] == 0x38)
110+
{
111+
return "gif";
112+
}
113+
}
114+
115+
if (size >= 2)
116+
{
117+
if (data[0] == 0xFF && data[1] == 0xD8)
118+
{
119+
return "jpg";
120+
}
121+
122+
if (data[0] == 0x42 && data[1] == 0x4D)
123+
{
124+
return "bmp";
125+
}
126+
}
127+
128+
return std::string();
129+
}
130+
89131
#if defined(__ANDROID_API__) && __ANDROID_API__ >= 30
90132

91133
Frame Image::decodeImage(AImageDecoder* aImageDecoder)

impl/ocean/media/android/Image.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ class Image
7373

7474
protected:
7575

76+
/**
77+
* Returns the image type of a given buffer.
78+
* @param buffer The buffer from which the image type will be determined, must be valid
79+
* @param size The size of the given buffer in bytes, with range [1, infinity)
80+
* @return The image type, an empty string if the type could not be determined, e.g., "jpg", "png", "bmp", "tiff", etc.
81+
*/
82+
static std::string determineImageType(const void* buffer, const size_t size);
83+
7684
#if defined(__ANDROID_API__) && __ANDROID_API__ >= 30
7785

7886
/**

0 commit comments

Comments
 (0)