Skip to content

Commit

Permalink
Fixed colorspace bug
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Mar 9, 2024
1 parent a730669 commit f58ed6f
Show file tree
Hide file tree
Showing 8 changed files with 379 additions and 43 deletions.
80 changes: 45 additions & 35 deletions app/src/main/java/com/awxkee/jxlcoder/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.awxkee.jxlcoder

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.ImageDecoder
import android.graphics.Matrix
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
Expand All @@ -26,14 +23,15 @@ import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.lifecycleScope
import com.awxkee.jxlcoder.animation.AnimatedDrawable
import com.awxkee.jxlcoder.animation.JxlAnimatedStore
import com.awxkee.jxlcoder.ui.theme.JXLCoderTheme
import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi
import com.google.accompanist.drawablepainter.rememberDrawablePainter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okio.buffer
import okio.source
import java.io.FileNotFoundException
import java.util.UUID

class MainActivity : ComponentActivity() {
Expand Down Expand Up @@ -114,49 +112,61 @@ class MainActivity : ComponentActivity() {
}
LaunchedEffect(key1 = Unit, block = {
lifecycleScope.launch(Dispatchers.IO) {

// val bufferPng = assets.open("lin.png").source().buffer().readByteArray()
// val bitmap = BitmapFactory.decodeByteArray(bufferPng, 0, bufferPng.size)
// lifecycleScope.launch {
// drawables.add(BitmapDrawable(resources, bitmap))
// }
// val jxlBuffer = JxlCoder.encode(bitmap)
// val animated = JxlCoder.decode(jxlBuffer)
// lifecycleScope.launch {
// drawables.add(BitmapDrawable(resources, animated))
// }

// val buffer4 = assets.open("its_totally_safe.gif").source().buffer().readByteArray()
// val jxlBuffer = JxlCoder.Convenience.gif2JXL(buffer4, quality = 55)
// val animated = JxlAnimatedImage(jxlBuffer)
// val drawable = animated.animatedDrawable
// val drawable = AnimatedDrawable(JxlAnimatedStore(animated))
// lifecycleScope.launch {
// drawables.add(drawable)
// }
//
////
// val buffer5 = assets.open("elephant.png").source().buffer().readByteArray()
// val jxlBufferPNG = JxlCoder.Convenience.apng2JXL(buffer5, quality = 55)
// val animated1 = JxlAnimatedImage(jxlBufferPNG)
// val drawable1 = animated1.animatedDrawable
// val drawable1 = AnimatedDrawable(JxlAnimatedStore(animated1))
// lifecycleScope.launch {
// drawables.add(drawable1)
// }
var assets = (this@MainActivity.assets.list("") ?: return@launch).toList()
for (asset in assets) {
try {
val buffer4 =
this@MainActivity.assets.open(asset).source().buffer()
.readByteArray()

val largeImageSize = JxlCoder.getSize(buffer4)
if (largeImageSize != null) {
var srcImage = JxlCoder.decodeSampled(
buffer4,
largeImageSize.width / 3,
largeImageSize.height / 3,
preferredColorConfig = PreferredColorConfig.RGBA_8888,
com.awxkee.jxlcoder.ScaleMode.FIT,
JxlResizeFilter.BICUBIC,
toneMapper = JxlToneMapper.LOGARITHMIC,
)
lifecycleScope.launch {
imagesArray.add(srcImage)
}
}
} catch (e: Exception) {
if (e !is FileNotFoundException) {
throw e
}
}
}
// var assets = ([email protected]("") ?: return@launch).toList()
// for (asset in assets) {
// try {
// val buffer4 =
// [email protected](asset).source().buffer()
// .readByteArray()
//
// val largeImageSize = JxlCoder.getSize(buffer4)
// if (largeImageSize != null) {
// var srcImage = JxlCoder.decodeSampled(
// buffer4,
// largeImageSize.width / 3,
// largeImageSize.height / 3,
// preferredColorConfig = PreferredColorConfig.RGBA_8888,
// com.awxkee.jxlcoder.ScaleMode.FIT,
// JxlResizeFilter.BICUBIC,
// toneMapper = JxlToneMapper.LOGARITHMIC,
// )
// lifecycleScope.launch {
// imagesArray.add(srcImage)
// }
// }
// } catch (e: Exception) {
// if (e !is FileNotFoundException) {
// throw e
// }
// }
// }
}
})
// A surface container using the 'background' color from the theme
Expand Down
3 changes: 1 addition & 2 deletions jxlcoder/src/main/cpp/JniDecoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ jobject decodeSampledImageImpl(JNIEnv *env, std::vector<uint8_t> &imageData, jin
if (colorEncoding.primaries == JXL_PRIMARIES_2100) {
sourceProfile = GamutRgbToXYZ(getRec2020Primaries(), getIlluminantD65());
} else if (colorEncoding.primaries == JXL_PRIMARIES_P3) {
sourceProfile = GamutRgbToXYZ(getDisplayP3Primaries(), getIlluminantDCI());
useChromaticAdaptation = true;
sourceProfile = GamutRgbToXYZ(getDisplayP3Primaries(), getIlluminantD65());
} else if (colorEncoding.primaries == JXL_PRIMARIES_SRGB) {
sourceProfile = GamutRgbToXYZ(getSRGBPrimaries(), getIlluminantD65());
} else {
Expand Down
3 changes: 1 addition & 2 deletions jxlcoder/src/main/cpp/JxlAnimatedDecoderCoordinator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ Java_com_awxkee_jxlcoder_JxlAnimatedImage_getFrameImpl(JNIEnv *env, jobject thiz
if (colorEncoding.primaries == JXL_PRIMARIES_2100) {
sourceProfile = GamutRgbToXYZ(getRec2020Primaries(), getIlluminantD65());
} else if (colorEncoding.primaries == JXL_PRIMARIES_P3) {
sourceProfile = GamutRgbToXYZ(getDisplayP3Primaries(), getIlluminantDCI());
useChromaticAdaptation = true;
sourceProfile = GamutRgbToXYZ(getDisplayP3Primaries(), getIlluminantD65());
} else if (colorEncoding.primaries == JXL_PRIMARIES_SRGB) {
sourceProfile = GamutRgbToXYZ(getSRGBPrimaries(), getIlluminantD65());
} else {
Expand Down
2 changes: 1 addition & 1 deletion jxlcoder/src/main/cpp/JxlEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ Java_com_awxkee_jxlcoder_JxlCoder_encodeImpl(JNIEnv *env, jobject thiz, jobject
.primaries_green_xy = {matrix(1, 0), matrix(1, 1)},
.primaries_blue_xy = {matrix(2, 0), matrix(2, 1)},
.transfer_function = JXL_TRANSFER_FUNCTION_SRGB,
.gamma = 1 / 2.6
.gamma = 1 / 2.2
};
} else if (stdString == "sRGB IEC61966-2.1 (Linear)" ||
dataSpace == ADataSpace::ADATASPACE_SCRGB_LINEAR) {
Expand Down
6 changes: 3 additions & 3 deletions jxlcoder/src/main/cpp/colorspaces/ColorSpaceProfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ static Eigen::Matrix<float, 3, 2> getRec2020Primaries() {

static const Eigen::Matrix3f getBradfordAdaptation() {
Eigen::Matrix3f M;
M << 0.8951, 0.2664, -0.1614,
-0.7502, 1.7135, 0.0367,
0.0389, -0.0685, 1.0296;
M << 0.8446965f, -0.1179225f, 0.3948108f,
-0.1366303f, 1.1041226f, 0.1291718f,
0.0798489f, -0.1348999f, 3.1924009f;

return M;
}
Expand Down
Loading

0 comments on commit f58ed6f

Please sign in to comment.