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

fix #62 https://github.com/jai-imageio/jai-imageio-core/issues/62 #63

Open
wants to merge 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.github.jaiimageio.impl.plugins.tiff;

import java.awt.color.ColorSpace;
import java.awt.image.ComponentColorModel;
import java.awt.image.ComponentSampleModel;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;
import java.awt.image.DataBuffer;

public class TIFFExtraSamplesColorModel extends ComponentColorModel {

private final int numComponents;

private final int componentSize;

public TIFFExtraSamplesColorModel(ColorSpace colorSpace,
int[] bits,
boolean hasAlpha,
boolean isAlphaPremultiplied,
int transparency,
int transferType,
int extraComponents) {
super(colorSpace, bits, hasAlpha, isAlphaPremultiplied, transparency, transferType);
this.numComponents = colorSpace.getNumComponents() + (hasAlpha ? 1 : 0) + extraComponents;
this.componentSize = DataBuffer.getDataTypeSize(transferType);
}

@Override
public int getNumComponents() {
return numComponents;
}

@Override
public int getComponentSize(int componentIdx) {
return componentSize;
}

@Override
public boolean isCompatibleSampleModel(SampleModel sm) {
// Must have the same number of components
return (sm instanceof ComponentSampleModel) && numComponents == sm.getNumBands() && transferType == sm.getTransferType();
}

@Override
public WritableRaster getAlphaRaster(WritableRaster raster) {
if (!hasAlpha()) {
return null;
}

int x = raster.getMinX();
int y = raster.getMinY();
int[] band = new int[] {getAlphaComponent()};

return raster.createWritableChild(x, y, raster.getWidth(), raster.getHeight(), x, y, band);
}

private int getAlphaComponent() {
return super.getNumComponents() - 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -765,24 +765,41 @@ public Iterator getImageTypes(int imageIndex) throws IIOException {
int numBands = smRaw.getNumBands();
int numComponents = iccColorSpace.getNumComponents();

int extraSamplesLength = 0;

if (extraSamples != null) {
extraSamplesLength = extraSamples.length;
}

// Replace the ColorModel with the ICC ColorModel if the
// numbers of samples and color components are amenable.
if(numBands == numComponents ||
numBands == numComponents + 1) {
if(numBands == numComponents + extraSamplesLength ||
numBands == numComponents + 1 + extraSamplesLength) {
// Set alpha flags.
boolean hasAlpha = numComponents != numBands;
boolean hasAlpha = numComponents + extraSamplesLength != numBands;
boolean isAlphaPre =
hasAlpha && cmRaw.isAlphaPremultiplied();

// Create a ColorModel of the same class and with
// the same transfer type.
ColorModel iccColorModel =
new ComponentColorModel(iccColorSpace,
cmRaw.getComponentSize(),
hasAlpha,
isAlphaPre,
cmRaw.getTransparency(),
cmRaw.getTransferType());
ColorModel iccColorModel = null;
if (extraSamplesLength == 0) {
iccColorModel =
new ComponentColorModel(iccColorSpace,
cmRaw.getComponentSize(),
hasAlpha,
isAlphaPre,
cmRaw.getTransparency(),
cmRaw.getTransferType());
} else {
iccColorModel = new TIFFExtraSamplesColorModel(iccColorSpace,
cmRaw.getComponentSize(),
hasAlpha,
isAlphaPre,
cmRaw.getTransparency(),
cmRaw.getTransferType(),
extraSamplesLength);
}

// Prepend the ICC profile-based ITS to the List. The
// ColorModel and SampleModel are guaranteed to be
Expand Down