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).

1  
lib 1400572 // pngj
2  
3  
import ar.com.hjg.pngj.*;
4  
import ar.com.hjg.pngj.chunks.*;
5  
6  
svoid printPNGChunks(File pngFile) {
7  
  PngReaderDumb png = new(pngFile);
8  
  png.setIncludeIdat(true);
9  
  png.readAll();
10  
  print(f2s(pngFile));
11  
  pnlMap("Chunk ", png.getChunks(), chunk ->
12  
    chunk + ", crcval=" + bytesToHex(chunk.crcval));
13  
}
14  
15  
/**
16  
 * Sample implementation of a very basic reader that only loads the empty chunks
17  
 * (except the IHDR). The IDAT are optional.
18  
 */
19  
sclass PngReaderDumb {
20  
  protected ChunkSeqReader chunkseq;
21  
  protected final BufferedStreamFeeder streamFeeder;
22  
  protected List<ChunkRaw> chunks = new ArrayList<ChunkRaw>();
23  
  protected boolean includeIdat = true;
24  
  protected ImageInfo imgInfo;
25  
  private boolean interlaced = false;
26  
27  
  public PngReaderDumb(InputStream inputStream) {
28  
    streamFeeder = new BufferedStreamFeeder(inputStream);
29  
  }
30  
31  
  public PngReaderDumb(File file) {
32  
    this(PngHelperInternal.istreamFromFile(file));
33  
  }
34  
35  
  public void readAll() {
36  
    chunkseq = createChunkSeqReader();
37  
    try {
38  
      streamFeeder.feedAll(chunkseq);
39  
    } finally {
40  
      close();
41  
    }
42  
  }
43  
44  
  protected ChunkSeqReader createChunkSeqReader() {
45  
    ChunkSeqSkipping cs = new ChunkSeqSkipping(false) { // don't check CRC
46  
      @Override
47  
      protected void postProcessChunk(ChunkReader chunkR) {
48  
        super.postProcessChunk(chunkR);
49  
        if (!(chunkR.getChunkRaw().id.equals(ChunkHelper.IDAT) && !includeIdat))
50  
          chunks.add(chunkR.getChunkRaw());
51  
      }
52  
53  
      @Override
54  
      protected void startNewChunk(int len, String id, long offset) {
55  
        super.startNewChunk(len, id, offset);
56  
        //
57  
      }
58  
59  
      @Override
60  
      protected boolean shouldSkipContent(int len, String id) {
61  
        return !id.equals(ChunkHelper.IHDR); // we skip everything
62  
      }
63  
    };
64  
    return cs;
65  
  }
66  
67  
  public ImageInfo getImageInfo() {
68  
    if (imgInfo == null) {
69  
      if (chunks.size() > 0) {
70  
        PngChunkIHDR ihdr = new PngChunkIHDR(null);
71  
        ihdr.parseFromRaw(chunks.get(0));
72  
        imgInfo = ihdr.createImageInfo();
73  
        interlaced = ihdr.isInterlaced();
74  
      }
75  
    }
76  
    return imgInfo;
77  
  }
78  
79  
  public ChunkSeqReader getChunkseq() {
80  
    return chunkseq;
81  
  }
82  
83  
  public List<ChunkRaw> getChunks() {
84  
    return chunks;
85  
  }
86  
87  
  public void setIncludeIdat(boolean includeIdat) {
88  
    this.includeIdat = includeIdat;
89  
  }
90  
91  
  protected boolean shouldStoreChunkOnList(ChunkRaw raw) {
92  
    return raw.id.equals("IDAT") && !includeIdat ? false : true;
93  
  }
94  
95  
  public void setShouldCloseStream(boolean shouldCloseStream) {
96  
    streamFeeder.setCloseStream(shouldCloseStream);
97  
  }
98  
99  
  public void close() {
100  
    if (chunkseq != null)
101  
      chunkseq.close();
102  
    streamFeeder.close();
103  
  }
104  
105  
  public String toStringCompact() {
106  
    return imgInfo.toStringBrief() + (interlaced ? "i" : "");
107  
  }
108  
}

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: 134 / 209
Version history: 4 change(s)
Referenced in: [show references]