Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
coobird committed Feb 11, 2022
2 parents b97f8fb + 6939c4a commit 3a75461
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 41 deletions.
4 changes: 2 additions & 2 deletions README.md
@@ -1,4 +1,4 @@
_*January 2, 2022: Thumbnailator 0.4.16 has been released!
_*February 11, 2022: Thumbnailator 0.4.17 has been released!
See [Changes](https://github.com/coobird/thumbnailator/wiki/Changes) for details.*_

_*Thumbnailator is now available through
Expand Down Expand Up @@ -49,7 +49,7 @@ The following pages have more information on what _Thumbnailator_ can do:

* [Features](https://github.com/coobird/thumbnailator/wiki/Features)
* [Examples](https://github.com/coobird/thumbnailator/wiki/Examples)
* [Thumbnailator API Documentation](https://coobird.github.io/thumbnailator/javadoc/0.4.16/)
* [Thumbnailator API Documentation](https://coobird.github.io/thumbnailator/javadoc/0.4.17/)
* [Frequently Asked Questions](https://github.com/coobird/thumbnailator/wiki/FAQ)

# Disclaimer
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.16</version>
<version>0.4.17</version>
<packaging>jar</packaging>
<name>thumbnailator</name>
<description>Thumbnailator - a thumbnail generation library for Java</description>
Expand Down
75 changes: 41 additions & 34 deletions src/main/java/net/coobird/thumbnailator/Thumbnails.java
@@ -1,7 +1,7 @@
/*
* Thumbnailator - a thumbnail generation library
*
* Copyright (c) 2008-2020 Chris Kroells
* Copyright (c) 2008-2022 Chris Kroells
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -1051,8 +1051,8 @@ public Builder<T> scale(double scaleWidth, double scaleHeight) {
}

/**
* Specifies the source region from which the thumbnail is to be
* created from.
* Specifies the region of the source image where the thumbnail
* will be created from.
* <p>
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
Expand All @@ -1075,8 +1075,8 @@ public Builder<T> sourceRegion(Region sourceRegion) {
}

/**
* Specifies the source region from which the thumbnail is to be
* created from.
* Specifies the region of the source image where the thumbnail
* will be created from.
* <p>
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
Expand All @@ -1100,15 +1100,15 @@ public Builder<T> sourceRegion(Position position, Size size) {
}

/**
* Specifies the source region from which the thumbnail is to be
* created from.
* Specifies the region of the source image where the thumbnail
* will be created from.
* <p>
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param x The horizontal-compoennt of the top left-hand
* @param x The horizontal-component of the top left-hand
* corner of the source region.
* @param y The vertical-compoennt of the top left-hand
* @param y The vertical-component of the top left-hand
* corner of the source region.
* @param width Width of the source region.
* @param height Height of the source region.
Expand All @@ -1131,8 +1131,8 @@ public Builder<T> sourceRegion(int x, int y, int width, int height) {
}

/**
* Specifies the source region from which the thumbnail is to be
* created from.
* Specifies the region of the source image where the thumbnail
* will be created from.
* <p>
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
Expand Down Expand Up @@ -1164,8 +1164,8 @@ public Builder<T> sourceRegion(Position position, int width, int height) {
}

/**
* Specifies the source region from which the thumbnail is to be
* created from.
* Specifies the region of the source image where the thumbnail
* will be created from.
* <p>
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
Expand All @@ -1188,27 +1188,34 @@ public Builder<T> sourceRegion(Rectangle region) {
}

/**
* Crops the thumbnail to the size specified when calling the
* {@link #size(int, int)} method, positioned by the given
* {@link Position} object.
* <p>
* Calling this method will guarantee that the size of the thumbnail
* will be exactly the dimensions specified in the
* {@link #size(int, int)} method.
* <p>
* Internally, the resizing is performed in two steps.
* First, the thumbnail will be sized so that one of the dimensions will
* be sized exactly to the dimension specified in the {@code size}
* method, while allowing the other dimension to overhang the specified
* dimension. Then, the thumbnail will be cropped to the dimensions
* specified in the {@code size} method, positioned using the speficied
* {@link Position} object.
* <p>
* Once this method is called, calling the {@link #scale(double)} method
* will result in an {@link IllegalStateException}.
* <p>
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
* Crops the thumbnail at the position specified by {@link Position}.
* This method must be used along with the {@link #size(int, int)}
* method to specify the size of the thumbnail.
*
* <p>This method will guarantee that the thumbnail will be exactly
* the dimensions specified in the {@link #size(int, int)} method.
*
* <p>Internally, the resizing is performed in two steps:
*
* <p>First, the thumbnail will be sized so that one of the dimensions
* will be exactly the dimension specified in the {@code size} method.
* The other dimension may overhang the specified dimension.
* For example, if the {@code .size(200, 200)} is called and the
* source image is 400 x 600, the image will be resized to 200 x 300,
* internally.
*
* <p>Next, the resized image will be cropped to the dimensions
* specified in the {@code size} method, positioned using
* the specified {@link Position} object.
* Continuing the example from the previous paragraph, the 200 x 300
* image will be cropped to 200 x 200 using the specified positioning.
*
* <p>Once this method is called, calling the {@link #scale(double)}
* or {@link #scale(double, double)} method will throw an
* {@link IllegalStateException}.
*
* <p>Calling this method multiple times will throw an
* {@link IllegalStateException}.
*
* @param position The position to which the thumbnail should be
* cropped to. For example, if
Expand Down
@@ -1,7 +1,7 @@
/*
* Thumbnailator - a thumbnail generation library
*
* Copyright (c) 2008-2021 Chris Kroells
* Copyright (c) 2008-2022 Chris Kroells
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -210,6 +210,15 @@ private void debugln(String format, Object... args) {
}
}

/**
* Debug message, optimized to reduce calls on Arrays.toString.
*/
private void debugln(String format, byte[] array) {
if (isDebug) {
debugln(format, Arrays.toString(array));
}
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
int bytesRead = is.read(b, off, len);
Expand Down Expand Up @@ -243,8 +252,8 @@ public int read(byte[] b, int off, int len) throws IOException {
System.arraycopy(b, off, tmpBuffer, totalRead - bytesRead, bytesRead);
buffer = tmpBuffer;

debugln("Source: %s", Arrays.toString(b));
debugln("Buffer: %s", Arrays.toString(buffer));
debugln("Source: %s", b);
debugln("Buffer: %s", buffer);

while (position < totalRead && (totalRead - position) >= 2) {
debugln("Start loop, position: %s", position);
Expand Down Expand Up @@ -285,7 +294,7 @@ public int read(byte[] b, int off, int len) throws IOException {
if (position == 0 && totalRead >= 2) {
// Check the first two bytes of stream to see if SOI exists.
// If SOI is not found, this is not a JPEG.
debugln("Check if JPEG. buffer: %s", Arrays.toString(buffer));
debugln("Check if JPEG. buffer: %s", buffer);
if (!(buffer[position] == (byte) 0xFF && buffer[position + 1] == (byte) 0xD8)) {
// Not SOI, so it's not a JPEG.
// We no longer need to keep intercepting.
Expand Down Expand Up @@ -380,6 +389,14 @@ public int read(byte[] b, int off, int len) throws IOException {
}
}

if (totalRead <= 6) {
// SOI (2 bytes) + marker+length (4 bytes) == 6 bytes
// If we didn't find a 2-byte (standalone) marker, then
// we'll need to wait around to get enough one for 4-byte.
debugln("Not enough data read. Attempt one additional read.");
break;
}

terminateIntercept();
debugln("Shouldn't be here. Terminating intercept.");
break;
Expand Down

0 comments on commit 3a75461

Please sign in to comment.