Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bi level support #188

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions jipp-pdl/src/main/kotlin/com/hp/jipp/pdl/pwg/PackBits.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,43 @@

package com.hp.jipp.pdl.pwg

import com.hp.jipp.pdl.pwg.PwgSettings.Companion.BITS_PER_BYTE
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.util.* // ktlint-disable no-wildcard-imports
import kotlin.math.ceil

/**
* Encoder/Decoder for the PackBits algorithm described in the Wi-Fi Peer-to-Peer Services Print Technical
* Specification v1.1
*/
class PackBits(
/** Number of bytes per pixel (1 for grayscale, 3 for RGB) */
private val bytesPerPixel: Int,
/** Number of bits per pixel (1 for bi-level, 8 for grayscale, 24 for RGB) */
private val bitsPerPixel: Int,
/** Total number of pixels on each horizontal line */
private val pixelsPerLine: Int
) {

/** Reads [inputPixels] until there are no more, writing encoded bytes to [outputBytes] */
fun encode(inputPixels: InputStream, outputBytes: OutputStream) {
EncodeContext(inputPixels, outputBytes, bytesPerPixel, pixelsPerLine).encode()
EncodeContext(
inputPixels,
outputBytes,
bitsPerPixel / BITS_PER_BYTE,//bitsPerPixel,
pixelsPerLine
).encode()
}

/** Manage the mutable context during encoding */
private class EncodeContext(
private val pixelsIn: InputStream,
private val bytesOut: OutputStream,
private val bytesPerPixel: Int,
private val bytesPerPixel: Int,//bitsPerPixel: Int,
pixelsPerLine: Int
) {
private val bytesPerLine = bytesPerPixel * pixelsPerLine
private val bytesPerLine =
bytesPerPixel * pixelsPerLine//ceil(bitsPerPixel.toDouble() / BITS_PER_BYTE * pixelsPerLine).toInt()
private var lineArrayValid = false
private var lineArray = ByteArray(bytesPerLine)
private var nextLineArrayValid = false
Expand Down Expand Up @@ -189,8 +196,8 @@ class PackBits(

private fun decodeLine(bytes: InputStream, pixelsPerLine: Int): ByteArray {
val pixels = ByteArrayOutputStream()
val pixel = ByteArray(bytesPerPixel)
while (pixels.size() < pixelsPerLine * bytesPerPixel) {
val pixel = ByteArray(ceil(bitsPerPixel.toDouble() / BITS_PER_BYTE).toInt())
while (pixels.size() < pixelsPerLine * bitsPerPixel.toDouble() / BITS_PER_BYTE) {
val control = bytes.read()
if (control == -1) throw IOException("EOF before EOL")
if (control < MAX_GROUP) {
Expand All @@ -206,8 +213,8 @@ class PackBits(
}
}
}
if (pixels.size() > pixelsPerLine * bytesPerPixel) {
throw IOException("Line too long; ${pixels.size()} with max ${pixelsPerLine * bytesPerPixel}")
if (pixels.size() > ceil(pixelsPerLine * bitsPerPixel.toDouble() / BITS_PER_BYTE)) {
throw IOException("Line too long; ${pixels.size()} with max ${pixelsPerLine / bitsPerPixel * BITS_PER_BYTE}")
}
return pixels.toByteArray()
}
Expand Down
2 changes: 1 addition & 1 deletion jipp-pdl/src/main/kotlin/com/hp/jipp/pdl/pwg/PwgHeader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ data class PwgHeader(
val numColors: Int = bitsPerPixel / bitsPerColor

/** PackBits object used for encoding/decoding pixels of data. */
val packBits = PackBits(bytesPerPixel = bitsPerPixel / PwgSettings.BITS_PER_BYTE, pixelsPerLine = width)
val packBits = PackBits(bitsPerPixel = bitsPerPixel, pixelsPerLine = width)

init {
if (vendorData.size > MAX_VENDOR_DATA_SIZE) {
Expand Down
2 changes: 1 addition & 1 deletion jipp-pdl/src/test/kotlin/pwg/PwgValidator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ object PwgValidator {
println("renderingIntent=$renderingIntent, pageSizeName=$pageSizeName")

val imageBytes = ByteArrayOutputStream()
PackBits(bytesPerPixel = bitsPerPixel / 8, pixelsPerLine = width)
PackBits(bitsPerPixel = bitsPerPixel, pixelsPerLine = width)
.decode(this, imageBytes, lines = height)
println(ByteWindow(imageBytes.toByteArray()).toString(200))
assertEquals(width * height, imageBytes.size() / (bitsPerPixel / 8))
Expand Down