The map was one picture of one floor. A second floor was added, the blueprint changed size, and machines moved, so a position now records WHICH DRAWING its coordinates belong to. Buildings and levels (ADR-017). Each level owns its blueprint per theme and its own native pixel size; assets.mapx/mapy are pixels of assets.levelid, not of the site. A position whose level is unknown renders "level unknown" and is never drawn on the default level, because a marker on the wrong floor plan looks entirely correct while pointing at the wrong place. Repositioning in bulk: filter by unplaced, needs-review or level, search, place, confirm. Landmark recalibration solves the transform PER AXIS from landmark pairs and never from image dimensions - the canvas grew taller without rescaling, so a dimension-derived scale would stretch Y by 1.57 and be wrong everywhere. It defaults to a dry run, reports what would land off the drawing, snapshots before applying, and clears mapverifiedat because a transform is a guess awaiting review. Snapshots restore, including the level and the review state, and a restore snapshots first so an undo is undoable. Search: gaugelabreference was matched only for measuring tools and maintenancereference was matched nowhere at all, for any asset type, while Settings happily offers both identifiers on machines and PCs. A tag an operator is told to record has to be findable or it is a write-only field. USB devices and printed items were unreachable from search entirely - neither is an asset, so the generic asset search could not see them and no searcher existed; they now match on serial, asset tag, label, bin code and gage-lab tag, honouring isactive, with Settings toggles and result labels to match. The retired-application rule was half a rule: GET /api/knowledgebase hid articles whose topic application is retired while global search still returned them and printed the retired application as the subject. A filter is only real if every path that reaches the row applies it. Contract to 0.20.0 (additive): Asset gained levelid and mapverifiedat, Location gained levelid, and resolve_asset_position returns the levelid belonging to whichever source supplied the coordinates. The five plugins that write a map position are re-pinned. The install-list text format gained levelid as a NINTH field, appended, because the shipped Pascal installer reads fields 0-7 by index. That installer still compiles in one drawing's dimensions and bundles one blueprint, so its map is accurate for the default level only; /api/maplevels is deliberately unauthenticated so it can read both at runtime once rebuilt. Recorded in PRINTER-INSTALLER.md section 6 along with the other known gaps. Migration 7d33 converts an existing single-map site into one building and one default level carrying the old map_* settings, then assigns every placed asset and location to it. Nothing moves on screen. Old settings rows are kept so a rollback still finds them. Verified end to end on MySQL 5.6 from a production-shaped database.
135 lines
4.8 KiB
Python
135 lines
4.8 KiB
Python
"""Read an image's pixel dimensions from its header, with no image library.
|
|
|
|
A level's `mapwidth`/`mapheight` are the coordinate space every marker on it is
|
|
expressed in, so getting them wrong moves every marker relative to the drawing.
|
|
Reading them off the uploaded file removes the most likely way to get them
|
|
wrong, which is somebody typing what they remember.
|
|
|
|
Pillow would do this in one line and is not a dependency. Adding it would mean a
|
|
new cp314 win_amd64 wheel in the offline installer's hash-pinned wheelhouse -
|
|
built on Windows, verified against bundle-lock.json, shipped in a 240 MB
|
|
installer - to read two integers out of a header. This is the cheaper trade.
|
|
|
|
Returns (width, height), or (None, None) when the format is not one of these or
|
|
the header is truncated. A caller must treat None as "ask the operator" and
|
|
never as a default.
|
|
"""
|
|
|
|
import re
|
|
import struct
|
|
|
|
|
|
def png_size(data):
|
|
# An IHDR chunk always follows the 8-byte signature, and its first two
|
|
# fields are width and height as big-endian 32-bit integers.
|
|
if len(data) < 24 or data[:8] != b'\x89PNG\r\n\x1a\n':
|
|
return None, None
|
|
if data[12:16] != b'IHDR':
|
|
return None, None
|
|
width, height = struct.unpack('>II', data[16:24])
|
|
return width, height
|
|
|
|
|
|
def gif_size(data):
|
|
if len(data) < 10 or data[:6] not in (b'GIF87a', b'GIF89a'):
|
|
return None, None
|
|
width, height = struct.unpack('<HH', data[6:10])
|
|
return width, height
|
|
|
|
|
|
def webp_size(data):
|
|
if len(data) < 30 or data[:4] != b'RIFF' or data[8:12] != b'WEBP':
|
|
return None, None
|
|
kind = data[12:16]
|
|
if kind == b'VP8 ':
|
|
width, height = struct.unpack('<HH', data[26:30])
|
|
return width & 0x3FFF, height & 0x3FFF
|
|
if kind == b'VP8L':
|
|
bits = struct.unpack('<I', data[21:25])[0]
|
|
return (bits & 0x3FFF) + 1, ((bits >> 14) & 0x3FFF) + 1
|
|
if kind == b'VP8X':
|
|
width = int.from_bytes(data[24:27], 'little') + 1
|
|
height = int.from_bytes(data[27:30], 'little') + 1
|
|
return width, height
|
|
return None, None
|
|
|
|
|
|
def jpeg_size(data):
|
|
"""Walk the marker segments to the start-of-frame, which carries the size.
|
|
|
|
JPEG has no fixed header offset - the dimensions live in whichever SOF
|
|
marker the encoder used, after any number of application and comment
|
|
segments of varying length. So this walks rather than indexes.
|
|
"""
|
|
if len(data) < 4 or data[:2] != b'\xff\xd8':
|
|
return None, None
|
|
index = 2
|
|
end = len(data)
|
|
while index < end - 9:
|
|
if data[index] != 0xFF:
|
|
index += 1
|
|
continue
|
|
marker = data[index + 1]
|
|
# SOF0 through SOF15, excluding the DHT/JPG/DAC markers interleaved
|
|
# in that range, all carry height then width at the same offset.
|
|
if marker in (0xC0, 0xC1, 0xC2, 0xC3, 0xC5, 0xC6, 0xC7,
|
|
0xC9, 0xCA, 0xCB, 0xCD, 0xCE, 0xCF):
|
|
height, width = struct.unpack('>HH', data[index + 5:index + 9])
|
|
return width, height
|
|
if marker in (0xD8, 0x01) or 0xD0 <= marker <= 0xD9:
|
|
index += 2
|
|
continue
|
|
segment = struct.unpack('>H', data[index + 2:index + 4])[0]
|
|
index += 2 + segment
|
|
return None, None
|
|
|
|
|
|
def svg_size(data):
|
|
"""SVG states a size in attributes, or implies one through viewBox.
|
|
|
|
Width and height may carry units (mm, in, pt) or be percentages, and a
|
|
percentage says nothing about pixels - so a unit that is not px falls back to
|
|
the viewBox, which is unitless user space and is what a renderer scales to.
|
|
"""
|
|
try:
|
|
head = data[:4096].decode('utf-8', errors='replace')
|
|
except Exception:
|
|
return None, None
|
|
if '<svg' not in head:
|
|
return None, None
|
|
|
|
def attribute(name):
|
|
found = re.search(r'\b%s\s*=\s*["\']([^"\']+)["\']' % name, head)
|
|
return found.group(1).strip() if found else None
|
|
|
|
def pixels(value):
|
|
if not value:
|
|
return None
|
|
match = re.match(r'^([0-9.]+)\s*(px)?$', value)
|
|
return int(round(float(match.group(1)))) if match else None
|
|
|
|
width = pixels(attribute('width'))
|
|
height = pixels(attribute('height'))
|
|
if width and height:
|
|
return width, height
|
|
|
|
viewbox = attribute('viewBox')
|
|
if viewbox:
|
|
parts = re.split(r'[\s,]+', viewbox.strip())
|
|
if len(parts) == 4:
|
|
try:
|
|
return (int(round(float(parts[2]))),
|
|
int(round(float(parts[3]))))
|
|
except ValueError:
|
|
return None, None
|
|
return None, None
|
|
|
|
|
|
def image_size(data):
|
|
"""Dimensions of PNG, JPEG, GIF, WEBP or SVG data. (None, None) otherwise."""
|
|
for reader in (png_size, jpeg_size, gif_size, webp_size, svg_size):
|
|
width, height = reader(data)
|
|
if width and height:
|
|
return width, height
|
|
return None, None
|