diff --git a/app/src/main/cpp/shaders/align_lk.comp b/app/src/main/cpp/shaders/align_lk.comp index 25eb5b6c..0e40edf4 100644 --- a/app/src/main/cpp/shaders/align_lk.comp +++ b/app/src/main/cpp/shaders/align_lk.comp @@ -126,7 +126,7 @@ void main() { float sIxIt = 0.0; float sIyIt = 0.0; - const int windowRadius = 4; + const int windowRadius = 3; const float sigma2 = 10.0; vec2 dX = vec2(1.0, 0.0); vec2 dY = vec2(0.0, 1.0); @@ -147,30 +147,24 @@ void main() { vec2 currentValPoint = basePoint + currentFlow; float I = sampleGreenProxy(trgImg, currentValPoint); - float I_x = (sampleGreenProxy(trgImg, currentValPoint + dX) - - sampleGreenProxy(trgImg, currentValPoint - dX)) * 0.5; - float I_y = (sampleGreenProxy(trgImg, currentValPoint + dY) - - sampleGreenProxy(trgImg, currentValPoint - dY)) * 0.5; - float T_x = (sampleGreenProxy(refImg, basePoint + dX) - sampleGreenProxy(refImg, basePoint - dX)) * 0.5; float T_y = (sampleGreenProxy(refImg, basePoint + dY) - sampleGreenProxy(refImg, basePoint - dY)) * 0.5; float refDetail = max(abs(T_x), abs(T_y)); - float trgDetail = max(abs(I_x), abs(I_y)); - float detailGate = clamp(max(refDetail, trgDetail) * 18.0, 0.08, 1.0); + float detailGate = clamp(refDetail * 18.0, 0.08, 1.0); float spatialW = exp(-0.5 * float(ox * ox + oy * oy) / sigma2); float w = spatialW * detailGate; float inlierWeight = 0.25 + 0.75 * sampleInlier(basePoint); float It = I - T; float totalWeight = w * inlierWeight; - sIxIx += totalWeight * I_x * I_x; - sIyIy += totalWeight * I_y * I_y; - sIxIy += totalWeight * I_x * I_y; - sIxIt += totalWeight * I_x * It; - sIyIt += totalWeight * I_y * It; + sIxIx += totalWeight * T_x * T_x; + sIyIy += totalWeight * T_y * T_y; + sIxIy += totalWeight * T_x * T_y; + sIxIt += totalWeight * T_x * It; + sIyIt += totalWeight * T_y * It; } } diff --git a/app/src/main/cpp/shaders/color_gather_raw.comp b/app/src/main/cpp/shaders/color_gather_raw.comp index 6b48bfc1..16a44b0c 100644 --- a/app/src/main/cpp/shaders/color_gather_raw.comp +++ b/app/src/main/cpp/shaders/color_gather_raw.comp @@ -179,8 +179,22 @@ void processPlaneGather(ivec2 outPos, uint planeIndex) { // 4. Neighborhood search in the target plane: ivec2 targetPlaneCenter = ivec2(round(targetPlanePos)); - for (int oy = -2; oy <= 2; ++oy) { - for (int ox = -2; ox <= 2; ++ox) { + // Dynamic bounding box computation for non-zero cubic weight + vec2 refPos0 = vec2(targetPlaneCenter) - flow; + vec2 outCenter0 = sourceToOutputPlaneCenter(refPos0, sampleOffset); + + float minOx = (float(outPos.x) - outCenter0.x - 2.0) / pc.scale; + float maxOx = (float(outPos.x) - outCenter0.x + 2.0) / pc.scale; + float minOy = (float(outPos.y) - outCenter0.y - 2.0) / pc.scale; + float maxOy = (float(outPos.y) - outCenter0.y + 2.0) / pc.scale; + + int oxStart = max(-2, int(ceil(minOx))); + int oxEnd = min( 2, int(floor(maxOx))); + int oyStart = max(-2, int(ceil(minOy))); + int oyEnd = min( 2, int(floor(maxOy))); + + for (int oy = oyStart; oy <= oyEnd; ++oy) { + for (int ox = oxStart; ox <= oxEnd; ++ox) { ivec2 targetPlaneNeighbor = targetPlaneCenter + ivec2(ox, oy); // Boundary checks for the target plane coordinate diff --git a/app/src/main/cpp/shaders/green_gather_raw.comp b/app/src/main/cpp/shaders/green_gather_raw.comp index c9fe9bea..0a53dff5 100644 --- a/app/src/main/cpp/shaders/green_gather_raw.comp +++ b/app/src/main/cpp/shaders/green_gather_raw.comp @@ -191,8 +191,22 @@ void processGreenPlaneGather(ivec2 outPos, int planeIndex) { // 4. Neighborhood search in the target plane: ivec2 targetPlaneCenter = ivec2(round(targetPlanePos)); - for (int oy = -2; oy <= 2; ++oy) { - for (int ox = -2; ox <= 2; ++ox) { + // Dynamic bounding box computation for non-zero cubic weight + vec2 refPos0 = vec2(targetPlaneCenter) - flow; + vec2 outCenter0 = sourceToOutputPlaneCenter(refPos0, sampleOffset); + + float minOx = (float(outPos.x) - outCenter0.x - 2.0) / pc.scale; + float maxOx = (float(outPos.x) - outCenter0.x + 2.0) / pc.scale; + float minOy = (float(outPos.y) - outCenter0.y - 2.0) / pc.scale; + float maxOy = (float(outPos.y) - outCenter0.y + 2.0) / pc.scale; + + int oxStart = max(-2, int(ceil(minOx))); + int oxEnd = min( 2, int(floor(maxOx))); + int oyStart = max(-2, int(ceil(minOy))); + int oyEnd = min( 2, int(floor(maxOy))); + + for (int oy = oyStart; oy <= oyEnd; ++oy) { + for (int ox = oxStart; ox <= oxEnd; ++ox) { ivec2 targetPlaneNeighbor = targetPlaneCenter + ivec2(ox, oy); // Boundary checks for the target plane coordinate diff --git a/app/src/main/cpp/shaders/normalize_green_hr.comp b/app/src/main/cpp/shaders/normalize_green_hr.comp index a73fab47..32711421 100644 --- a/app/src/main/cpp/shaders/normalize_green_hr.comp +++ b/app/src/main/cpp/shaders/normalize_green_hr.comp @@ -96,32 +96,7 @@ float computeObservationNorm(float meanVal, int bayerIndex) { return clampRawNorm((meanVal * 65535.0) / whiteRange); } -void gatherGreenNeighborhood(int familyIndex, int centerX, int centerY, - out float meanVal, out float support) { - float sum = 0.0; - float weight = 0.0; - for (int oy = -2; oy <= 2; ++oy) { - for (int ox = -2; ox <= 2; ++ox) { - if (ox == 0 && oy == 0) continue; - uvec2 neighAccum = readGreenAccum(familyIndex, centerX + ox, centerY + oy); - if (neighAccum.y == 0u) continue; - float spatialWeight = 1.0 / (1.0 + float(ox * ox + oy * oy)); - float sampleSupport = clamp(float(neighAccum.y) / 256.0, 0.15, 1.0); - float w = spatialWeight * sampleSupport; - sum += accumMean(neighAccum) * w; - weight += w; - } - } - - if (weight > 0.0) { - meanVal = sum / weight; - support = clamp(weight / 3.0, 0.0, 1.0); - } else { - meanVal = 0.0; - support = 0.0; - } -} bool getGreenSite(uint cfaPattern, uvec2 pos, out int familyIndex, out int bayerIndex) { uint phase = ((pos.y & 1u) << 1u) | (pos.x & 1u); @@ -161,10 +136,7 @@ void main() { uint idx = ty * pc.bufferStride + tx; uvec2 primary = (familyIndex == 0) ? greenPhase0.data[idx] : greenPhase1.data[idx]; - float primaryNeighborVal = 0.0; - float primaryNeighborSupport = 0.0; - gatherGreenNeighborhood(familyIndex, int(tx), int(ty), - primaryNeighborVal, primaryNeighborSupport); + uint outIndex = globalY * pc.width + globalX; float priorNorm = float(priorBuffer.data[outIndex]) / 65535.0; diff --git a/app/src/main/java/com/hinnka/mycamera/gallery/GalleryManager.kt b/app/src/main/java/com/hinnka/mycamera/gallery/GalleryManager.kt index ee285cf6..1d3ad965 100644 --- a/app/src/main/java/com/hinnka/mycamera/gallery/GalleryManager.kt +++ b/app/src/main/java/com/hinnka/mycamera/gallery/GalleryManager.kt @@ -1870,7 +1870,8 @@ object GalleryManager { RawProcessor.RawBufferValueDomain.NORMALIZED_SENSOR_RANGE } else { RawProcessor.RawBufferValueDomain.SENSOR - } + }, + customWriter = true ) } } catch (e: Throwable) { diff --git a/app/src/main/java/com/hinnka/mycamera/utils/RawProcessor.kt b/app/src/main/java/com/hinnka/mycamera/utils/RawProcessor.kt index 95e54f4d..f270b71b 100644 --- a/app/src/main/java/com/hinnka/mycamera/utils/RawProcessor.kt +++ b/app/src/main/java/com/hinnka/mycamera/utils/RawProcessor.kt @@ -153,6 +153,7 @@ object RawProcessor { blackLevel: FloatArray = floatArrayOf(0f, 0f, 0f, 0f), whiteLevel: Int = 65535, valueDomain: RawBufferValueDomain = RawBufferValueDomain.SENSOR, + customWriter: Boolean, ): Boolean { val orientation = when (rotation) { 90 -> ExifInterface.ORIENTATION_ROTATE_90 @@ -160,7 +161,7 @@ object RawProcessor { 270 -> ExifInterface.ORIENTATION_ROTATE_270 else -> ExifInterface.ORIENTATION_NORMAL } - if (!canDngCreatorWriteBuffer(width, height, characteristics)) { + if (customWriter) { PLog.i(TAG, "Writing stacked RAW DNG with custom writer: ${width}x${height}") return SuperResolutionDngWriter.write( outputStream = outputStream,