-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add methods to convert between images and bytes * Use new Images utility class where applicable * Make Image(...) MIME type support RenderedImage * Add an example of displaying a BufferedImage * Add a unit test for Images utility class * Display image outputs as images by default
- Loading branch information
1 parent
583a38f
commit 728571d
Showing
8 changed files
with
190 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
kernel/base/src/main/java/com/twosigma/beakerx/util/Images.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.twosigma.beakerx.util; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.awt.image.RenderedImage; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
|
||
import javax.imageio.ImageIO; | ||
|
||
/** | ||
* Utility methods for working with Java image objects. | ||
* | ||
* @see ImageIO | ||
* @see RenderedImage | ||
*/ | ||
public final class Images { | ||
|
||
private Images() { | ||
// Prevent instantiation of utility class. | ||
} | ||
|
||
/** | ||
* Converts the given {@link RenderedImage} into a stream of PNG bytes. | ||
* | ||
* @param image The image to convert to a byte stream. | ||
* @return A stream of bytes in PNG format. | ||
*/ | ||
public static byte[] encode(RenderedImage image) throws IOException { | ||
return encode(image, "png"); | ||
} | ||
|
||
/** | ||
* Converts the given {@link RenderedImage} into a stream of bytes. | ||
* | ||
* @param image The image to convert to a byte stream. | ||
* @param format The informal name of the format for the returned bytes; e.g. | ||
* "png" or "jpg". See {@link ImageIO#getImageWritersByFormatName(String)}. | ||
* @return A stream of bytes in the requested format, or null if the | ||
* image cannot be converted to the specified format. | ||
*/ | ||
public static byte[] encode(RenderedImage image, String format) | ||
throws IOException | ||
{ | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
boolean success = ImageIO.write(image, format, baos); | ||
return success ? baos.toByteArray() : null; | ||
} | ||
|
||
/** | ||
* Converts the given byte array into a {@link BufferedImage}. | ||
* | ||
* @param data The bytes to convert to an image. | ||
* @return A {@link BufferedImage} of the given bytes, or null if an | ||
* image cannot be decoded converted to the specified format. | ||
*/ | ||
public static BufferedImage decode(byte[] data) | ||
throws IOException | ||
{ | ||
return ImageIO.read(new ByteArrayInputStream(data)); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
kernel/base/src/test/java/com/twosigma/beakerx/util/ImagesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.twosigma.beakerx.util; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.io.IOException; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Tests {@link Images}. | ||
*/ | ||
public class ImagesTest { | ||
|
||
@Test | ||
public void testRoundTrip() throws IOException { | ||
byte[] happyFace = { | ||
-0x77, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, | ||
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x07, | ||
0x08, 0x02, 0x00, 0x00, 0x00, 0x4b, 0x30, -0x40, -0x7c, 0x00, 0x00, 0x00, | ||
0x3d, 0x49, 0x44, 0x41, 0x54, 0x78, -0x26, 0x63, 0x60, 0x60, 0x60, 0x50, | ||
0x51, 0x51, -0x0f, -0x0c, -0x0c, -0x34, -0x33, -0x33, -0x63, 0x34, 0x69, | ||
-0x2e, -0x4a, 0x6d, -0x25, 0x6e, -0x21, -0x42, -0x33, 0x00, 0x11, 0x65, | ||
-0x68, -0x50, 0x13, 0x28, 0x0a, 0x24, -0x77, 0x10, -0x3b, 0x62, 0x42, | ||
0x2c, 0x33, 0x03, 0x5c, 0x14, -0x38, -0x3a, 0x2b, 0x0a, 0x34, 0x01, -0x38, | ||
-0x7f, 0x20, -0x78, 0x09, 0x00, 0x46, 0x2a, 0x38, 0x70, -0x7d, 0x64, 0x48, | ||
0x7a, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, -0x52, 0x42, 0x60, | ||
-0x7e | ||
}; | ||
BufferedImage bi = Images.decode(happyFace); | ||
assertEquals(7, bi.getWidth()); | ||
assertEquals(7, bi.getHeight()); | ||
assertEquals(0xFF0090b9, bi.getRGB(2, 1)); | ||
assertEquals(0xFF5d0300, bi.getRGB(5, 5)); | ||
byte[] reencoded = Images.encode(bi); | ||
assertArrayEquals(happyFace, reencoded); | ||
} | ||
} |