mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-05 04:25:29 -04:00
- Fixed a bug and some clean-up of 1-bit subsampling
- Added comand line parameters to specify source region and subsampling
This commit is contained in:
parent
8be8f286f3
commit
a782bcfc3b
@ -536,32 +536,30 @@ public class PSDImageReader extends ImageReaderBase {
|
|||||||
|
|
||||||
// TODO: Destination offset...??
|
// TODO: Destination offset...??
|
||||||
int offset = (y - pSource.y) / pYSub * destWidth;
|
int offset = (y - pSource.y) / pYSub * destWidth;
|
||||||
if (pXSub == 1) {
|
if (pXSub == 1 && pSource.x % 8 == 0) {
|
||||||
// Fast normal case, no sub sampling
|
// Fast normal case, no sub sampling
|
||||||
for (int i = 0; i < destWidth; i++) {
|
for (int i = 0; i < destWidth; i++) {
|
||||||
byte value = row[pSource.x + i * pXSub];
|
byte value = row[pSource.x / 8 + i * pXSub];
|
||||||
// NOTE: Invert bits to match Java's default monochrome
|
// NOTE: Invert bits to match Java's default monochrome
|
||||||
data[offset + i] = (byte) (~value & 0xff);
|
data[offset + i] = (byte) (~value & 0xff);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Copy line sub sampled into real data
|
// Copy line sub sampled into real data
|
||||||
|
final int maxX = pSource.x + pSource.width;
|
||||||
int x = pSource.x;
|
int x = pSource.x;
|
||||||
for (int i = 0; i < destWidth; i++) {
|
for (int i = 0; i < destWidth; i++) {
|
||||||
byte result = 0;
|
byte result = 0;
|
||||||
|
|
||||||
for (int j = 0; j < 8; j++) {
|
for (int j = 0; j < 8 && x < maxX; j++) {
|
||||||
int pos = x / 8;
|
int bytePos = x / 8;
|
||||||
|
|
||||||
if (pos >= row.length) {
|
int sourceBitOff = 7 - (x % 8);
|
||||||
break; // Stay inside bounds...
|
int mask = 1 << sourceBitOff;
|
||||||
}
|
|
||||||
|
|
||||||
int sourceBitOff = x % 8;
|
|
||||||
int mask = 0x80 >> sourceBitOff;
|
|
||||||
int destBitOff = 7 - j;
|
int destBitOff = 7 - j;
|
||||||
|
|
||||||
result |= (((row[pos] & mask) != 0) ? 1 : 0) << destBitOff;
|
// Shift bit into place
|
||||||
|
result |= ((row[bytePos] & mask) >> sourceBitOff) << destBitOff;
|
||||||
|
|
||||||
x += pXSub;
|
x += pXSub;
|
||||||
}
|
}
|
||||||
@ -780,9 +778,45 @@ public class PSDImageReader extends ImageReaderBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(final String[] pArgs) throws IOException {
|
public static void main(final String[] pArgs) throws IOException {
|
||||||
|
int subsampleFactor = 1;
|
||||||
|
Rectangle sourceRegion = null;
|
||||||
|
|
||||||
|
int idx = 0;
|
||||||
|
while (pArgs[idx].charAt(0) == '-') {
|
||||||
|
if (pArgs[idx].equals("-s")) {
|
||||||
|
subsampleFactor = Integer.parseInt(pArgs[++idx]);
|
||||||
|
}
|
||||||
|
else if (pArgs[idx].equals("-r")) {
|
||||||
|
int xw = Integer.parseInt(pArgs[++idx]);
|
||||||
|
int yh = Integer.parseInt(pArgs[++idx]);
|
||||||
|
|
||||||
|
try {
|
||||||
|
int w = Integer.parseInt(pArgs[idx + 1]);
|
||||||
|
int h = Integer.parseInt(pArgs[idx + 2]);
|
||||||
|
|
||||||
|
idx += 2;
|
||||||
|
|
||||||
|
// x y w h
|
||||||
|
sourceRegion = new Rectangle(xw, yh, w, h);
|
||||||
|
}
|
||||||
|
catch (NumberFormatException e) {
|
||||||
|
// w h
|
||||||
|
sourceRegion = new Rectangle(xw, yh);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("sourceRegion: " + sourceRegion);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.err.println("Usage: java PSDImageReader [-s <subsample factor>] [-r [<x y>] <w h>] <image file>");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
idx++;
|
||||||
|
}
|
||||||
|
|
||||||
PSDImageReader imageReader = new PSDImageReader(null);
|
PSDImageReader imageReader = new PSDImageReader(null);
|
||||||
|
|
||||||
File file = new File(pArgs[0]);
|
File file = new File(pArgs[idx]);
|
||||||
ImageInputStream stream = ImageIO.createImageInputStream(file);
|
ImageInputStream stream = ImageIO.createImageInputStream(file);
|
||||||
imageReader.setInput(stream);
|
imageReader.setInput(stream);
|
||||||
imageReader.readHeader();
|
imageReader.readHeader();
|
||||||
@ -796,13 +830,17 @@ public class PSDImageReader extends ImageReaderBase {
|
|||||||
System.out.println("imageReader.mGlobalLayerMask: " + imageReader.mGlobalLayerMask);
|
System.out.println("imageReader.mGlobalLayerMask: " + imageReader.mGlobalLayerMask);
|
||||||
|
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
ImageReadParam param = new ImageReadParam();
|
|
||||||
if (pArgs.length > 1) {
|
ImageReadParam param = imageReader.getDefaultReadParam();
|
||||||
// param.setSourceRegion(new Rectangle(200, 200, 400, 400));
|
|
||||||
// param.setSourceRegion(new Rectangle(300, 200));
|
if (sourceRegion != null) {
|
||||||
param.setSourceSubsampling(3, 3, 0, 0);
|
param.setSourceRegion(sourceRegion);
|
||||||
// param.setSourceSubsampling(2, 2, 0, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (subsampleFactor > 1) {
|
||||||
|
param.setSourceSubsampling(subsampleFactor, subsampleFactor, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
BufferedImage image = imageReader.read(0, param);
|
BufferedImage image = imageReader.read(0, param);
|
||||||
System.out.println("time: " + (System.currentTimeMillis() - start));
|
System.out.println("time: " + (System.currentTimeMillis() - start));
|
||||||
System.out.println("image: " + image);
|
System.out.println("image: " + image);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user