Why editing PNG file result in tEXt disappear?












0















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.










share|improve this question





























    0















    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.










    share|improve this question



























      0












      0








      0








      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.










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 6:05









      Owain Esau

      879718




      879718










      asked Nov 20 '18 at 5:08









      JosephJoseph

      1124




      1124
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          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

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]