Skip to content

Commit fddce67

Browse files
committed
fix: generate valid 256x256 transparent PNG for empty tiles
1 parent 097cdea commit fddce67

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

apps/web/tsconfig.tsbuildinfo

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

services/api/app/routers/share.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,28 @@
3737
_http_client: httpx.AsyncClient | None = None
3838

3939

40-
# 1×1 transparent PNG (67 bytes) — returned for tiles outside COG extent
41-
_EMPTY_TILE = (
42-
b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01"
43-
b"\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89"
44-
b"\x00\x00\x00\nIDATx\x9cc\x00\x01\x00\x00\x05\x00\x01"
45-
b"\r\n\xb4\x00\x00\x00\x00IEND\xaeB`\x82"
46-
)
40+
def _make_empty_tile() -> bytes:
41+
"""Generate a valid 256×256 transparent PNG at import time."""
42+
import struct
43+
import zlib
44+
45+
width, height = 256, 256
46+
47+
def _chunk(ctype: bytes, data: bytes) -> bytes:
48+
c = ctype + data
49+
crc = struct.pack(">I", zlib.crc32(c) & 0xFFFFFFFF)
50+
return struct.pack(">I", len(data)) + c + crc
51+
52+
sig = b"\x89PNG\r\n\x1a\n"
53+
ihdr = _chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 6, 0, 0, 0))
54+
scanline = b"\x00" + b"\x00\x00\x00\x00" * width
55+
raw = scanline * height
56+
idat = _chunk(b"IDAT", zlib.compress(raw))
57+
iend = _chunk(b"IEND", b"")
58+
return sig + ihdr + idat + iend
59+
60+
61+
_EMPTY_TILE = _make_empty_tile()
4762

4863

4964
def _get_http_client() -> httpx.AsyncClient:

0 commit comments

Comments
 (0)