Skip to content

Commit aca1ab6

Browse files
author
Evgeniy Sidenko
committed
+ added a new parameter int[] dstArray in the methods getByteArray, getInt32Array of classes HeicImage and HeicImageFrame. It allows allocating the destination buffer only once.
* updated API documentation
1 parent 3a20b37 commit aca1ab6

6 files changed

Lines changed: 226 additions & 92 deletions

File tree

README.md

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,21 @@ try (IOFileStream fs = new IOFileStream("filename.heic", IOMode.READ))
4444
{
4545
HeicImage image = HeicImage.load(fs);
4646

47-
int[] pixels = frames.get(key).getInt32Array(PixelFormat.Argb32);
4847
var width = (int)image.Width;
49-
var height = (int)image.Height;
50-
48+
var height = (int)image.Height;
5149
BufferedImage image2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
52-
image2.setRGB(0, 0, width, height, pixels, 0, width);
50+
51+
final int[] dstArray = ((DataBufferInt) image2.getRaster()
52+
.getDataBuffer()).getData();
53+
54+
int[] pixels = frames.get(key).getInt32Array(PixelFormat.Argb32, dstArray);
55+
56+
57+
if (pixels != dstArray)
58+
{
59+
image2.setRGB(0, 0, width, height, pixels, 0, width);
60+
}
61+
5362
ImageIO.write(image2, "JPEG", new File("output.jpg"));
5463
}
5564
```
@@ -60,12 +69,21 @@ try (IOFileStream fs = new IOFileStream("filename.heic", IOMode.READ))
6069
{
6170
HeicImage image = HeicImage.load(fs);
6271

63-
int[] pixels = frames.get(key).getInt32Array(PixelFormat.Argb32);
6472
var width = (int)image.Width;
6573
var height = (int)image.Height;
6674

67-
BufferedImage image2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
68-
image2.setRGB(0, 0, width, height, pixels, 0, width);
75+
BufferedImage image2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
76+
77+
final int[] dstArray = ((DataBufferInt) image2.getRaster()
78+
.getDataBuffer()).getData();
79+
80+
int[] pixels = frames.get(key).getInt32Array(PixelFormat.Argb32, dstArray);
81+
82+
if (pixels != dstArray)
83+
{
84+
image2.setRGB(0, 0, width, height, pixels, 0, width);
85+
}
86+
6987
ImageIO.write(image2, "PNG", new File("output.png"));
7088
}
7189
```
@@ -76,13 +94,18 @@ try (IOFileStream fs = new IOFileStream("filename.heic", IOMode.READ))
7694
{
7795
HeicImage image = HeicImage.load(fs);
7896

79-
int[] pixels = image.getInt32Array(PixelFormat.Argb32);
8097
int width = (int)image.getWidth();
8198
int height = (int)image.getHeight();
82-
int i = 0;
8399

84100
BufferedImage outImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
85-
outImage.setRGB(0, 0, width, height, pixels, 0, width);
101+
final int[] dstArray = ((DataBufferInt) outImage.getRaster()
102+
.getDataBuffer()).getData();
103+
int[] pixels = image.getInt32Array(PixelFormat.Argb32, dstArray);
104+
105+
if (pixels != dstArray)
106+
{
107+
outImage.setRGB(0, 0, width, height, pixels, 0, width);
108+
}
86109
ImageIO.write(outImage, "PNG", new File("output.png"));
87110
}
88111
```
@@ -98,10 +121,18 @@ try (IOFileStream fs = new IOFileStream("filename.heic", IOMode.READ))
98121
{
99122
int width = (int)frames.get(key).getWidth();
100123
int height = (int)frames.get(key).getHeight();
101-
int[] pixels = frames.get(key).getInt32Array(PixelFormat.Argb32);
102-
124+
103125
BufferedImage image2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
104-
image2.setRGB(0, 0, width, height, pixels, 0, width);
126+
final int[] dstArray = ((DataBufferInt) image2.getRaster()
127+
.getDataBuffer()).getData();
128+
129+
int[] pixels = frames.get(key).getInt32Array(PixelFormat.Argb32, dstArray);
130+
131+
if (pixels != dstArray)
132+
{
133+
image2.setRGB(0, 0, width, height, pixels, 0, width);
134+
}
135+
105136
ImageIO.write(image2, "PNG", new File("output"+key+".png"));
106137
}
107138
}
@@ -110,8 +141,8 @@ try (IOFileStream fs = new IOFileStream("filename.heic", IOMode.READ))
110141
## Documentation
111142

112143
All public classes, methods and properties are documented in corresponding API_README:
113-
* [/Openize.Heic.Decoder/docs/API_README.md](https://github.com/openize-com/openize-heic-java/blob/main/docs/Openize.Heic.Decoder/API_README.md) for Openize.Heic.Decoder;
114-
* [/Openize.IsoBmff/docs/API_README.md](https://github.com/openize-com/openize-heic-java/blob/main/docs/Openize.IsoBmff/API_README.md) for Openize.IsoBmff.
144+
* [/Openize.Heic.Decoder/docs/API_README.md](/docs/Openize.Heic.Decoder/API_README.md) for Openize.Heic.Decoder;
145+
* [/Openize.IsoBmff/docs/API_README.md](/docs/Openize.IsoBmff/API_README.md) for Openize.IsoBmff.
115146

116147
### HeicImage
117148

@@ -120,8 +151,8 @@ Name | Type | Description | Parameters | Notes
120151
------------ | ------------- | ------------- | ------------- | -------------
121152
**load** | **HeicImage** | Reads the file meta data and creates a class object for further decoding of the file contents. | `Stream stream` - File stream. | This operation does not decode pixels. Use the default frame methods GetByteArray or GetInt32Array afterwards in order to decode pixels.
122153
**canLoad** | **boolean** | Checks if the stream can be read as a heic image. Returns true if file header contains heic signarure, false otherwise | `Stream stream` - File stream. |
123-
**getByteArray** | **byte[]** | Get pixel data of the default image frame in the format of byte array.<br />Each three or four bytes (the count depends on the pixel format) refer to one pixel left to right top to bottom line by line.<br />Returns null if frame does not contain image data. | `PixelFormat pixelFormat` - Pixel format that defines the order of colors and the presence of alpha byte.<br />`Rectangle boundsRectangle` - Bounds of the requested area.
124-
**getInt32Array** | **int[]** | Get pixel data of the default image frame in the format of integer array.<br />Each int value refers to one pixel left to right top to bottom line by line.<br />Returns null if frame does not contain image data. | `PixelFormat pixelFormat` - Pixel format that defines the order of colors.<br />`Rectangle boundsRectangle` - Bounds of the requested area.
154+
**getByteArray** | **byte[]** | Get pixel data of the default image frame in the format of byte array.<br />Each three or four bytes (the count depends on the pixel format) refer to one pixel left to right top to bottom line by line.<br />Returns null if frame does not contain image data. In general, it equals to `dstArray`. | `PixelFormat pixelFormat` - Pixel format that defines the order of colors and the presence of alpha byte.<br />`Rectangle boundsRectangle` - Bounds of the requested area.<br/>`byte[] dstArray` - Byte array for storing the pixel values. If it is `null` or its length is less than necessary the new array will be allocated and returned.
155+
**getInt32Array** | **int[]** | Get pixel data of the default image frame in the format of integer array.<br />Each int value refers to one pixel left to right top to bottom line by line.<br />Returns null if frame does not contain image data. In general, it equals to `dstArray`. | `PixelFormat pixelFormat` - Pixel format that defines the order of colors.<br />`Rectangle boundsRectangle` - Bounds of the requested area.<br/>`int[] dstArray` - Integer array for storing the argb values. If it is `null` or its length is less than necessary the new array will be allocated and returned.
125156

126157

127158
#### Properties
@@ -136,8 +167,8 @@ Name | Type | Description
136167
#### Methods
137168
Name | Type | Description | Parameters
138169
------------ | ------------- | ------------- | -------------
139-
**getByteArray** | **byte[]** | Get pixel data in the format of byte array. Each three or four bytes (the count depends on the pixel format) refer to one pixel left to right top to bottom line by line. | `PixelFormat pixelFormat` - Pixel format that defines the order of colors and the presence of alpha byte. `Rectangle boundsRectangle` - Bounds of the requested area.
140-
**getInt32Array** | **int[]** | Get pixel data in the format of integer array. Each int value refers to one pixel left to right top to bottom line by line. | `PixelFormat pixelFormat` - Pixel format that defines the order of colors. `Rectangle boundsRectangle` - Bounds of the requested area.
170+
**getByteArray** | **byte[]** | Get pixel data in the format of byte array. Each three or four bytes (the count depends on the pixel format) refer to one pixel left to right top to bottom line by line. In general, it equals to `dstArray`. | `PixelFormat pixelFormat` - Pixel format that defines the order of colors and the presence of alpha byte. `Rectangle boundsRectangle` - Bounds of the requested area.<br/>`byte[] dstArray` - Byte array for storing the pixel values. If it is `null` or its length is less than necessary the new array will be allocated and returned.
171+
**getInt32Array** | **int[]** | Get pixel data in the format of integer array. Each int value refers to one pixel left to right top to bottom line by line. In general, it equals to `dstArray`. | `PixelFormat pixelFormat` - Pixel format that defines the order of colors. `Rectangle boundsRectangle` - Bounds of the requested area.<br/>`int[] dstArray` - Integer array for storing the argb values. If it is `null` or its length is less than necessary the new array will be allocated and returned.
141172
**getTextData** | **String** | Get frame text data. Exists only for mime frame types. |
142173

143174
### Properties

docs/Openize.Heic.Decoder/HeicImage.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ Name | Type | Description | Notes
1717

1818
Name | Type | Description | Parameters | Notes
1919
------------ | ------------- | ------------- | ------------- | -------------
20-
**load** | **HeicImage** | Reads the file meta data and creates a class object for further decoding of the file contents. | Stream **stream** - File stream. | This operation does not decode pixels.<br />Use the default frame methods GetByteArray or GetInt32Array afterwards in order to decode pixels.
21-
**canLoad** | **boolean** | Checks if the stream can be read as a heic image.<br />Returns true if file header contains heic signarure, false otherwise | Stream **stream** - File stream. |
22-
**getByteArray** | **byte[]** | Get pixel data of the default image frame in the format of byte array.<br />Each three or four bytes (the count depends on the pixel format) refer to one pixel left to right top to bottom line by line.<br />Returns null if frame does not contain image data. | PixelFormat <b>pixelFormat</b> - Pixel format that defines the order of colors and the presence of alpha byte.<br />Rectangle <b>boundsRectangle</b> - Bounds of the requested area.
23-
**getInt32Array** | **int[]** | Get pixel data of the default image frame in the format of integer array.<br />Each int value refers to one pixel left to right top to bottom line by line.<br />Returns null if frame does not contain image data. | PixelFormat <b>pixelFormat</b> - Pixel format that defines the order of colors.<br />Rectangle <b>boundsRectangle</b> - Bounds of the requested area.
20+
**load** | **HeicImage** | Reads the file meta data and creates a class object for further decoding of the file contents. | **IOStream** ***stream*** - File stream. | This operation does not decode pixels.<br />Use the default frame methods GetByteArray or GetInt32Array afterwards in order to decode pixels.
21+
**canLoad** | **boolean** | Checks if the stream can be read as a heic image.<br />Returns true if file header contains heic signarure, false otherwise | **IOStream** ***stream*** - File stream. |
22+
**getByteArray** | **byte[]** | Get pixel data of the default image frame in the format of byte array.<br />Each three or four bytes (the count depends on the pixel format) refer to one pixel left to right top to bottom line by line.<br/>Returns null if frame does not contain image data. In general, it equals to ***dstArray***. | **PixelFormat**<br/> ***pixelFormat*** - Pixel format that defines the order of colors and the presence of alpha byte.<br/>**Rectangle**<br/>***boundsRectangle*** - Bounds of the requested area.<br/>**byte[]**<br/>***dstArray*** - Byte array for storing the pixel values. If it is `null` or its length is less than necessary the new array will be allocated and returned.
23+
**getInt32Array** | **int[]** | Get pixel data of the default image frame in the format of integer array.<br />Each int value refers to one pixel left to right top to bottom line by line.<br />Returns **null** if frame does not contain image data. In general, it equals to ***dstArray***. | **PixelFormat**<br/>***pixelFormat*** - Pixel format that defines the order of colors.<br />**Rectangle**<br/>***boundsRectangle*** - Bounds of the requested area.<br/>**int[]**<br/>***dstArray*** - Integer array for storing the argb values. If it is `null` or its length is less than necessary the new array will be allocated and returned.
2424

2525
[[Back to API_README]](API_README.md)

docs/Openize.Heic.Decoder/HeicImageFrame.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ Name | Type | Description | Notes
2323

2424
Name | Type | Description | Parameters
2525
------------ | ------------- | ------------- | -------------
26-
**getByteArray** | **byte[]** | Get pixel data in the format of byte array.<br />Each three or four bytes (the count depends on the pixel format) refer to one pixel left to right top to bottom line by line.<br />Returns null if frame does not contain image data. | PixelFormat **pixelFormat** - Pixel format that defines the order of colors and the presence of alpha byte.<br />Rectangle **boundsRectangle** - Bounds of the requested area.
27-
**getInt32Array** | **int[]** | Get pixel data in the format of integer array.<br />Each int value refers to one pixel left to right top to bottom line by line.<br />Returns null if frame does not contain image data. | PixelFormat **pixelFormat** - Pixel format that defines the order of colors.<br />Rectangle **boundsRectangle** - Bounds of the requested area.
26+
**getByteArray** | **byte[]** | Get pixel data in the format of byte array.<br />Each three or four bytes (the count depends on the pixel format) refer to one pixel left to right top to bottom line by line.<br />Returns null if frame does not contain image data. In general, it equals to ***dstArray***. | **PixelFormat **<br/>***pixelFormat*** - Pixel format that defines the order of colors and the presence of alpha byte.<br />**Rectangle**<br/>***boundsRectangle*** - Bounds of the requested area.<br/>**byte[]**<br/>***dstArray*** - Byte array for storing the pixel values. If it is `null` or its length is less than necessary the new array will be allocated and returned.
27+
**getInt32Array** | **int[]** | Get pixel data in the format of integer array.<br />Each int value refers to one pixel left to right top to bottom line by line.<br />Returns null if frame does not contain image data. In general, it equals to ***dstArray***. | **PixelFormat**<br/>***pixelFormat*** - Pixel format that defines the order of colors.<br />**Rectangle**<br/>***boundsRectangle*** - Bounds of the requested area.<br/>**int[]**<br/>***dstArray*** - Integer array for storing the argb values. If it is `null` or its length is less than necessary the new array will be allocated and returned.
2828
**getTextData** | **String** | Get frame text data.<br />Exists only for mime frame types. |
2929

3030
## Fields

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>openize</groupId>
88
<artifactId>openize-heic</artifactId>
9-
<version>25.4</version>
9+
<version>25.6</version>
1010
<name>Openize.HEIC for Java</name>
1111
<description>Openize.Heic is an open source implementation of the ISO/IEC 23008-12:2017 HEIF file format decoder. It is written from scratch and has a plain Java API to enable a simple integration into other software.</description>
1212
<url>https://github.com/openize-heic/Openize.HEIC-for-Java</url>

src/main/java/openize/heic/decoder/HeicImage.java

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,23 @@ public static boolean canLoad(IOStream stream)
132132
*/
133133
public final /*Byte*/byte[] getByteArray(PixelFormat pixelFormat)
134134
{
135-
return getByteArray(pixelFormat, new Rectangle());
135+
return getByteArray(pixelFormat, new Rectangle(), null);
136+
}
137+
138+
/**
139+
* <p>
140+
* Get pixel data of the default image frame in the format of byte array.
141+
* </p>
142+
* <p>Each three or four bytes (the count depends on the pixel format) refer to one pixel left to right top to bottom line by line.</p>
143+
*
144+
* @param pixelFormat Pixel format that defines the order of colors and the presence of alpha byte.
145+
* @param dstArray Byte array for storing the pixel values. If {@code null} or its length is less than
146+
* necessary the new array will be allocated and returned.
147+
* @return Byte array, null if frame does not contain image data. In general, it equals to {@code dstArray}.
148+
*/
149+
public final /*Byte*/byte[] getByteArray(PixelFormat pixelFormat, byte[] dstArray)
150+
{
151+
return getDefaultFrame().getByteArray(pixelFormat, new Rectangle(), dstArray);
136152
}
137153

138154
/**
@@ -143,11 +159,13 @@ public static boolean canLoad(IOStream stream)
143159
*
144160
* @param pixelFormat Pixel format that defines the order of colors and the presence of alpha byte.
145161
* @param boundsRectangle Bounds of the requested area.
146-
* @return Byte array, null if frame does not contain image data.
162+
* @param dstArray Byte array for storing the pixel values. If {@code null} or its length is less than
163+
* necessary the new array will be allocated and returned.
164+
* @return Byte array, null if frame does not contain image data. In general, it equals to {@code dstArray}.
147165
*/
148-
public final /*Byte*/byte[] getByteArray(PixelFormat pixelFormat, Rectangle boundsRectangle)
166+
public final /*Byte*/byte[] getByteArray(PixelFormat pixelFormat, Rectangle boundsRectangle, byte[] dstArray)
149167
{
150-
return getDefaultFrame().getByteArray(pixelFormat, boundsRectangle);
168+
return getDefaultFrame().getByteArray(pixelFormat, boundsRectangle, dstArray);
151169
}
152170

153171
/**
@@ -161,7 +179,23 @@ public static boolean canLoad(IOStream stream)
161179
*/
162180
public final int[] getInt32Array(PixelFormat pixelFormat)
163181
{
164-
return getInt32Array(pixelFormat, new Rectangle());
182+
return getInt32Array(pixelFormat, new Rectangle(), null);
183+
}
184+
185+
/**
186+
* <p>
187+
* Get pixel data of the default image frame in the format of integer array.
188+
* </p>
189+
* <p>Each int value refers to one pixel left to right top to bottom line by line.</p>
190+
*
191+
* @param pixelFormat Pixel format that defines the order of colors.
192+
* @param dstArray Integer array for storing the argb values. If {@code null} or its length is less than
193+
* necessary the new array will be allocated and returned.
194+
* @return Integer array, null if frame does not contain image data. In general, it equals to {@code dstArray}.
195+
*/
196+
public final int[] getInt32Array(PixelFormat pixelFormat, int[] dstArray)
197+
{
198+
return getInt32Array(pixelFormat, new Rectangle(), dstArray);
165199
}
166200

167201
/**
@@ -172,11 +206,13 @@ public final int[] getInt32Array(PixelFormat pixelFormat)
172206
*
173207
* @param pixelFormat Pixel format that defines the order of colors.
174208
* @param boundsRectangle Bounds of the requested area.
175-
* @return Integer array, null if frame does not contain image data.
209+
* @param dstArray Integer array for storing the argb values. If {@code null} or its length is less than
210+
* necessary the new array will be allocated and returned.
211+
* @return Integer array, null if frame does not contain image data. In general, it equals to {@code dstArray}.
176212
*/
177-
public final int[] getInt32Array(PixelFormat pixelFormat, Rectangle boundsRectangle)
213+
public final int[] getInt32Array(PixelFormat pixelFormat, Rectangle boundsRectangle, int[] dstArray)
178214
{
179-
return getDefaultFrame().getInt32Array(pixelFormat, boundsRectangle);
215+
return getDefaultFrame().getInt32Array(pixelFormat, boundsRectangle, dstArray);
180216
}
181217

182218
/**

0 commit comments

Comments
 (0)