poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2] pickles: Add new pickle for jpeg


From: Andreas Klinger
Subject: [PATCH v2] pickles: Add new pickle for jpeg
Date: Mon, 12 Feb 2024 18:55:08 +0100

2024-02-12  Andreas Klinger  <ak@it-klinger.de>

        * pickles/jpeg.pk: New pickle for jpeg files
---

Thanks for the reviews and recommendations of Mohammad and Jose.

The pickle have been reworked to recognize the image part of the jpeg properly.

This patch is available in the repo as branch anderl/add-pickle-jpeg-v2

 ChangeLog           |  4 ++
 pickles/Makefile.am |  2 +-
 pickles/jpeg.pk     | 94 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 99 insertions(+), 1 deletion(-)
 create mode 100644 pickles/jpeg.pk

diff --git a/ChangeLog b/ChangeLog
index f101815d..d9abd119 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2024-02-12  Andreas Klinger  <ak@it-klinger.de>
+
+       * pickles/jpeg.pk: New pickle for jpeg files
+
 2024-02-12  Jose E. Marchesi  <jemarch@gnu.org>
 
        * bootstrap.conf (gnulib_modules): Import regex module from
diff --git a/pickles/Makefile.am b/pickles/Makefile.am
index 177aac70..eb993711 100644
--- a/pickles/Makefile.am
+++ b/pickles/Makefile.am
@@ -28,6 +28,6 @@ dist_pickles_DATA = ctf.pk ctf-dump.pk leb128.pk \
                    uuid.pk redoxfs.pk pcap.pk ieee754.pk pdap.pk \
                     iscan.pk iscan-str.pk base64.pk gpt-partition-attrs.pk \
                    gpt-partition-types.pk gpt.pk guid.pk linux.pk srec.pk \
-                   jojodiff.pk
+                   jojodiff.pk jpeg.pk
 
 EXTRA_DIST = README.elf
diff --git a/pickles/jpeg.pk b/pickles/jpeg.pk
new file mode 100644
index 00000000..045e2ec8
--- /dev/null
+++ b/pickles/jpeg.pk
@@ -0,0 +1,94 @@
+/* jpeg.pk - JPEG implementation for GNU poke */
+
+/* Copyright (C) 2024 Andreas Klinger */
+
+/* This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* Implemented as specified in
+ * https://en.wikipedia.org/wiki/JPEG as well as
+ * https://de.wikipedia.org/wiki/JPEG_File_Interchange_Format  */
+
+/* Marker of JPEG pictures
+ * Array id 0 ("SOF0") starts at binary marker value 0xC0:
+ *   0xC0, 0xC1, ... , 0xC7
+ *   0xC8, 0xC1, ... , 0xCF
+ *   0xD0, 0xD1, ... , 0xD7  (restart marker)
+ *   ...
+ *   0xF8, 0xF1, ... , 0xFF
+ */
+var jpeg_marker =
+  ["SOF0", "SOF1", "SOF2", "SOF3", "DHT", "SOF5", "SOF6", "SOF7",
+   "JPG", "SOF9", "SOF10", "SOF11", "DAC", "SOF13", "SOF14", "SOF15",
+   "-", "-", "-", "-", "-", "-", "-", "-",
+   "SOI", "EOI", "SOS", "DQT", "-", "DRI", "-", "-",
+   "APP0", "APP1", "APP2", "APP3", "APP4", "APP5", "APP6", "APP7",
+   "APP8", "APP9", "APP10", "APP11", "APP12", "APP13", "APP14", "APP15",
+   "-", "-", "-", "-", "-", "-", "-", "-",
+   "-", "-", "-", "-", "-", "-", "COM", "-"];
+
+/* Start and stop marker */
+type JPEG_STS =
+  struct
+  {
+    byte ff = 0xFF;
+    byte xx : xx == 0xD8 || xx == 0xD9 || xx == 0xDA;
+    method _print = void:
+    {
+      printf ("\n#<tag: %u16x\tsymbol: %s>",
+                        ff:::xx, jpeg_marker[xx - 0xC0]);
+    }
+  };
+
+type JPEG_Segment =
+  struct
+  {
+    byte ff == 0xFF;
+    byte xx : xx != 0xD8 && xx != 0xDA && xx != 0xD9 && xx >= 0xC0 && xx < 
0xFF;
+    uint16 size : size >= 2;
+    char[size >= 2 ? size - 2 : 0U] data;
+    method _print = void:
+    {
+      printf ("\n#<tag: %u16x\tsymbol: %s\tsize: %u16d>",
+                        ff:::xx, jpeg_marker[xx - 0xC0], size);
+    }
+  };
+
+type JPEG_Tag =
+  struct
+  {
+    type Image =
+      union
+      {
+        uint<8>[2] pair: ((pair[1] != 0xFFUB) && (pair != [0xFFUB,0xD9UB])) || 
(pair == [0xFFUB,0x00UB]);
+        uint<8> single: single != 0xFFUB;
+      };
+    JPEG_STS soi: soi.xx == 0xD8UB;
+    JPEG_Segment[] segments;
+    JPEG_STS sos: sos.xx == 0xDAUB;
+    Image[] image;
+    JPEG_STS eoi: eoi.xx == 0xD9UB;
+    method get_nsegs = int:
+    {
+      return segments'length;
+    }
+    method get_image = uint<8>[]:
+    {
+      var bytes = uint<8>[]();
+      for (i in image)
+        try bytes += i.pair;
+        catch if E_elem { bytes += [i.single]; }
+      return bytes;
+    }
+  };
-- 
2.39.2

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]