Blob Blame History Raw
commit 4706031223f6809b40ef7d4c3f14103941621d57
Author: Richard Hughes <richard@hughsie.com>
Date:   Fri Apr 9 15:20:13 2021 +0100

    Write BMP data directly without using PIL
    
    This allows us to drop a build-time dep.

diff --git a/plugins/uefi-capsule/make-images.py b/plugins/uefi-capsule/make-images.py
index f1b00b8d..1d15bcf3 100755
--- a/plugins/uefi-capsule/make-images.py
+++ b/plugins/uefi-capsule/make-images.py
@@ -16,6 +16,8 @@ import argparse
 import tarfile
 import math
 import io
+import struct
+
 from typing import Dict, Optional, Any
 
 import cairo
@@ -24,7 +26,6 @@ import gi
 gi.require_version("Pango", "1.0")
 gi.require_version("PangoCairo", "1.0")
 from gi.repository import Pango, PangoCairo
-from PIL import Image
 
 
 def languages(podir: str):
@@ -60,6 +61,33 @@ class PotFile:
                     continue
 
 
+def _cairo_surface_write_to_bmp(img: cairo.ImageSurface) -> bytes:
+
+    data = bytes(img.get_data())
+    return (
+        b"BM"
+        + struct.pack(
+            "<ihhiiiihhiiiiii",
+            54 + len(data),  # size of BMP file
+            0,  # unused
+            0,  # unused
+            54,  # pixel array offset
+            40,  # DIB header
+            img.get_width(),  # width
+            -img.get_height(),  # height (top down)
+            1,  # planes
+            32,  # BPP
+            0,  # no compression
+            len(data),  # size of the raw bitmap data
+            2835,  # 72DPI H
+            2835,  # 72DPI V
+            0,  # palette
+            0,  # all colors are important
+        )
+        + data
+    )
+
+
 def main(args) -> int:
 
     # open output archive
@@ -164,20 +192,14 @@ def main(args) -> int:
                 fs.foreach(do_write, None)
                 img.flush()
 
-                # write PNG
-                with io.BytesIO() as io_png:
-                    img.write_to_png(io_png)
-                    io_png.seek(0)
-
-                    # convert to BMP and add to archive
-                    with io.BytesIO() as io_bmp:
-                        pimg = Image.open(io_png)
-                        pimg.save(io_bmp, format="BMP")
-                        filename = "fwupd-{}-{}-{}.bmp".format(lang, width, height)
-                        tarinfo = tarfile.TarInfo(filename)
-                        tarinfo.size = io_bmp.tell()
-                        io_bmp.seek(0)
-                        tar.addfile(tarinfo, fileobj=io_bmp)
+                # convert to BMP and add to archive
+                with io.BytesIO() as io_bmp:
+                    io_bmp.write(_cairo_surface_write_to_bmp(img))
+                    filename = "fwupd-{}-{}-{}.bmp".format(lang, width, height)
+                    tarinfo = tarfile.TarInfo(filename)
+                    tarinfo.size = io_bmp.tell()
+                    io_bmp.seek(0)
+                    tar.addfile(tarinfo, fileobj=io_bmp)
 
     # success
     return 0
diff --git a/po/test-deps b/po/test-deps
index f5276daa..27b4055b 100755
--- a/po/test-deps
+++ b/po/test-deps
@@ -34,12 +34,6 @@ except ValueError:
     print("Error: missing cairo gobject introspection library")
     err = 1
 
-try:
-    from PIL import Image
-except ImportError:
-    print("Error: missing dependency python pillow (python3-pil)")
-    err = 1
-
 try:
     import cairo
 except ImportError: