From c0ab3a4d45d63f830abf89750a7edd3d10dd3e56 Mon Sep 17 00:00:00 2001 From: Emanuel Rabina Date: Sun, 5 May 2024 17:01:58 +1200 Subject: [PATCH] Test out CPS writer - still no good --- .gitignore | 24 +++++---- .../classic/filetypes/CpsFileWriter.groovy | 15 +++--- .../classic/filetypes/ShpFile.groovy | 3 +- .../cli/converter/Pcx2CpsConverterCli.groovy | 2 +- .../converter/Pcx2CpsConverterCliTests.groovy | 4 +- .../redhorizon/filetypes/PcxFile.groovy | 54 +++++++++---------- 6 files changed, 51 insertions(+), 51 deletions(-) diff --git a/.gitignore b/.gitignore index c5631a56..14512d32 100644 --- a/.gitignore +++ b/.gitignore @@ -6,14 +6,16 @@ /libraries /mix /mods -/*.aud -/*.cps -/*.ini -/*.log -/*.mix -/*.pcx -/*.shp -/*.v00 -/*.vqa -/*.wsa -build + +build/ + +*.aud +*.cps +*.ini +*.log +*.mix +*.pcx +*.shp +*.v00 +*.vqa +*.wsa diff --git a/redhorizon-classic/source/nz/net/ultraq/redhorizon/classic/filetypes/CpsFileWriter.groovy b/redhorizon-classic/source/nz/net/ultraq/redhorizon/classic/filetypes/CpsFileWriter.groovy index f1d1f96a..d312d5b0 100644 --- a/redhorizon-classic/source/nz/net/ultraq/redhorizon/classic/filetypes/CpsFileWriter.groovy +++ b/redhorizon-classic/source/nz/net/ultraq/redhorizon/classic/filetypes/CpsFileWriter.groovy @@ -37,26 +37,27 @@ class CpsFileWriter extends FileWriter { @Override void write(OutputStream outputStream, Void options) { - def output = new NativeDataOutputStream(outputStream) - def lcw = new LCW() - def palette = source instanceof InternalPalette ? source.palette : null + var output = new NativeDataOutputStream(outputStream) + var lcw = new LCW() + var palette = source instanceof InternalPalette ? source.palette : null // Encode image - def encodedImage = lcw.encode(source.imageData, ByteBuffer.allocateNative(source.imageData.capacity())) + var encodedImage = lcw.encode(source.imageData, ByteBuffer.allocateNative(source.imageData.capacity())) // Write header output.writeShort(8 + encodedImage.limit()) // (Header - this value) + image output.writeShort(COMPRESSION_LCW) - output.writeShort(IMAGE_SIZE) - output.writeShort(0) + output.writeInt(IMAGE_SIZE) output.writeShort(palette ? PALETTE_SIZE : 0) - // Write optional palette and image data + // Write optional palette if (palette) { palette.size.times { i -> output.write(palette[i]) } } + + // Write image data output.write(encodedImage.array(), 0, encodedImage.limit()) } } diff --git a/redhorizon-classic/source/nz/net/ultraq/redhorizon/classic/filetypes/ShpFile.groovy b/redhorizon-classic/source/nz/net/ultraq/redhorizon/classic/filetypes/ShpFile.groovy index bbb607d3..bb47f9d9 100644 --- a/redhorizon-classic/source/nz/net/ultraq/redhorizon/classic/filetypes/ShpFile.groovy +++ b/redhorizon-classic/source/nz/net/ultraq/redhorizon/classic/filetypes/ShpFile.groovy @@ -50,7 +50,6 @@ class ShpFile implements ImagesFile { static final byte FORMAT_LCW = (byte)0x80 static final byte FORMAT_XOR_BASE = (byte)0x40 static final byte FORMAT_XOR_CHAIN = (byte)0x20 - private static final byte[] FORMATS = [FORMAT_LCW, FORMAT_XOR_BASE, FORMAT_XOR_CHAIN] // @formatter:on private final NativeDataInputStream input @@ -98,7 +97,7 @@ class ShpFile implements ImagesFile { imageOffsets = new ShpImageInfo[numImages + 2] imageOffsets.length.times { i -> var imageInfo = new ShpImageInfo(input) - assert imageInfo.offsetFormat == 0 || imageInfo.offsetFormat in FORMATS + assert imageInfo.offsetFormat == 0 || imageInfo.offsetFormat in [FORMAT_LCW, FORMAT_XOR_BASE, FORMAT_XOR_CHAIN] imageOffsets[i] = imageInfo } } diff --git a/redhorizon-cli/source/nz/net/ultraq/redhorizon/cli/converter/Pcx2CpsConverterCli.groovy b/redhorizon-cli/source/nz/net/ultraq/redhorizon/cli/converter/Pcx2CpsConverterCli.groovy index 59c97dc8..efb6877e 100644 --- a/redhorizon-cli/source/nz/net/ultraq/redhorizon/cli/converter/Pcx2CpsConverterCli.groovy +++ b/redhorizon-cli/source/nz/net/ultraq/redhorizon/cli/converter/Pcx2CpsConverterCli.groovy @@ -63,7 +63,7 @@ class Pcx2CpsConverterCli implements Callable { if (!destFile.exists()) { sourceFile.withInputStream { inputStream -> destFile.withOutputStream { outputStream -> - def pcxFile = new PcxFile(inputStream) + var pcxFile = new PcxFile(inputStream) new CpsFileWriter(pcxFile).write(outputStream) } } diff --git a/redhorizon-cli/test/nz/net/ultraq/redhorizon/cli/converter/Pcx2CpsConverterCliTests.groovy b/redhorizon-cli/test/nz/net/ultraq/redhorizon/cli/converter/Pcx2CpsConverterCliTests.groovy index 7feb9039..2b7927e3 100644 --- a/redhorizon-cli/test/nz/net/ultraq/redhorizon/cli/converter/Pcx2CpsConverterCliTests.groovy +++ b/redhorizon-cli/test/nz/net/ultraq/redhorizon/cli/converter/Pcx2CpsConverterCliTests.groovy @@ -35,8 +35,8 @@ class Pcx2CpsConverterCliTests extends Specification { @Ignore("CPS conversion got busted somewhere along the way 😢") def "Converts a PCX file to a CPS file"() { given: - var pcxPath = 'nz/net/ultraq/redhorizon/cli/converter/alipaper.pcx' - var cpsPath = 'nz/net/ultraq/redhorizon/cli/converter/alipaper.cps' + var pcxPath = 'alipaper.pcx' + var cpsPath = 'alipaper.cps' var converter = new Pcx2CpsConverterCli( sourceFile: getResourceAsFile(pcxPath), destFile: new File("${System.getProperty('user.dir')}/build/classes/test/${cpsPath}"), diff --git a/redhorizon-filetypes/source/nz/net/ultraq/redhorizon/filetypes/PcxFile.groovy b/redhorizon-filetypes/source/nz/net/ultraq/redhorizon/filetypes/PcxFile.groovy index b13c9378..af36dbd0 100644 --- a/redhorizon-filetypes/source/nz/net/ultraq/redhorizon/filetypes/PcxFile.groovy +++ b/redhorizon-filetypes/source/nz/net/ultraq/redhorizon/filetypes/PcxFile.groovy @@ -1,12 +1,12 @@ -/* +/* * Copyright 2007 Emanuel Rabina (http://www.ultraq.net.nz/) - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -18,7 +18,7 @@ package nz.net.ultraq.redhorizon.filetypes import nz.net.ultraq.redhorizon.filetypes.codecs.RunLengthEncoding import nz.net.ultraq.redhorizon.filetypes.io.NativeDataInputStream -import static nz.net.ultraq.redhorizon.filetypes.ColourFormat.* +import static nz.net.ultraq.redhorizon.filetypes.ColourFormat.FORMAT_RGB import java.nio.ByteBuffer @@ -30,7 +30,7 @@ import java.nio.ByteBuffer * but for the purpose of Red Horizon, PCX files will be of the type used in the * Command & Conquer games: a 256-colour file with an internal palette located * at the tail of the file. - * + * * @author Emanuel Rabina. */ @FileExtensions('pcx') @@ -38,6 +38,7 @@ import java.nio.ByteBuffer class PcxFile implements ImageFile, InternalPalette { // Header constants + // @formatter:off static final int HEADER_PALETTE_SIZE = 48 static final byte MANUFACTURER_ZSOFT = 0x0a // 10 = ZSoft .pcx static final byte VERSION_PCP25 = 0 // PC Paintbrush 2.5 @@ -51,6 +52,7 @@ class PcxFile implements ImageFile, InternalPalette { static final int PALETTE_COLOURS = 256 static final int PALETTE_SIZE = PALETTE_COLOURS * FORMAT_RGB.value static final int PALETTE_PADDING_SIZE = 1 + // @formatter:on // Header data final byte manufacturer @@ -80,23 +82,19 @@ class PcxFile implements ImageFile, InternalPalette { /** * Constructor, creates a new PCX file from data in the given input stream. - * + * * @param inputStream Input stream of the PCX file data. */ PcxFile(InputStream inputStream) { - def input = new NativeDataInputStream(inputStream) + var input = new NativeDataInputStream(inputStream) // File header manufacturer = input.readByte() assert manufacturer == MANUFACTURER_ZSOFT version = input.readByte() - assert version == VERSION_PCP25 || - version == VERSION_PCP28_PAL || - version == VERSION_PCP28_NO_PAL || - version == VERSION_PCP4WIN || - version == VERSION_PCPPLUS + assert version in [VERSION_PCP25, VERSION_PCP28_PAL, VERSION_PCP28_NO_PAL, VERSION_PCP4WIN, VERSION_PCPPLUS] encoding = input.readByte() assert encoding == ENCODING_RLE @@ -111,41 +109,41 @@ class PcxFile implements ImageFile, InternalPalette { hdpi = input.readShort() vdpi = input.readShort() - egaPalette = input.readNBytes(HEADER_PALETTE_SIZE) - reserved = input.readByte() - planes = input.readByte() + egaPalette = input.readNBytes(HEADER_PALETTE_SIZE) + reserved = input.readByte() + planes = input.readByte() bytesPerLine = input.readShort() - paletteInfo = input.readShort() - hScreenSize = input.readShort() - vScreenSize = input.readShort() - filler = input.readNBytes(54) + paletteInfo = input.readShort() + hScreenSize = input.readShort() + vScreenSize = input.readShort() + filler = input.readNBytes(54) width = xMax - xMin + 1 height = yMax - yMin + 1 // Read the rest of the stream - def imageAndPalette = input.readAllBytes() - def encodedImage = ByteBuffer.wrapNative(imageAndPalette, 0, imageAndPalette.length - PALETTE_SIZE - PALETTE_PADDING_SIZE) + var imageAndPalette = input.readAllBytes() + var encodedImage = ByteBuffer.wrapNative(imageAndPalette, 0, imageAndPalette.length - PALETTE_SIZE - PALETTE_PADDING_SIZE) // Build up the raw image data for use with a palette later // NOTE: The below is for the case when the scanline data exceeds the // width/height data, but have I ever encountered that? Otherwise // this is double-handling the same data. - def scanLines = new ArrayList() - def runLengthEncoding = new RunLengthEncoding((byte)0xc0) + var scanLines = new ArrayList() + var runLengthEncoding = new RunLengthEncoding((byte)0xc0) while (encodedImage.hasRemaining()) { scanLines << runLengthEncoding.decode(encodedImage, ByteBuffer.allocateNative(planes * bytesPerLine)) } - def indexedData = ByteBuffer.allocateNative(width * height) + var indexedData = ByteBuffer.allocateNative(width * height) (yMin..yMax).each { y -> - def scanLine = scanLines[y] + var scanLine = scanLines[y] (xMin..xMax).each { x -> indexedData.put(scanLine.get(x)) } } indexedData.flip() - def paletteData = ByteBuffer.wrapNative(imageAndPalette, imageAndPalette.length - PALETTE_SIZE, PALETTE_SIZE) + var paletteData = ByteBuffer.wrapNative(imageAndPalette, imageAndPalette.length - PALETTE_SIZE, PALETTE_SIZE) palette = new Palette(PALETTE_COLOURS, FORMAT_RGB, paletteData) // Apply palette to raw image data to create the final image @@ -154,7 +152,7 @@ class PcxFile implements ImageFile, InternalPalette { /** * Returns some information on this PCX file. - * + * * @return PCX file info. */ @Override