Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

108
LINES

< > BotCompany Repo | #1035064 // printPNGChunks

JavaX fragment (include) [tags: use-pretranspiled]

Uses 170K of libraries. Click here for Pure Java version (8071L/46K).

lib 1400572 // pngj

import ar.com.hjg.pngj.*;
import ar.com.hjg.pngj.chunks.*;

svoid printPNGChunks(File pngFile) {
  PngReaderDumb png = new(pngFile);
  png.setIncludeIdat(true);
  png.readAll();
  print(f2s(pngFile));
  pnlMap("Chunk ", png.getChunks(), chunk ->
    chunk + ", crcval=" + bytesToHex(chunk.crcval));
}

/**
 * Sample implementation of a very basic reader that only loads the empty chunks
 * (except the IHDR). The IDAT are optional.
 */
sclass PngReaderDumb {
  protected ChunkSeqReader chunkseq;
  protected final BufferedStreamFeeder streamFeeder;
  protected List<ChunkRaw> chunks = new ArrayList<ChunkRaw>();
  protected boolean includeIdat = true;
  protected ImageInfo imgInfo;
  private boolean interlaced = false;

  public PngReaderDumb(InputStream inputStream) {
    streamFeeder = new BufferedStreamFeeder(inputStream);
  }

  public PngReaderDumb(File file) {
    this(PngHelperInternal.istreamFromFile(file));
  }

  public void readAll() {
    chunkseq = createChunkSeqReader();
    try {
      streamFeeder.feedAll(chunkseq);
    } finally {
      close();
    }
  }

  protected ChunkSeqReader createChunkSeqReader() {
    ChunkSeqSkipping cs = new ChunkSeqSkipping(false) { // don't check CRC
      @Override
      protected void postProcessChunk(ChunkReader chunkR) {
        super.postProcessChunk(chunkR);
        if (!(chunkR.getChunkRaw().id.equals(ChunkHelper.IDAT) && !includeIdat))
          chunks.add(chunkR.getChunkRaw());
      }

      @Override
      protected void startNewChunk(int len, String id, long offset) {
        super.startNewChunk(len, id, offset);
        //
      }

      @Override
      protected boolean shouldSkipContent(int len, String id) {
        return !id.equals(ChunkHelper.IHDR); // we skip everything
      }
    };
    return cs;
  }

  public ImageInfo getImageInfo() {
    if (imgInfo == null) {
      if (chunks.size() > 0) {
        PngChunkIHDR ihdr = new PngChunkIHDR(null);
        ihdr.parseFromRaw(chunks.get(0));
        imgInfo = ihdr.createImageInfo();
        interlaced = ihdr.isInterlaced();
      }
    }
    return imgInfo;
  }

  public ChunkSeqReader getChunkseq() {
    return chunkseq;
  }

  public List<ChunkRaw> getChunks() {
    return chunks;
  }

  public void setIncludeIdat(boolean includeIdat) {
    this.includeIdat = includeIdat;
  }

  protected boolean shouldStoreChunkOnList(ChunkRaw raw) {
    return raw.id.equals("IDAT") && !includeIdat ? false : true;
  }

  public void setShouldCloseStream(boolean shouldCloseStream) {
    streamFeeder.setCloseStream(shouldCloseStream);
  }

  public void close() {
    if (chunkseq != null)
      chunkseq.close();
    streamFeeder.close();
  }

  public String toStringCompact() {
    return imgInfo.toStringBrief() + (interlaced ? "i" : "");
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035064
Snippet name: printPNGChunks
Eternal ID of this version: #1035064/5
Text MD5: 1f8bedadb745067043d9576e2bb16e68
Transpilation MD5: cbc89e825ac1f9591dc3eba9b1c75678
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-28 15:01:43
Source code size: 2911 bytes / 108 lines
Pitched / IR pitched: No / No
Views / Downloads: 133 / 208
Version history: 4 change(s)
Referenced in: [show references]