[Test] GenBMP python module
authorMyungJoo Ham <myungjoo.ham@samsung.com>
Thu, 12 Jul 2018 10:35:32 +0000 (19:35 +0900)
committer문지중/동작제어Lab(SR)/Principal Engineer/삼성전자 <jijoong.moon@samsung.com>
Mon, 16 Jul 2018 00:36:33 +0000 (09:36 +0900)
Write BMP files based on the string input.
Addresses #266 partially.

Tested with locally modified nnstreamer_converter cases.

Signed-off-by: MyungJoo Ham <myungjoo.ham@samsung.com>
tests/gen24bBMP.py [new file with mode: 0644]

diff --git a/tests/gen24bBMP.py b/tests/gen24bBMP.py
new file mode 100644 (file)
index 0000000..413c22a
--- /dev/null
@@ -0,0 +1,62 @@
+#!/usr/bin/env python
+
+##
+# Copyright (C) 2018 Samsung Electronics
+# License: LGPL-2.1
+#
+# @file gen24bBMP.py
+# @brief Generate 24bpp .bmp files for test cases
+# @author MyungJoo Ham <myungjoo.ham@samsung.com>
+
+from __future__ import print_function
+
+from struct import pack
+import sys
+
+
+##
+# @brief Save bitmap "data" to "filename"
+# @param[in] filename The filename to be saves as a .png file.
+# @param[in] data string of RGB (packed in 'BBB') or BGRx (packed in 'BBBB')
+# @param[in] colorspace "RGB" or "BGRx"
+# @param[in] width Width of the picture
+# @param[in] height Height of the picture
+def saveBMP(filename, data, colorspace, width, height):
+    size = len(data)
+    graphics = ""
+
+    if colorspace == 'RGB':
+        assert(size == (width * height * 3))
+        # BMP is stored bottom to top. Reverse the order
+        for h in range(height-1, -1, -1):
+            for w in range(0, width):
+                pos = 3 * (w + width * h)
+                graphics += data[pos+2]
+                graphics += data[pos+1]
+                graphics += data[pos]
+            for x in range(0, (width * 3) % 4):
+                graphics += pack('<B', 0)
+    elif colorspace == 'BGRx':
+        assert(size == (width * height * 4))
+        # BMP is stored bottom to top. Reverse the order
+        for h in range(height-1, -1, -1):
+            for w in range(0, width):
+                pos = 4 * (w + width * h)
+                graphics += data[pos]
+                graphics += data[pos+1]
+                graphics += data[pos+2]
+            for x in range(0, (width * 3) % 4):
+                graphics += pack('<B', 0)
+    else:
+        print("Unrecognized colorspace %", colorspace)
+        sys.exit(1)
+
+    # BMP file header
+    header = ""
+    header += pack('<HLHHL', 19778, (26 + width * height * 3), 0, 0, 26)
+    header += pack('<LHHHH', 12, width, height, 1, 24)
+
+    file = open(filename, 'wb')
+    file.write(header)
+    file.write(graphics)
+    file.close()