ctucx.git: nimjpg

parse jpg file header

commit 0a72ba7cd4dcbe80b4594becbc81ae4cd5b2f9e2
parent 7cfcb30c2f3ffc4e71df1445c99f2a9444d2cb86
Author: Milan Pässler <milan@petabyte.dev>
Date: Sat, 13 Mar 2021 16:20:15 +0100

choose sof section with bigger resolution
2 files changed, 5 insertions(+), 3 deletions(-)
M
nimjpg.nim
|
6
++++--
M
utils.nim
|
2
+-
diff --git a/nimjpg.nim b/nimjpg.nim
@@ -49,7 +49,6 @@ proc getSectionSize(file: Stream | AsyncFile): Future[uint16] {.multisync.} =
   var size: uint16
   let val = toHex(file.read(2).await)
   discard parseHex(val, size)
-  echo val
   return size - uint16(2)
 
 proc skipSection(file: Stream | AsyncFile): Future[int] {.multisync.} =

@@ -68,6 +67,9 @@ proc process_sof*(file: Stream | AsyncFile): Future[SofData] {.multisync.} =
   discard parseHex(toHex(file.read(2).await), result.width)
   discard parseHex(toHex(file.read(1).await), result.components)
 
+proc calcRes(sd: SofData): uint64 =
+  return uint64(sd.height) * uint64(sd.width)
+
 proc collect_jpg*(file: Stream | AsyncFile): Future[JpgInfo] {.multisync,gcsafe.} =
   var done = false
   var byte: string

@@ -107,7 +109,7 @@ proc collect_jpg*(file: Stream | AsyncFile): Future[JpgInfo] {.multisync,gcsafe.
         let section_start = file.getFilePos() + 2
         let section_end = section_start + int64(file.getSectionSize().await)
         let sd = file.process_sof().await
-        if result.sofData.isNone:
+        if result.sofData.isNone or sd.calcRes() > result.sofData.get.calcRes():
           result.sofData = some(sd)
         debug sd
         file.setFilePos(section_end)
diff --git a/utils.nim b/utils.nim
@@ -21,4 +21,4 @@ proc setFilePos*(stream: Stream, position: int64) =
   stream.setPosition(int(position))
 
 proc readBuffer*(stream: Stream, buffer: pointer, length: int): int =
-  result = stream.readData(buffer, length)
+  return stream.readData(buffer, length)