Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webp format support (Android Only) for GLContext.takeSnapshot function #7490

Merged
merged 2 commits into from May 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/pages/versions/unversioned/sdk/gl-view.md
Expand Up @@ -80,9 +80,11 @@ Takes a snapshot of the framebuffer and saves it as a file to app's cache direct
- **framebuffer (_WebGLFramebuffer_)** -- Specify the framebuffer that we will be reading from. Defaults to underlying framebuffer that is presented in the view or the current framebuffer if context is headless.
- **rect (`{ x: number, y: number, width: number, height: number }`)** -- Rect to crop the snapshot. It's passed directly to `glReadPixels`.
- **flip (_boolean_)** -- Whether to flip the snapshot vertically. Defaults to `false`.
- **format (_string_)** -- Either `'jpeg'` or `'png'`. Specifies what type of compression should be used and what is the result file extension. PNG compression is lossless but slower, JPEG is faster but the image has visible artifacts. Defaults to `'jpeg'`.
- **format (_string_)** -- Either `'jpeg'`, `'png'` or `'webp'` (_Android only_ for the latter). Specifies what type of compression should be used and what is the result file extension. PNG compression is lossless but slower, JPEG is faster but the image has visible artifacts. Defaults to `'jpeg'`.
- **compress (_number_)** -- A value in range 0 - 1 specifying compression level of the result image. 1 means no compression and 0 the highest compression. Defaults to `1.0`.

> **Note:** When using WebP format, the iOS version will print a warning, and generate a `'png'` file instead. It is recommendable to use platform dependant code in this case. You can refer to the [documentation on platform specifi code](https://docs.expo.io/versions/latest/react-native/platform-specific-code/).

#### Returns

Returns `{ uri, localUri, width, height }` where `uri` is a URI to the snapshot. `localUri` is a synonym for `uri` that makes this object compatible with `texImage2D`. `width, height` specify the dimensions of the snapshot.
Expand Down
Expand Up @@ -278,9 +278,14 @@ protected Void doInBackground(Void... params) {
FileOutputStream output = null;
Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;

if (mFormat != null && mFormat.equals("png")) {
compressFormat = Bitmap.CompressFormat.PNG;
extension = ".png";
if (mFormat != null) {
if (mFormat.equals("png")) {
compressFormat = Bitmap.CompressFormat.PNG;
extension = ".png";
} else if (mFormat.equals("webp")) {
compressFormat = Bitmap.CompressFormat.WEBP;
extension = ".webp";
}
}

Context context = mContext.get();
Expand Down
7 changes: 6 additions & 1 deletion packages/expo-gl/ios/EXGL/EXGLContext.m
Expand Up @@ -230,7 +230,12 @@ - (void)takeSnapshotWithOptions:(nonnull NSDictionary *)options
NSData *imageData;
NSString *extension;

if ([format isEqualToString:@"png"]) {
if ([format isEqualToString:@"webp"]) {
UMLogWarn(@"iOS doesn't support 'webp' representation, so 'takeSnapshot' won't work with that format. The image is going to be exported as 'png', but consider using a different code for iOS. Check this docs to learn how to do platform specific code (https://reactnative.dev/docs/platform-specific-code)");
imageData = UIImagePNGRepresentation(image);
extension = @".png";
}
else if ([format isEqualToString:@"png"]) {
imageData = UIImagePNGRepresentation(image);
extension = @".png";
} else {
Expand Down