Skip to content

Commit

Permalink
Fix tile size/count writing when calling saveBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
melissalinkert committed Jun 20, 2024
1 parent b94bed3 commit 23780de
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion components/formats-bsd/src/loci/formats/out/DicomWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ else if (x % thisTileWidth != 0 || y % thisTileHeight != 0 ||
boolean first = x == 0 && y == 0;
boolean last = x + w == getSizeX() && y + h == getSizeY();

int xTiles = (int) Math.ceil((double) getSizeX() / thisTileWidth);
int yTiles = (int) Math.ceil((double) getSizeY() / thisTileHeight);
int sizeZ = r.getPixelsSizeZ(series).getValue().intValue();

// the compression type isn't supplied to the writer until
// after setId is called, so metadata that indicates or
// depends on the compression type needs to be set in
Expand All @@ -436,6 +440,15 @@ else if (x % thisTileWidth != 0 || y % thisTileHeight != 0 ||
ifds[resolutionIndex][no].put(IFD.PHOTOMETRIC_INTERPRETATION, PhotoInterp.Y_CB_CR.getCode());
}
}

out.seek(tileWidthPointer[resolutionIndex]);
out.writeShort((short) getTileSizeX());
out.seek(tileHeightPointer[resolutionIndex]);
out.writeShort((short) getTileSizeY());
out.seek(tileCountPointer[resolutionIndex]);

out.writeBytes(padString(String.valueOf(
xTiles * yTiles * sizeZ * r.getChannelCount(series))));
}

// TILED_SPARSE, so the tile coordinates must be written
Expand Down Expand Up @@ -528,7 +541,6 @@ else if (x % thisTileWidth != 0 || y % thisTileHeight != 0 ||
// in the IFD
// this tries to calculate the index without assuming sequential tile
// writing, but maybe there is a better way to calculate this?
int xTiles = (int) Math.ceil((double) getSizeX() / tileWidth[resolutionIndex]);
int xTile = x / tileWidth[resolutionIndex];
int yTile = y / tileHeight[resolutionIndex];
int tileIndex = (yTile * xTiles) + xTile;
Expand All @@ -538,6 +550,17 @@ else if (x % thisTileWidth != 0 || y % thisTileHeight != 0 ||
if (ifds[resolutionIndex][no] != null) {
tileByteCounts = (long[]) ifds[resolutionIndex][no].getIFDValue(IFD.TILE_BYTE_COUNTS);
tileOffsets = (long[]) ifds[resolutionIndex][no].getIFDValue(IFD.TILE_OFFSETS);

if (tileByteCounts.length < xTiles * yTiles) {
long[] newTileByteCounts = new long[xTiles * yTiles];
long[] newTileOffsets = new long[xTiles * yTiles];
System.arraycopy(tileByteCounts, 0, newTileByteCounts, 0, tileByteCounts.length);
System.arraycopy(tileOffsets, 0, newTileOffsets, 0, tileOffsets.length);
tileByteCounts = newTileByteCounts;
tileOffsets = newTileOffsets;
ifds[resolutionIndex][no].put(IFD.TILE_BYTE_COUNTS, tileByteCounts);
ifds[resolutionIndex][no].put(IFD.TILE_OFFSETS, tileOffsets);
}
}

if (compression == null || compression.equals(CompressionType.UNCOMPRESSED.getCompression())) {
Expand Down

0 comments on commit 23780de

Please sign in to comment.