in plot-api/src/commonMain/kotlin/org/jetbrains/letsPlot/util/pngj/pixels/PixelsWriter.kt [121:194]
protected fun filterRowWithFilterType(
_filterType: FilterType, _rowb: ByteArray, _rowbprev: ByteArray,
_rowf: ByteArray
): ByteArray {
// warning: some filters rely on: "previous row" (rowbprev) it must be initialized to 0 the first time
@Suppress("NAME_SHADOWING")
var _rowf = _rowf
if (_filterType === FilterType.FILTER_NONE) _rowf = _rowb
_rowf[0] = _filterType.value.toByte()
var i: Int
var j: Int
when (_filterType) {
FilterType.FILTER_NONE -> {}
FilterType.FILTER_PAETH -> {
i = 1
while (i <= bytesPixel) {
_rowf[i] =
PngHelperInternal.filterRowPaeth(_rowb[i].toInt(), 0, _rowbprev[i].toInt() and 0xFF, 0).toByte()
i++
}
j = 1
i = bytesPixel + 1
while (i <= bytesRow) {
_rowf[i] = PngHelperInternal.filterRowPaeth(
_rowb[i].toInt(), _rowb[j].toInt() and 0xFF, _rowbprev[i].toInt() and 0xFF,
_rowbprev[j].toInt() and 0xFF
).toByte()
i++
j++
}
}
FilterType.FILTER_SUB -> {
i = 1
while (i <= bytesPixel) {
_rowf[i] = _rowb[i]
i++
}
j = 1
i = bytesPixel + 1
while (i <= bytesRow) {
_rowf[i] = (_rowb[i] - _rowb[j]).toByte()
i++
j++
}
}
FilterType.FILTER_AVERAGE -> {
i = 1
while (i <= bytesPixel) {
_rowf[i] = (_rowb[i] - (_rowbprev[i].toInt() and 0xFF) / 2).toByte()
i++
}
j = 1
i = bytesPixel + 1
while (i <= bytesRow) {
_rowf[i] = (_rowb[i] - ((_rowbprev[i].toInt() and 0xFF) + (_rowb[j].toInt() and 0xFF)) / 2).toByte()
i++
j++
}
}
FilterType.FILTER_UP -> {
i = 1
while (i <= bytesRow) {
_rowf[i] = (_rowb[i] - _rowbprev[i]).toByte()
i++
}
}
else -> throw PngjOutputException("Filter type not recognized: $_filterType")
}
return _rowf
}