Skip to content

Commit 5aaa49f

Browse files
authored
Merge pull request #1066 from WeatherGod/palette_support
[5.x]: Adding the ability to save geotiffs with a palette
2 parents da644a8 + 6b091b7 commit 5aaa49f

6 files changed

Lines changed: 972 additions & 34 deletions

File tree

cdm/misc/src/main/java/ucar/nc2/geotiff/GeoTiff.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import java.nio.ByteBuffer;
1515
import java.nio.ByteOrder;
1616
import java.nio.FloatBuffer;
17+
import java.nio.IntBuffer;
18+
import java.nio.ShortBuffer;
1719
import java.nio.channels.FileChannel;
1820
import java.nio.charset.StandardCharsets;
1921
import java.util.ArrayList;
@@ -191,6 +193,54 @@ int writeData(byte[] data, int imageNumber) throws IOException {
191193
return nextOverflowData;
192194
}
193195

196+
int writeData(short[] data, int imageNumber) throws IOException {
197+
if (file == null)
198+
init();
199+
200+
if (imageNumber == 1)
201+
channel.position(headerSize);
202+
else
203+
channel.position(nextOverflowData);
204+
205+
// no way around making a copy
206+
ByteBuffer direct = ByteBuffer.allocateDirect(2 * data.length);
207+
ShortBuffer buffer = direct.asShortBuffer();
208+
buffer.put(data);
209+
// buffer.flip();
210+
channel.write(direct);
211+
212+
if (imageNumber == 1)
213+
firstIFD = headerSize + 2 * data.length;
214+
else
215+
firstIFD = 2 * data.length + nextOverflowData;
216+
217+
return nextOverflowData;
218+
}
219+
220+
int writeData(int[] data, int imageNumber) throws IOException {
221+
if (file == null)
222+
init();
223+
224+
if (imageNumber == 1)
225+
channel.position(headerSize);
226+
else
227+
channel.position(nextOverflowData);
228+
229+
// no way around making a copy
230+
ByteBuffer direct = ByteBuffer.allocateDirect(4 * data.length);
231+
IntBuffer buffer = direct.asIntBuffer();
232+
buffer.put(data);
233+
// buffer.flip();
234+
channel.write(direct);
235+
236+
if (imageNumber == 1)
237+
firstIFD = headerSize + 4 * data.length;
238+
else
239+
firstIFD = 4 * data.length + nextOverflowData;
240+
241+
return nextOverflowData;
242+
}
243+
194244
int writeData(float[] data, int imageNumber) throws IOException {
195245
if (file == null)
196246
init();

0 commit comments

Comments
 (0)