Skip to content

Commit

Permalink
Export image uncompressed
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackYps committed Dec 5, 2023
1 parent e903f03 commit 7dda2ec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public static void exportMapwideTexture(Path folderPath, SCMap map) throws IOExc
Path writingPath = folderPath.resolve(filePath);
Files.createDirectories(writingPath.getParent());
try {
ImageUtil.writeCompressedDDS(image, writingPath);
ImageUtil.writeRawDDS(image, writingPath);
} catch (IOException e) {
System.out.print("Could not write the map-wide texture\n" + e);
}
Expand Down
28 changes: 28 additions & 0 deletions shared/src/main/java/com/faforever/neroxis/util/ImageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,34 @@ public static void writeCompressedDDS(BufferedImage image, Path path) throws IOE
Files.write(path, compressedData, StandardOpenOption.APPEND);
}

public static void writeRawDDS(BufferedImage image, Path path) throws IOException {
int size = image.getHeight();
int length = size * size * 4;
Raster imageRaster = image.getData();
ByteBuffer imageBytes = ByteBuffer.allocate(length).order(ByteOrder.LITTLE_ENDIAN);
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
int[] values = imageRaster.getPixel(x, y, new int[4]);
for (int val : values) {
imageBytes.put((byte) val);
}
}
}
DDSHeader ddsHeader = new DDSHeader();
ddsHeader.setWidth(size);
ddsHeader.setHeight(size);
ddsHeader.setRGBBitCount(32);
ddsHeader.setRBitMask(0x000000FF);
ddsHeader.setGBitMask(0x0000FF00);
ddsHeader.setBBitMask(0x00FF0000);
ddsHeader.setABitMask(0xFF000000);

// If we don't do this we get weird results when the file already exists
Files.deleteIfExists(path);
Files.write(path, ddsHeader.toBytes(), StandardOpenOption.CREATE);
Files.write(path, imageBytes.array(), StandardOpenOption.APPEND);
}

public static BufferedImage getMapwideTexture(NormalMask normalMask, FloatMask waterDepth, FloatMask shadowMask) {
if (shadowMask.getSize() != normalMask.getSize()) {
throw new IllegalArgumentException("Mask sizes do not match: shadow size %d, normal size %d"
Expand Down

0 comments on commit 7dda2ec

Please sign in to comment.