Skip to content

Commit a19f4cd

Browse files
Liel Cohenfacebook-github-bot
Liel Cohen
authored andcommitted
enable to transform avif -> on client
Reviewed By: defHLT Differential Revision: D70459157 fbshipit-source-id: 60df87b59e22fa80bdc91e32be73034b7fd31ac0
1 parent 97b3c6c commit a19f4cd

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

imagepipeline-base/src/main/java/com/facebook/imagepipeline/transcoder/ImageTranscodeResult.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77

88
package com.facebook.imagepipeline.transcoder
99

10+
import com.facebook.imageformat.ImageFormat
1011
import java.util.Locale
1112

1213
/** Result returned by an [ImageTranscoder] when transcoding an image. */
1314
class ImageTranscodeResult(
14-
@field:TranscodeStatus @get:TranscodeStatus @param:TranscodeStatus val transcodeStatus: Int
15+
@field:TranscodeStatus @get:TranscodeStatus @param:TranscodeStatus val transcodeStatus: Int,
16+
val outputFormat: ImageFormat
1517
) {
1618
override fun toString(): String = String.format(null as Locale?, "Status: %d", transcodeStatus)
1719
}

imagepipeline-base/src/main/java/com/facebook/imagepipeline/transcoder/ImageTranscoder.kt

+8
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ interface ImageTranscoder {
6666
*/
6767
fun canTranscode(imageFormat: ImageFormat): Boolean
6868

69+
/**
70+
* Determines whether the given image format should be transformed into a different format.
71+
*
72+
* @param imageFormat The [ImageFormat] of the input image.
73+
* @return true if the image format should be transformed to a different format, else false.
74+
*/
75+
fun canTransformAVIF(imageFormat: ImageFormat): Boolean
76+
6977
/**
7078
* Gets the identifier of the image transcoder. This is mostly used for logging purposes.
7179
*

imagepipeline/src/main/java/com/facebook/imagepipeline/producers/ResizeAndRotateProducer.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ private void doTransform(
250250
CloseableReference.of(outputStream.toByteBuffer());
251251
try {
252252
ret = new EncodedImage(ref);
253-
ret.setImageFormat(JPEG);
253+
ret.setImageFormat(result.getOutputFormat());
254254
try {
255255
ret.parseMetaData();
256256
mProducerContext
@@ -322,7 +322,8 @@ private static TriState shouldTransform(
322322
return TriState.valueOf(
323323
shouldRotate(request.getRotationOptions(), encodedImage)
324324
|| imageTranscoder.canResize(
325-
encodedImage, request.getRotationOptions(), request.getResizeOptions()));
325+
encodedImage, request.getRotationOptions(), request.getResizeOptions())
326+
|| imageTranscoder.canTransformAVIF(encodedImage.getImageFormat()));
326327
}
327328

328329
private static boolean shouldRotate(RotationOptions rotationOptions, EncodedImage encodedImage) {

imagepipeline/src/main/java/com/facebook/imagepipeline/transcoder/SimpleImageTranscoder.kt

+19-5
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ class SimpleImageTranscoder(private val resizingEnabled: Boolean, private val ma
5555
BitmapFactory.decodeStream(encodedImage.inputStream, null, options)
5656
} catch (oom: OutOfMemoryError) {
5757
FLog.e(TAG, "Out-Of-Memory during transcode", oom)
58-
return ImageTranscodeResult(TranscodeStatus.TRANSCODING_ERROR)
58+
return ImageTranscodeResult(TranscodeStatus.TRANSCODING_ERROR, encodedImage.imageFormat)
5959
}
6060
if (resizedBitmap == null) {
6161
FLog.e(TAG, "Couldn't decode the EncodedImage InputStream ! ")
62-
return ImageTranscodeResult(TranscodeStatus.TRANSCODING_ERROR)
62+
return ImageTranscodeResult(TranscodeStatus.TRANSCODING_ERROR, encodedImage.imageFormat)
6363
}
6464
val transformationMatrix =
6565
JpegTranscoderUtils.getTransformationMatrix(encodedImage, rotationOptions)
@@ -76,13 +76,15 @@ class SimpleImageTranscoder(private val resizingEnabled: Boolean, private val ma
7676
transformationMatrix,
7777
false)
7878
}
79-
srcBitmap.compress(getOutputFormat(outputFormat), quality, outputStream)
79+
val outputFormat = getOutputFormat(outputFormat)
80+
srcBitmap.compress(outputFormat, quality, outputStream)
8081
ImageTranscodeResult(
8182
if (sampleSize > DownsampleUtil.DEFAULT_SAMPLE_SIZE) TranscodeStatus.TRANSCODING_SUCCESS
82-
else TranscodeStatus.TRANSCODING_NO_RESIZING)
83+
else TranscodeStatus.TRANSCODING_NO_RESIZING,
84+
convertCompressFormatToImageFormat(outputFormat))
8385
} catch (oom: OutOfMemoryError) {
8486
FLog.e(TAG, "Out-Of-Memory during transcode", oom)
85-
ImageTranscodeResult(TranscodeStatus.TRANSCODING_ERROR)
87+
ImageTranscodeResult(TranscodeStatus.TRANSCODING_ERROR, encodedImage.imageFormat)
8688
} finally {
8789
srcBitmap.recycle()
8890
resizedBitmap.recycle()
@@ -107,6 +109,8 @@ class SimpleImageTranscoder(private val resizingEnabled: Boolean, private val ma
107109
override fun canTranscode(imageFormat: ImageFormat): Boolean =
108110
imageFormat === DefaultImageFormats.HEIF || imageFormat === DefaultImageFormats.JPEG
109111

112+
override fun canTransformAVIF(imageFormat: ImageFormat): Boolean = false
113+
110114
override val identifier: String = "SimpleImageTranscoder"
111115

112116
private fun getSampleSize(
@@ -152,4 +156,14 @@ class SimpleImageTranscoder(private val resizingEnabled: Boolean, private val ma
152156
}
153157
}
154158
}
159+
160+
/** Maps an Android [CompressFormat] back to the corresponding [ImageFormat]. */
161+
private fun convertCompressFormatToImageFormat(compressFormat: CompressFormat): ImageFormat {
162+
return when (compressFormat) {
163+
CompressFormat.JPEG -> DefaultImageFormats.JPEG
164+
CompressFormat.PNG -> DefaultImageFormats.PNG
165+
CompressFormat.WEBP -> DefaultImageFormats.WEBP_SIMPLE
166+
else -> DefaultImageFormats.JPEG
167+
}
168+
}
155169
}

native-imagetranscoder/src/main/java/com/facebook/imagepipeline/nativecode/NativeJpegTranscoder.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public boolean canTranscode(ImageFormat imageFormat) {
7878
return imageFormat == DefaultImageFormats.JPEG;
7979
}
8080

81+
@Override
82+
public boolean canTransformAVIF(ImageFormat imageFormat) {
83+
return false;
84+
}
85+
8186
@Override
8287
public String getIdentifier() {
8388
return TAG;
@@ -146,7 +151,8 @@ public ImageTranscodeResult transcode(
146151
return new ImageTranscodeResult(
147152
downsampleRatio == DownsampleUtil.DEFAULT_SAMPLE_SIZE
148153
? TranscodeStatus.TRANSCODING_NO_RESIZING
149-
: TranscodeStatus.TRANSCODING_SUCCESS);
154+
: TranscodeStatus.TRANSCODING_SUCCESS,
155+
DefaultImageFormats.JPEG);
150156
}
151157

152158
/**

0 commit comments

Comments
 (0)