QrLuminance.kt (2309B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2026 Taler Systems S.A. 4 * 5 * GNU Taler is free software; you can redistribute it and/or modify it under the 6 * terms of the GNU General Public License as published by the Free Software 7 * Foundation; either version 3, or (at your option) any later version. 8 * 9 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 package net.taler.merchantpos.config 18 19 import com.google.zxing.PlanarYUVLuminanceSource 20 21 import java.nio.ByteBuffer 22 23 /** Camera planes may contain padding between rows and between individual pixels. */ 24 internal fun qrLuminance( 25 buffer: ByteBuffer, 26 width: Int, 27 height: Int, 28 rowStride: Int, 29 pixelStride: Int, 30 rotation: Int, 31 ): PlanarYUVLuminanceSource 32 { 33 require(width > 0 && height > 0 && pixelStride > 0) 34 require(rowStride.toLong() >= (width - 1L) * pixelStride + 1) 35 require(rotation in setOf(0, 90, 180, 270)) 36 require((height - 1L) * rowStride + (width - 1L) * pixelStride < buffer.remaining()) 37 val swapped = rotation == 90 || rotation == 270 38 val outputWidth = if (swapped) height else width 39 val outputHeight = if (swapped) width else height 40 val pixels = ByteArray(Math.multiplyExact(width, height)) 41 val offset = buffer.position() 42 for (y in 0 until height) 43 { 44 for (x in 0 until width) 45 { 46 val destination = 47 when (rotation) 48 { 49 90 -> x * outputWidth + height - 1 - y 50 180 -> (height - 1 - y) * outputWidth + width - 1 - x 51 270 -> (width - 1 - x) * outputWidth + y 52 else -> y * outputWidth + x 53 } 54 pixels[destination] = buffer.get(offset + y * rowStride + x * pixelStride) 55 } 56 } 57 return PlanarYUVLuminanceSource( 58 pixels, 59 outputWidth, 60 outputHeight, 61 0, 62 0, 63 outputWidth, 64 outputHeight, 65 false, 66 ) 67 }