Skip to content

Commit

Permalink
Merge pull request #173 from melissalinkert/remove-pixel-type-conversion
Browse files Browse the repository at this point in the history
Remove the "--pixel-type" option
  • Loading branch information
chris-allan authored Dec 5, 2022
2 parents 72ea0bf + afc1bbe commit 5a6015f
Showing 1 changed file with 2 additions and 104 deletions.
106 changes: 2 additions & 104 deletions src/main/java/com/glencoesoftware/bioformats2raw/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.stream.IntStream;

import loci.common.Constants;
import loci.common.DataTools;
import loci.common.image.IImageScaler;
import loci.common.image.SimpleImageScaler;
import loci.common.services.DependencyException;
Expand Down Expand Up @@ -71,7 +70,6 @@
import ome.xml.model.TransmittanceRange;
import ome.xml.model.enums.DimensionOrder;
import ome.xml.model.enums.EnumerationException;
import ome.xml.model.enums.PixelType;
import ome.xml.model.enums.UnitsLength;
import ome.xml.model.enums.UnitsTime;
import ome.xml.model.primitives.Color;
Expand Down Expand Up @@ -333,13 +331,6 @@ public class Converter implements Callable<Void> {
)
private volatile boolean keepMemoFiles = false;

@Option(
names = "--pixel-type",
description = "Pixel type to write if input data is " +
" float or double (${COMPLETION-CANDIDATES})"
)
private PixelType outputPixelType;

@Option(
names = "--downsample-type",
description = "Tile downsampling algorithm (${COMPLETION-CANDIDATES})"
Expand Down Expand Up @@ -645,16 +636,6 @@ public void convert()
meta.setPixelsDimensionOrder(dimensionOrder, s);
}

PixelType type = meta.getPixelsType(s);
int bfType =
getRealType(FormatTools.pixelTypeFromString(type.getValue()));
String realType = FormatTools.getPixelTypeString(bfType);
if (!type.getValue().equals(realType)) {
meta.setPixelsType(PixelType.fromString(realType), s);
meta.setPixelsSignificantBits(new PositiveInteger(
FormatTools.getBytesPerPixel(bfType) * 8), s);
}

if (!noHCS) {
HCSIndex index = new HCSIndex(meta, s);
hcsIndexes.add(index);
Expand Down Expand Up @@ -1074,9 +1055,7 @@ private byte[] getTile(
if (resolution == 0) {
reader = readers.take();
try {
return changePixelType(
reader.openBytes(plane, xx, yy, width, height),
reader.getPixelType(), reader.isLittleEndian());
return reader.openBytes(plane, xx, yy, width, height);
}
finally {
readers.put(reader);
Expand Down Expand Up @@ -1262,7 +1241,7 @@ public void saveResolutions(int series)
sizeC = workingReader.getSizeC();
readerDimensionOrder = workingReader.getDimensionOrder();
imageCount = workingReader.getImageCount();
pixelType = getRealType(workingReader.getPixelType());
pixelType = workingReader.getPixelType();
}
finally {
readers.put(workingReader);
Expand Down Expand Up @@ -1933,87 +1912,6 @@ private IMetadata createMetadata() throws FormatException {
}
}

/**
* Get the actual pixel type to write based upon the given input type
* and command line options. Input and output pixel types are Bio-Formats
* types as defined in FormatTools.
*
* Changing the pixel type during export is only supported when the input
* type is float or double.
*
* @param srcPixelType pixel type of the input data
* @return pixel type of the output data
*/
private int getRealType(int srcPixelType) {
if (outputPixelType == null) {
return srcPixelType;
}
int bfPixelType =
FormatTools.pixelTypeFromString(outputPixelType.getValue());
if (bfPixelType == srcPixelType ||
(srcPixelType != FormatTools.FLOAT && srcPixelType != FormatTools.DOUBLE))
{
return srcPixelType;
}
return bfPixelType;
}

/**
* Change the pixel type of the input tile as appropriate.
*
* @param tile input pixel data
* @param srcPixelType pixel type of the input data
* @param littleEndian true if tile bytes have little-endian ordering
* @return pixel data with the correct output pixel type
*/
private byte[] changePixelType(byte[] tile, int srcPixelType,
boolean littleEndian)
{
if (outputPixelType == null) {
return tile;
}
int bfPixelType =
FormatTools.pixelTypeFromString(outputPixelType.getValue());

if (bfPixelType == srcPixelType ||
(srcPixelType != FormatTools.FLOAT && srcPixelType != FormatTools.DOUBLE))
{
return tile;
}

int bpp = FormatTools.getBytesPerPixel(bfPixelType);
int srcBpp = FormatTools.getBytesPerPixel(srcPixelType);
byte[] output = new byte[bpp * (tile.length / srcBpp)];

double[] range = getRange(bfPixelType);
if (range == null) {
throw new IllegalArgumentException(
"Cannot convert to " + outputPixelType);
}

if (srcPixelType == FormatTools.FLOAT) {
float[] pixels = DataTools.normalizeFloats(
(float[]) DataTools.makeDataArray(tile, 4, true, littleEndian));

for (int pixel=0; pixel<pixels.length; pixel++) {
long v = (long) ((pixels[pixel] * (range[1] - range[0])) + range[0]);
DataTools.unpackBytes(v, output, pixel * bpp, bpp, littleEndian);
}

}
else if (srcPixelType == FormatTools.DOUBLE) {
double[] pixels = DataTools.normalizeDoubles(
(double[]) DataTools.makeDataArray(tile, 8, true, littleEndian));

for (int pixel=0; pixel<pixels.length; pixel++) {
long v = (long) ((pixels[pixel] * (range[1] - range[0])) + range[0]);
DataTools.unpackBytes(v, output, pixel * bpp, bpp, littleEndian);
}
}

return output;
}

/**
* Get the minimum and maximum pixel values for the given pixel type.
*
Expand Down

0 comments on commit 5a6015f

Please sign in to comment.