fix: photo edit crop

This commit is contained in:
Hinnka committed 2026-03-21 17:33:30 +08:00
1 parent 7c0c2088b9
commit 2f02f2ff1b
9 files changed
+8274 -50

No files matched your search

@@ -0,0 +1,294 @@
/*
* This file is part of RawTherapee.
*
* Copyright (c) 2011 Jan Rinze Peterzon (janrinze@gmail.com)
*
* RawTherapee is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RawTherapee is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RawTherapee. If not, see <https://www.gnu.org/licenses/>.
*/
/*
* Declaration of flexible 2D arrays
*
* Usage:
*
* array2D<type> name (X-size,Y-size);
* array2D<type> name (X-size,Y-size,type ** data);
*
* creates an array which is valid within the normal C/C++ scope "{ ... }"
*
* access to elements is as simple as:
*
* array2D<float> my_array (10,10); // creates 10x10 array of floats
* value = my_array[3][5];
* my_array[4][6]=value;
*
* or copy an existing 2D array
*
* float ** mydata;
* array2D<float> my_array (10,10,mydata);
*
*
* Useful extra pointers
*
* <type> ** my_array gives access to the pointer for access with [][]
* <type> * my_array gives access to the flat stored data.
*
* Advanced usage:
* array2D<float> my_array ; // empty container.
* my_array(10,10) ; // resize to 10x10 array
* my_array(10,10,ARRAY2D_CLEAR_DATA) ; // resize to 10x10 and clear data
*
*/
#pragma once
#include <cassert>
#include <cstring>
#include <sys/types.h>
#include <vector>
#include "noncopyable.h"
// flags for use
constexpr unsigned int ARRAY2D_CLEAR_DATA = 1;
constexpr unsigned int ARRAY2D_BYREFERENCE = 2;
template<typename T>
class array2D
{
private:
ssize_t width;
std::vector<T*> rows;
std::vector<T> buffer;
void initRows(ssize_t h, int offset = 0)
{
rows.resize(h);
T* start = buffer.data() + offset;
for (ssize_t i = 0; i < h; ++i) {
rows[i] = start + width * i;
}
}
void ar_realloc(ssize_t w, ssize_t h, int offset = 0)
{
width = w;
buffer.resize(h * width + offset);
initRows(h, offset);
}
public:
// use as empty declaration, resize before use!
// very useful as a member object
array2D() : width(0) {}
// creator type1
array2D(int w, int h, unsigned int flags = 0) : width(w)
{
if (flags & ARRAY2D_CLEAR_DATA) {
buffer.resize(h * width, 0);
} else {
buffer.resize(h * width);
}
initRows(h);
}
// creator type 2
array2D(int w, int h, T ** source, unsigned int flags = 0) : width(w)
{
rows.resize(h);
if (!(flags & ARRAY2D_BYREFERENCE)) {
buffer.resize(h * width);
T* start = buffer.data();
for (ssize_t i = 0; i < h; ++i) {
rows[i] = start + i * width;
for (ssize_t j = 0; j < width; ++j) {
rows[i][j] = source[i][j];
}
}
} else {
for (ssize_t i = 0; i < h; ++i) {
rows[i] = source[i];
}
}
}
// creator type 3
array2D(int w, int h, int startx, int starty, T ** source, unsigned int flags = 0) : width(w)
{
rows.resize(h);
if (!(flags & ARRAY2D_BYREFERENCE)) {
buffer.resize(h * width);
T* start = buffer.data();
for (ssize_t i = 0; i < h; ++i) {
rows[i] = start + i * width;
for (ssize_t j = 0; j < width; ++j) {
rows[i][j] = source[i + starty][j + startx];
}
}
} else {
for (ssize_t i = 0; i < h; ++i) {
rows[i] = source[i + starty] + startx;
}
}
}
array2D(const array2D& other) :
width(other.width),
buffer(other.buffer)
{
initRows(other.rows.size());
}
array2D& operator =(const array2D& other)
{
if (this != &other) {
free();
width = other.width;
buffer = other.buffer;
initRows(other.rows.size());
}
return *this;
}
void fill(const T val, bool multiThread = false)
{
const ssize_t height = rows.size();
#ifdef _OPENMP
#pragma omp parallel for if(multiThread)
#endif
for (ssize_t i = 0; i < width * height; ++i) {
buffer[i] = val;
}
}
void free()
{
buffer.clear();
rows.clear();
width = 0;
}
// use with indices
T * operator[](int index)
{
assert((index >= 0) && (std::size_t(index) < rows.size()));
return rows[index];
}
const T * operator[](int index) const
{
assert((index >= 0) && (std::size_t(index) < rows.size()));
return rows[index];
}
// use as pointer to T**
operator T**()
{
return rows.data();
}
// use as pointer to T**
operator const T* const *() const
{
return rows.data();
}
// use as pointer to buffer
operator T*()
{
// only if owner this will return a valid pointer
return buffer.data();
}
operator const T*() const
{
// only if owner this will return a valid pointer
return buffer.data();
}
// useful within init of parent object
// or use as resize of 2D array
void operator()(int w, int h, unsigned int flags = 0, int offset = 0)
{
ar_realloc(w, h, offset);
if (flags & ARRAY2D_CLEAR_DATA) {
fill(0);
}
}
array2D<T>& operator+=(const array2D<T>& rhs)
{
if (rhs.getWidth() == this->getWidth() && rhs.getHeight() == this->getHeight()) {
for (int i = 0; i < getHeight(); ++i) {
#ifdef _OPENMP
#pragma omp simd
#endif
for (int j = 0; j < getWidth(); ++j) {
rows[i][j] += rhs[i][j];
}
}
}
return *this;
}
// import from flat data
void operator()(std::size_t w, std::size_t h, const T* const copy)
{
ar_realloc(w, h);
for (std::size_t y = 0; y < h; ++y) {
std::copy(copy + y * w, copy + y * w + w, rows.data()[y]);
}
}
int getWidth() const
{
return width;
}
int getHeight() const
{
return rows.size();
}
operator bool()
{
return (width > 0 && !rows.empty());
}
};
template<typename T, const size_t num>
class multi_array2D : public rtengine::NonCopyable
{
private:
array2D<T> list[num];
public:
multi_array2D(int width, int height, int flags = 0, int offset = 0)
{
for (size_t i = 0; i < num; ++i) {
list[i](width, height, flags, (i + 1) * offset);
}
}
array2D<T> & operator[](int index)
{
assert(static_cast<size_t>(index) < num);
return list[index];
}
};
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,34 @@
/*
* This file is part of RawTherapee.
*
* Copyright (c) 2016 Flössie <floessie.mail@gmail.com>
*
* RawTherapee is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RawTherapee is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RawTherapee. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
namespace rtengine
{
class NonCopyable
{
public:
NonCopyable() = default;
explicit NonCopyable(const NonCopyable&) = delete;
NonCopyable& operator =(const NonCopyable&) = delete;
};
}
@@ -0,0 +1,41 @@
////////////////////////////////////////////////////////////////
//
// opthelper.h includes some #defines which help to make optimizations easier and better readable
//
// copyright (c) 2013 Ingo Weyrich
//
// this is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////
#pragma once
#define pow_F(a,b) (xexpf((b)*xlogf(a)))
#ifdef __SSE2__
#include "sleefsseavx.h"
#endif
#ifdef __GNUC__
#define RESTRICT __restrict__
#define LIKELY(x) __builtin_expect (!!(x), 1)
#define UNLIKELY(x) __builtin_expect (!!(x), 0)
#define ALIGNED64 __attribute__ ((aligned (64)))
#define ALIGNED16 __attribute__ ((aligned (16)))
#else
#define RESTRICT
#define LIKELY(x) (x)
#define UNLIKELY(x) (x)
#define ALIGNED64
#define ALIGNED16
#endif
@@ -0,0 +1,246 @@
#pragma once
#include <algorithm>
#include <limits>
#include <cmath>
#include <cstdint>
#include <array>
namespace rtengine
{
constexpr int MAXVAL = 0xffff;
constexpr float MAXVALF = static_cast<float>(MAXVAL); // float version of MAXVAL
constexpr double MAXVALD = static_cast<double>(MAXVAL); // double version of MAXVAL
constexpr double RT_PI = 3.14159265358979323846; // pi
constexpr double RT_PI_2 = 1.57079632679489661923; // pi/2
constexpr double RT_PI_180 = 0.017453292519943295769; // pi/180
constexpr double RT_1_PI = 0.31830988618379067154; // 1/pi
constexpr double RT_2_PI = 0.63661977236758134308; // 2/pi
constexpr double RT_SQRT1_2 = 0.70710678118654752440; // 1/sqrt(2)
constexpr double RT_INFINITY = std::numeric_limits<double>::infinity();
constexpr double RT_NAN = std::numeric_limits<double>::quiet_NaN();
constexpr float RT_PI_F = RT_PI;
constexpr float RT_PI_F_2 = RT_PI_2;
constexpr float RT_PI_F_180 = RT_PI_180;
constexpr float RT_1_PI_F = RT_1_PI;
constexpr float RT_2_PI_F = RT_2_PI;
constexpr float RT_INFINITY_F = std::numeric_limits<float>::infinity();
constexpr float RT_NAN_F = std::numeric_limits<float>::quiet_NaN();
template<typename T>
constexpr T SQR(T x)
{
return x * x;
}
template<typename T>
constexpr T pow4(T x)
{
return SQR(SQR(x));
}
template<typename T>
constexpr T pow5(T x)
{
return x * pow4(x);
}
template<typename T>
constexpr const T& min(const T& a)
{
return a;
}
template<typename T>
constexpr const T& min(const T& a, const T& b)
{
return b < a ? b : a;
}
template<typename T, typename... ARGS>
constexpr const T& min(const T& a, const T& b, const ARGS&... args)
{
return min(min(a, b), min(args...));
}
template<typename T>
constexpr const T& max(const T& a)
{
return a;
}
template<typename T>
constexpr const T& max(const T& a, const T& b)
{
return a < b ? b : a;
}
template<typename T, typename... ARGS>
constexpr const T& max(const T& a, const T& b, const ARGS&... args)
{
return max(max(a, b), max(args...));
}
template<typename T>
constexpr const T& LIM(const T& val, const T& low, const T& high)
{
return max(low, min(val, high));
}
template<typename T>
constexpr T LIM01(const T& a)
{
return max(T(0), min(a, T(1)));
}
template<typename T>
constexpr T CLIP(const T& a)
{
return LIM(a, static_cast<T>(0), static_cast<T>(MAXVAL));
}
template <typename T>
constexpr T SGN(const T& a)
{
// returns -1 for a < 0, 0 for a = 0 and +1 for a > 0
return (T(0) < a) - (a < T(0));
}
template<typename T>
constexpr T intp(T a, T b, T c)
{
// calculate a * b + (1 - a) * c
// following is valid:
// intp(a, b+x, c+x) = intp(a, b, c) + x
// intp(a, b*x, c*x) = intp(a, b, c) * x
return a * (b - c) + c;
}
template<typename T>
inline T norm1(const T& x, const T& y)
{
return std::abs(x) + std::abs(y);
}
template<typename T>
inline T norm2(const T& x, const T& y)
{
return std::sqrt(x * x + y * y);
}
template< typename T >
inline T norminf(const T& x, const T& y)
{
return max(std::abs(x), std::abs(y));
}
constexpr int float2uint16range(float d)
{
// clips input to [0;65535] and rounds
return CLIP(d) + 0.5f;
}
constexpr std::uint8_t uint16ToUint8Rounded(std::uint16_t i)
{
return ((i + 128) - ((i + 128) >> 8)) >> 8;
}
template <typename T>
constexpr bool OOG(const T &val, const T &high=T(MAXVAL))
{
return (val < T(0)) || (val > high);
}
template <typename T>
void setUnlessOOG(T &out, const T &val)
{
if (!OOG(out)) {
out = val;
}
}
template <typename T>
bool invertMatrix(const std::array<std::array<T, 3>, 3> &in, std::array<std::array<T, 3>, 3> &out)
{
const T res00 = in[1][1] * in[2][2] - in[2][1] * in[1][2];
const T res10 = in[2][0] * in[1][2] - in[1][0] * in[2][2];
const T res20 = in[1][0] * in[2][1] - in[2][0] * in[1][1];
const T det = in[0][0] * res00 + in[0][1] * res10 + in[0][2] * res20;
if (std::abs(det) < 1.0e-10) {
return false;
}
out[0][0] = res00 / det;
out[0][1] = (in[2][1] * in[0][2] - in[0][1] * in[2][2]) / det;
out[0][2] = (in[0][1] * in[1][2] - in[1][1] * in[0][2]) / det;
out[1][0] = res10 / det;
out[1][1] = (in[0][0] * in[2][2] - in[2][0] * in[0][2]) / det;
out[1][2] = (in[1][0] * in[0][2] - in[0][0] * in[1][2]) / det;
out[2][0] = res20 / det;
out[2][1] = (in[2][0] * in[0][1] - in[0][0] * in[2][1]) / det;
out[2][2] = (in[0][0] * in[1][1] - in[1][0] * in[0][1]) / det;
return true;
}
template <typename T>
std::array<std::array<T, 3>, 3> dotProduct(const std::array<std::array<T, 3>, 3> &a, const std::array<std::array<T, 3>, 3> &b)
{
std::array<std::array<T, 3>, 3> res;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
res[i][j] = 0;
for (int k = 0; k < 3; ++k) {
res[i][j] += a[i][k] * b[k][j];
}
}
}
return res;
}
template <typename T>
std::array<T, 3> dotProduct(const std::array<std::array<T, 3>, 3> &a, const std::array<T, 3> &b)
{
std::array<T, 3> res;
for (int i = 0; i < 3; ++i) {
res[i] = 0;
for (int k = 0; k < 3; ++k) {
res[i] += a[i][k] * b[k];
}
}
return res;
}
template <typename T>
T lin2log(T x, T base)
{
constexpr T one(1);
return std::log(x * (base - one) + one) / std::log(base);
}
template <typename T>
T log2lin(T x, T base)
{
constexpr T one(1);
return (std::pow(base, x) - one) / (base - one);
}
}
File diff suppressed because it is too large. Load diff
@@ -2355,6 +2355,7 @@ class Camera2Controller(private val context: Context) {
private fun shouldUseHlgCapture(): Boolean {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
_state.value.useP010 &&
!_state.value.useRaw &&
isP010Supported &&
isHlg10Supported
}
@@ -2,7 +2,6 @@ package com.hinnka.mycamera.ui.gallery
import android.graphics.Bitmap
import android.graphics.RectF
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.*
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.*
@@ -11,7 +10,6 @@ import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
@@ -23,11 +21,8 @@ import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
@@ -75,28 +70,23 @@ sealed class CropAspectOption(
if (isFree) return null
if (isOriginal) return imageW.toFloat() / imageH.toFloat()
if (heightRatio == 0f) return null
val isImagePortrait = imageW < imageH
val isRatioPortrait = widthRatio < heightRatio
return if (isImagePortrait != isRatioPortrait) {
heightRatio / widthRatio
} else {
widthRatio / heightRatio
}
return widthRatio / heightRatio
}
}
/**
* 获取所有可用的裁剪选项列表
*/
fun getCropAspectOptions(imageWidth: Int, imageHeight: Int): List<CropAspectOption> {
fun getCropAspectOptions(): List<CropAspectOption> {
val options = mutableListOf<CropAspectOption>()
options.add(CropAspectOption.Free)
options.add(CropAspectOption.Original)
// 添加 AspectRatio 中定义的预设比例 (排除 XPAN)
for (ratio in AspectRatio.entries) {
options.add(CropAspectOption.FromAspectRatio(ratio))
val option = CropAspectOption.FromAspectRatio(ratio)
options.add(option)
if (option.widthRatio / option.heightRatio != 1f) {
options.add(CropAspectOption.Custom(option.heightRatio, option.widthRatio))
}
}
return options
}
@@ -112,8 +102,8 @@ fun CropEditPanel(
imageHeight: Int,
modifier: Modifier = Modifier
) {
val options = remember(imageWidth, imageHeight) {
getCropAspectOptions(imageWidth, imageHeight)
val options = remember {
getCropAspectOptions()
}
Column(
@@ -147,8 +137,7 @@ fun CropEditPanel(
items(options) { option ->
CropAspectOptionItem(
option = option,
isSelected = option == selectedOption
|| (option is CropAspectOption.Original && selectedOption is CropAspectOption.Original),
isSelected = option == selectedOption,
onClick = { onOptionSelected(option) }
)
}
@@ -586,17 +575,17 @@ private fun DrawScope.drawCropOverlay(
// 竖线
for (i in 1..2) {
val x = left + thirdWidth * i
drawLine(gridColor, Offset(x, top), Offset(x, bottom), strokeWidth = 0.5f)
drawLine(gridColor, Offset(x, top), Offset(x, bottom), strokeWidth = 2f)
}
// 横线
for (i in 1..2) {
val y = top + thirdHeight * i
drawLine(gridColor, Offset(left, y), Offset(right, y), strokeWidth = 0.5f)
drawLine(gridColor, Offset(left, y), Offset(right, y), strokeWidth = 2f)
}
// 四角手柄
val handleLength = 32f
val handleWidth = 6f
val handleLength = 64f
val handleWidth = 10f
val handleColor = Color.White
// 左上
@@ -709,6 +709,34 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
}
}
private fun restoreCropEditState(photo: PhotoData?, metadata: PhotoMetadata?) {
if (metadata == null) {
editCropRect.value = null
editCropAspectOption.value = CropAspectOption.Free
return
}
val cw = photo?.width ?: metadata.width
val ch = photo?.height ?: metadata.height
if (metadata.postCropRegion != null && cw > 0 && ch > 0) {
editCropRect.value = RectF(
metadata.postCropRegion.left.toFloat() / cw,
metadata.postCropRegion.top.toFloat() / ch,
metadata.postCropRegion.right.toFloat() / cw,
metadata.postCropRegion.bottom.toFloat() / ch
)
editCropAspectOption.value = metadata.ratio?.let { CropAspectOption.FromAspectRatio(it) }
?: CropAspectOption.Custom(
metadata.postCropRegion.width().toFloat(),
metadata.postCropRegion.height().toFloat()
)
} else {
editCropRect.value = null
editCropAspectOption.value = CropAspectOption.Free
}
}
private fun applyMetadataToEditState(metadata: PhotoMetadata?) {
val photo = getCurrentPhoto()
currentPhotoMetadataId = photo?.id
@@ -719,30 +747,7 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
editSharpening.value = m.sharpening ?: 0f
editNoiseReduction.value = m.noiseReduction ?: 0f
editChromaNoiseReduction.value = m.chromaNoiseReduction ?: 0f
val cw = photo?.width ?: m.width
val ch = photo?.height ?: m.height
if (m.postCropRegion != null && cw > 0 && ch > 0) {
editCropRect.value = RectF(
m.postCropRegion.left.toFloat() / cw,
m.postCropRegion.top.toFloat() / ch,
m.postCropRegion.right.toFloat() / cw,
m.postCropRegion.bottom.toFloat() / ch
)
// Set the aspect option based on ratio if it exists
if (m.ratio != null) {
editCropAspectOption.value = CropAspectOption.FromAspectRatio(m.ratio)
} else {
editCropAspectOption.value = CropAspectOption.Custom(
m.postCropRegion.width().toFloat(),
m.postCropRegion.height().toFloat()
)
}
} else {
editCropRect.value = null
editCropAspectOption.value = CropAspectOption.Free
}
restoreCropEditState(photo, m)
// 加载 LUT 配置
m.lutId?.let { id ->
@@ -752,7 +757,7 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
}
}
}
}
} ?: restoreCropEditState(photo, null)
}
fun loadThumbnail(photo: PhotoData): Bitmap? {
@@ -1262,6 +1267,7 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
editComputationalAperture.value = metadata.computationalAperture
editFocusPointX.value = metadata.focusPointX
editFocusPointY.value = metadata.focusPointY
restoreCropEditState(targetPhoto, metadata)
} ?: run {
editLutId.value = null
editFrameId.value = null
@@ -1272,6 +1278,7 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
editComputationalAperture.value = null
editFocusPointX.value = null
editFocusPointY.value = null
restoreCropEditState(targetPhoto, null)
}
// 加载当前编辑的 LUT 配置