Why editing PNG file result in tEXt disappear?
up vote
0
down vote
favorite
Really need some suggestions here.
I am trying to manually add a tEXt chunk into an existing PNG file just for learning purposes.
While it appeared to be a success, after deliberating editing the image, for e.g. deleting a small portion off the image or rotate it and save, my tEXt chunk is gone. Maybe some part of my code went wrong, some reviews would be appreciated.
byte RAWFILEBYTES = new byte[(int) pngFile.length()];
FileInputStream ins = new FileInputStream(pngFile);
for (int i = 0; i < RAWFILEBYTES.length; i++) {
RAWFILEBYTES[i] = (byte) ins.read();
}
byte size = {(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0e};
byte crc32 = new byte[4];
String chunkNameAndKey = "tEXtGreeting";
String value = "hello";
//Declaring size equal to the length of chunk name + key value pair
byte tEXtAndValue = new byte[chunkNameAndKey.getBytes().length + 1 + value.getBytes().length];
//Forming byte array of chunk name and key value pair
System.arraycopy(chunkNameAndKey.getBytes(), 0, tEXtAndValue, 0, chunkNameAndKey.getBytes().length);
tEXtAndValue[chunkNameAndKey.getBytes().length] = (byte) 0x00; //Follow specification key pair value separate by null
System.arraycopy(value.getBytes(), 0, tEXtAndValue, chunkNameAndKey.getBytes().length + 1, value.getBytes().length);
//Calculating checksum comprise of chunk name and value
//E.g. tEXtGreeting[null]hello
Checksum checksum = new CRC32();
checksum.update(tEXtAndValue, 0, tEXtAndValue.length);
//Convert checksum result to 4 bytes according to PNG.
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(checksum.getValue());
System.arraycopy(buffer.array(), buffer.array().length - 4, crc32, 0, 4);
//Entire tEXt chunk comprise of size, chunk name, key-value pair and CRC in order.
byte textChunkArr = new byte[4 + tEXtAndValue.length + 4];
System.arraycopy(size, 0, textChunkArr, 0, size.length);
System.arraycopy(tEXtAndValue, 0, textChunkArr, size.length, tEXtAndValue.length);
System.arraycopy(crc32, 0, textChunkArr, size.length + tEXtAndValue.length, crc32.length);
byte byte_array = new byte[RAWFILEBYTES.length + textChunkArr.length];
//Place the text chunk directly before IEND chunk.
System.arraycopy(RAWFILEBYTES, 0, byte_array, 0, RAWFILEBYTES.length - 12);
System.arraycopy(textChunkArr, 0, byte_array, RAWFILEBYTES.length - 12, textChunkArr.length);
System.arraycopy(RAWFILEBYTES, RAWFILEBYTES.length - 12, byte_array, RAWFILEBYTES.length - 12 + textChunkArr.length, 12);
try (FileOutputStream fos = new FileOutputStream("final2_file.png")) {
fos.write(byte_array);
}
In summary, what I'm trying to do, I am creating and insert a tEXt chunk manually directly before IEND chunk.
png
add a comment |
up vote
0
down vote
favorite
Really need some suggestions here.
I am trying to manually add a tEXt chunk into an existing PNG file just for learning purposes.
While it appeared to be a success, after deliberating editing the image, for e.g. deleting a small portion off the image or rotate it and save, my tEXt chunk is gone. Maybe some part of my code went wrong, some reviews would be appreciated.
byte RAWFILEBYTES = new byte[(int) pngFile.length()];
FileInputStream ins = new FileInputStream(pngFile);
for (int i = 0; i < RAWFILEBYTES.length; i++) {
RAWFILEBYTES[i] = (byte) ins.read();
}
byte size = {(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0e};
byte crc32 = new byte[4];
String chunkNameAndKey = "tEXtGreeting";
String value = "hello";
//Declaring size equal to the length of chunk name + key value pair
byte tEXtAndValue = new byte[chunkNameAndKey.getBytes().length + 1 + value.getBytes().length];
//Forming byte array of chunk name and key value pair
System.arraycopy(chunkNameAndKey.getBytes(), 0, tEXtAndValue, 0, chunkNameAndKey.getBytes().length);
tEXtAndValue[chunkNameAndKey.getBytes().length] = (byte) 0x00; //Follow specification key pair value separate by null
System.arraycopy(value.getBytes(), 0, tEXtAndValue, chunkNameAndKey.getBytes().length + 1, value.getBytes().length);
//Calculating checksum comprise of chunk name and value
//E.g. tEXtGreeting[null]hello
Checksum checksum = new CRC32();
checksum.update(tEXtAndValue, 0, tEXtAndValue.length);
//Convert checksum result to 4 bytes according to PNG.
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(checksum.getValue());
System.arraycopy(buffer.array(), buffer.array().length - 4, crc32, 0, 4);
//Entire tEXt chunk comprise of size, chunk name, key-value pair and CRC in order.
byte textChunkArr = new byte[4 + tEXtAndValue.length + 4];
System.arraycopy(size, 0, textChunkArr, 0, size.length);
System.arraycopy(tEXtAndValue, 0, textChunkArr, size.length, tEXtAndValue.length);
System.arraycopy(crc32, 0, textChunkArr, size.length + tEXtAndValue.length, crc32.length);
byte byte_array = new byte[RAWFILEBYTES.length + textChunkArr.length];
//Place the text chunk directly before IEND chunk.
System.arraycopy(RAWFILEBYTES, 0, byte_array, 0, RAWFILEBYTES.length - 12);
System.arraycopy(textChunkArr, 0, byte_array, RAWFILEBYTES.length - 12, textChunkArr.length);
System.arraycopy(RAWFILEBYTES, RAWFILEBYTES.length - 12, byte_array, RAWFILEBYTES.length - 12 + textChunkArr.length, 12);
try (FileOutputStream fos = new FileOutputStream("final2_file.png")) {
fos.write(byte_array);
}
In summary, what I'm trying to do, I am creating and insert a tEXt chunk manually directly before IEND chunk.
png
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Really need some suggestions here.
I am trying to manually add a tEXt chunk into an existing PNG file just for learning purposes.
While it appeared to be a success, after deliberating editing the image, for e.g. deleting a small portion off the image or rotate it and save, my tEXt chunk is gone. Maybe some part of my code went wrong, some reviews would be appreciated.
byte RAWFILEBYTES = new byte[(int) pngFile.length()];
FileInputStream ins = new FileInputStream(pngFile);
for (int i = 0; i < RAWFILEBYTES.length; i++) {
RAWFILEBYTES[i] = (byte) ins.read();
}
byte size = {(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0e};
byte crc32 = new byte[4];
String chunkNameAndKey = "tEXtGreeting";
String value = "hello";
//Declaring size equal to the length of chunk name + key value pair
byte tEXtAndValue = new byte[chunkNameAndKey.getBytes().length + 1 + value.getBytes().length];
//Forming byte array of chunk name and key value pair
System.arraycopy(chunkNameAndKey.getBytes(), 0, tEXtAndValue, 0, chunkNameAndKey.getBytes().length);
tEXtAndValue[chunkNameAndKey.getBytes().length] = (byte) 0x00; //Follow specification key pair value separate by null
System.arraycopy(value.getBytes(), 0, tEXtAndValue, chunkNameAndKey.getBytes().length + 1, value.getBytes().length);
//Calculating checksum comprise of chunk name and value
//E.g. tEXtGreeting[null]hello
Checksum checksum = new CRC32();
checksum.update(tEXtAndValue, 0, tEXtAndValue.length);
//Convert checksum result to 4 bytes according to PNG.
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(checksum.getValue());
System.arraycopy(buffer.array(), buffer.array().length - 4, crc32, 0, 4);
//Entire tEXt chunk comprise of size, chunk name, key-value pair and CRC in order.
byte textChunkArr = new byte[4 + tEXtAndValue.length + 4];
System.arraycopy(size, 0, textChunkArr, 0, size.length);
System.arraycopy(tEXtAndValue, 0, textChunkArr, size.length, tEXtAndValue.length);
System.arraycopy(crc32, 0, textChunkArr, size.length + tEXtAndValue.length, crc32.length);
byte byte_array = new byte[RAWFILEBYTES.length + textChunkArr.length];
//Place the text chunk directly before IEND chunk.
System.arraycopy(RAWFILEBYTES, 0, byte_array, 0, RAWFILEBYTES.length - 12);
System.arraycopy(textChunkArr, 0, byte_array, RAWFILEBYTES.length - 12, textChunkArr.length);
System.arraycopy(RAWFILEBYTES, RAWFILEBYTES.length - 12, byte_array, RAWFILEBYTES.length - 12 + textChunkArr.length, 12);
try (FileOutputStream fos = new FileOutputStream("final2_file.png")) {
fos.write(byte_array);
}
In summary, what I'm trying to do, I am creating and insert a tEXt chunk manually directly before IEND chunk.
png
Really need some suggestions here.
I am trying to manually add a tEXt chunk into an existing PNG file just for learning purposes.
While it appeared to be a success, after deliberating editing the image, for e.g. deleting a small portion off the image or rotate it and save, my tEXt chunk is gone. Maybe some part of my code went wrong, some reviews would be appreciated.
byte RAWFILEBYTES = new byte[(int) pngFile.length()];
FileInputStream ins = new FileInputStream(pngFile);
for (int i = 0; i < RAWFILEBYTES.length; i++) {
RAWFILEBYTES[i] = (byte) ins.read();
}
byte size = {(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0e};
byte crc32 = new byte[4];
String chunkNameAndKey = "tEXtGreeting";
String value = "hello";
//Declaring size equal to the length of chunk name + key value pair
byte tEXtAndValue = new byte[chunkNameAndKey.getBytes().length + 1 + value.getBytes().length];
//Forming byte array of chunk name and key value pair
System.arraycopy(chunkNameAndKey.getBytes(), 0, tEXtAndValue, 0, chunkNameAndKey.getBytes().length);
tEXtAndValue[chunkNameAndKey.getBytes().length] = (byte) 0x00; //Follow specification key pair value separate by null
System.arraycopy(value.getBytes(), 0, tEXtAndValue, chunkNameAndKey.getBytes().length + 1, value.getBytes().length);
//Calculating checksum comprise of chunk name and value
//E.g. tEXtGreeting[null]hello
Checksum checksum = new CRC32();
checksum.update(tEXtAndValue, 0, tEXtAndValue.length);
//Convert checksum result to 4 bytes according to PNG.
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(checksum.getValue());
System.arraycopy(buffer.array(), buffer.array().length - 4, crc32, 0, 4);
//Entire tEXt chunk comprise of size, chunk name, key-value pair and CRC in order.
byte textChunkArr = new byte[4 + tEXtAndValue.length + 4];
System.arraycopy(size, 0, textChunkArr, 0, size.length);
System.arraycopy(tEXtAndValue, 0, textChunkArr, size.length, tEXtAndValue.length);
System.arraycopy(crc32, 0, textChunkArr, size.length + tEXtAndValue.length, crc32.length);
byte byte_array = new byte[RAWFILEBYTES.length + textChunkArr.length];
//Place the text chunk directly before IEND chunk.
System.arraycopy(RAWFILEBYTES, 0, byte_array, 0, RAWFILEBYTES.length - 12);
System.arraycopy(textChunkArr, 0, byte_array, RAWFILEBYTES.length - 12, textChunkArr.length);
System.arraycopy(RAWFILEBYTES, RAWFILEBYTES.length - 12, byte_array, RAWFILEBYTES.length - 12 + textChunkArr.length, 12);
try (FileOutputStream fos = new FileOutputStream("final2_file.png")) {
fos.write(byte_array);
}
In summary, what I'm trying to do, I am creating and insert a tEXt chunk manually directly before IEND chunk.
png
png
edited Nov 20 at 6:05
Owain Esau
859517
859517
asked Nov 20 at 5:08
Joseph
1124
1124
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386563%2fwhy-editing-png-file-result-in-text-disappear%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown