summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMilan Pässler <milan@petabyte.dev>2021-03-13 15:05:55 +0100
committerMilan Pässler <milan@petabyte.dev>2021-03-13 15:05:55 +0100
commit6c2b1b777bc3903930a7e28d0f5800c2e72ab2ab (patch)
tree6d5b4142f3abbde0f3e919330f19797b90f0f677
parent55c1ef17b528b7892caba6a69f8e4d79bce2dd85 (diff)
downloadnimjpg-6c2b1b777bc3903930a7e28d0f5800c2e72ab2ab.tar.gz
nimjpg-6c2b1b777bc3903930a7e28d0f5800c2e72ab2ab.tar.bz2
nimjpg-6c2b1b777bc3903930a7e28d0f5800c2e72ab2ab.zip
multisync
-rw-r--r--example.nim2
-rw-r--r--example_sync.nim23
-rw-r--r--jpgnim.nim (renamed from jpg.nim)28
-rw-r--r--jpgnim.nimble13
-rw-r--r--utils.nim14
5 files changed, 69 insertions, 11 deletions
diff --git a/example.nim b/example.nim
index e830c5f..e358e89 100644
--- a/example.nim
+++ b/example.nim
@@ -1,5 +1,5 @@
import os
-import jpg
+import jpgnim
import asyncdispatch
import asyncfile
import json
diff --git a/example_sync.nim b/example_sync.nim
new file mode 100644
index 0000000..d1f9b9f
--- /dev/null
+++ b/example_sync.nim
@@ -0,0 +1,23 @@
+import os
+import jpgnim
+import asyncdispatch
+import asyncfile
+import json
+
+var result: cint
+
+if paramCount() == 0:
+ echo "Gimme a file pls"
+ quit(QuitFailure)
+
+let path = paramStr(1)
+
+init_jpg()
+
+if fileExists(path):
+ let file = open(path)
+ echo pretty(%* collect_jpg(file))
+else:
+ echo "Gimme an existing file pls"
+
+deinit_jpg()
diff --git a/jpg.nim b/jpgnim.nim
index 57d3ee4..5a3f898 100644
--- a/jpg.nim
+++ b/jpgnim.nim
@@ -1,4 +1,5 @@
import os
+import streams
import strutils
import asyncfile
import asyncdispatch
@@ -7,6 +8,7 @@ import exifnim/helpers
import tables
import parseutils
import options
+
import utils
var buf {.threadvar.}: pointer
@@ -42,38 +44,39 @@ type JpgInfo* = object
exifData*: Option[Table[string, string]]
sofData*: Option[SofData]
-proc getSectionSize(file: AsyncFile): Future[uint16] {.async.} =
+proc getSectionSize(file: Stream | AsyncFile): Future[uint16] {.multisync.} =
# FIXME consider endianness
var size: uint16
let val = toHex(file.read(2).await)
discard parseHex(val, size)
+ echo val
return size - uint16(2)
-proc skipSection(file: AsyncFile): Future[void] {.async.} =
+proc skipSection(file: Stream | AsyncFile): Future[int] {.multisync.} =
let size = int64(file.getSectionSize().await)
debug("skipping ", size)
file.setFilePos(file.getFilePos() + size)
-proc expect(file: AsyncFile, expected: string): Future[void] {.async.} =
+proc expect(file: Stream | AsyncFile, expected: string): Future[int] {.multisync.} =
let byte = file.read(1).await
if byte != expected:
error("expected ", toHex(expected),", got ", toHex(byte))
-proc process_sof*(file: AsyncFile): Future[SofData] {.gcsafe,async.} =
+proc process_sof*(file: Stream | AsyncFile): Future[SofData] {.multisync.} =
discard parseHex(toHex(file.read(1).await), result.precision)
discard parseHex(toHex(file.read(2).await), result.height)
discard parseHex(toHex(file.read(2).await), result.width)
discard parseHex(toHex(file.read(1).await), result.components)
-proc collect_jpg*(file: AsyncFile): Future[JpgInfo] {.gcsafe,async.} =
+proc collect_jpg*(file: Stream | AsyncFile): Future[JpgInfo] {.multisync,gcsafe.} =
var done = false
var byte: string
- file.expect(MARKER_START).await
- file.expect(SOI).await
+ discard file.expect(MARKER_START).await
+ discard file.expect(SOI).await
while not done:
- file.expect(MARKER_START).await
+ discard file.expect(MARKER_START).await
let marker = file.read(1).await
@@ -97,7 +100,7 @@ proc collect_jpg*(file: AsyncFile): Future[JpgInfo] {.gcsafe,async.} =
debug ed_table
of JFIF:
debug "found JFIF"
- file.skipSection().await
+ discard file.skipSection().await
else:
if toHex(marker).startsWith("C"):
debug "found SOF"
@@ -111,4 +114,9 @@ proc collect_jpg*(file: AsyncFile): Future[JpgInfo] {.gcsafe,async.} =
continue
debug("unknown section: ", toHex(marker))
- file.skipSection().await
+ discard file.skipSection().await
+
+proc collect_jpg*(file: File): JpgInfo =
+ let stream = newFileStream(file)
+ result = collect_jpg(stream)
+ stream.close()
diff --git a/jpgnim.nimble b/jpgnim.nimble
new file mode 100644
index 0000000..2a514ca
--- /dev/null
+++ b/jpgnim.nimble
@@ -0,0 +1,13 @@
+# Package
+
+version = "0.1.0"
+author = "ctucx, Milan"
+description = "Read jpg headers"
+license = "GPL-3.0"
+srcDir = "./"
+bin = @["example", "example_sync"]
+installFiles = @["jpgnim.nim"]
+
+# Dependencies
+requires "nim >= 1.4"
+requires "https://cgit.ctu.cx/exifnim/#main"
diff --git a/utils.nim b/utils.nim
index 76f8d02..fb7634e 100644
--- a/utils.nim
+++ b/utils.nim
@@ -1,3 +1,5 @@
+import streams
+
template debug*(x: varargs[untyped]) =
if not defined(release):
echo(x)
@@ -5,3 +7,15 @@ template debug*(x: varargs[untyped]) =
template error*(x: varargs[untyped]) =
echo(x)
quit(1)
+
+proc read*(stream: Stream, length: int): string =
+ return stream.readStr(length)
+
+proc getFilePos*(stream: Stream): int64 =
+ return int64(stream.getPosition())
+
+proc setFilePos*(stream: Stream, position: int64) =
+ stream.setPosition(int(position))
+
+proc readBuffer*(stream: Stream, buffer: pointer, length: int): int =
+ result = stream.readData(buffer, length)