#FEATURE by cg
authorClaus Gittinger <cg@exept.de>
Tue, 14 Feb 2017 11:47:47 +0100
changeset 3889 b7c261a8614d
parent 3888 72dcf9d128f0
child 3890 37131d0d77c6
#FEATURE by cg class: PNGReader pluggable extra chunk handlers class definition added: #processNIVIChunkLen: #specialChunkHandlerAt:put: changed: #processChunk:len:
PNGReader.st
--- a/PNGReader.st	Mon Feb 13 20:49:58 2017 +0100
+++ b/PNGReader.st	Tue Feb 14 11:47:47 2017 +0100
@@ -14,12 +14,12 @@
 "{ NameSpace: Smalltalk }"
 
 ImageReader subclass:#PNGReader
-        instanceVariableNames:'colorType bitsPerChannel depth compressionMethod filterMethod
-                interlaceMode bytesPerScanline globalDataChunk thisScanline
-                prevScanline processTextChunks'
-        classVariableNames:'Verbose'
-        poolDictionaries:''
-        category:'Graphics-Images-Readers'
+	instanceVariableNames:'colorType bitsPerChannel depth compressionMethod filterMethod
+		interlaceMode bytesPerScanline globalDataChunk thisScanline
+		prevScanline processTextChunks specialChunkHandlers'
+	classVariableNames:'Verbose'
+	poolDictionaries:''
+	category:'Graphics-Images-Readers'
 !
 
 !PNGReader class methodsFor:'documentation'!
@@ -131,6 +131,21 @@
     "Modified: 21.6.1996 / 20:38:46 / cg"
 ! !
 
+!PNGReader methodsFor:'hooks'!
+
+specialChunkHandlerAt:chunkType put:aHandlerBlock
+    "define a handler action, which processes unknown chunks.
+     The handler block will be called with one or two arguments:
+     the binary data of the chunk, and optionally the reader itself"
+
+    specialChunkHandlers isNil ifTrue:[ 
+        specialChunkHandlers := Dictionary new
+    ].
+    specialChunkHandlers at:chunkType put:aHandlerBlock
+
+    "Created: / 14-02-2017 / 11:43:33 / cg"
+! !
+
 !PNGReader methodsFor:'private-chunks'!
 
 doPass: pass
@@ -215,14 +230,25 @@
 
     type = 'iTXt' ifTrue:[^ self processITXTChunkLen:len].
     type = 'iCCP' ifTrue:[^ self processICCPChunkLen:len].
-    
+
+    specialChunkHandlers notNil ifTrue:[
+        |handler chunkData|
+        
+        handler := specialChunkHandlers at:type ifAbsent:nil.
+        handler notNil ifTrue:[
+            chunkData := inStream nextBytes:len.
+            handler value:chunkData optionalArgument:self.
+            ^ true.
+        ].
+    ].
+
     ('PNGReader: unrecognized chunk: ' , type , ' ignored.') infoPrintCR.
 
     inStream skip:len.
     ^ true.
 
-    "Created: 21.6.1996 / 21:10:37 / cg"
-    "Modified: 21.6.1996 / 21:22:37 / cg"
+    "Created: / 21-06-1996 / 21:10:37 / cg"
+    "Modified: / 14-02-2017 / 11:46:40 / cg"
 !
 
 processGAMAChunkLen:len
@@ -382,6 +408,22 @@
     "Modified: / 03-05-2011 / 12:03:33 / cg"
 !
 
+processNIVIChunkLen:len
+    |viData|
+    
+    "/ this cunk contains a labView vi.
+    "/ ignored for now...
+"/    inStream skip:len.
+"/    ^ true.
+    "/ inStream nextBytes:len.
+
+    viData := inStream nextBytes:len.
+    self halt.
+    ^ true
+
+    "Created: / 14-02-2017 / 08:57:29 / cg"
+!
+
 processNonInterlacedDATA:len
     self halt:'not used'
 "/