CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
McIdasImagePlugin.py79 linesDownload Raw Back to PIL
1#
2# The Python Imaging Library.
3# $Id$
4#
5# Basic McIdas support for PIL
6#
7# History:
8# 1997-05-05 fl  Created (8-bit images only)
9# 2009-03-08 fl  Added 16/32-bit support.
10#
11# Thanks to Richard Jones and Craig Swank for specs and samples.
12#
13# Copyright (c) Secret Labs AB 1997.
14# Copyright (c) Fredrik Lundh 1997.
15#
16# See the README file for information on usage and redistribution.
17#
18from __future__ import annotations
19
20import struct
21
22from . import Image, ImageFile
23
24
25def _accept(prefix: bytes) -> bool:
26    return prefix.startswith(b"\x00\x00\x00\x00\x00\x00\x00\x04")
27
28
29##
30# Image plugin for McIdas area images.
31
32
33class McIdasImageFile(ImageFile.ImageFile):
34    format = "MCIDAS"
35    format_description = "McIdas area file"
36
37    def _open(self) -> None:
38        # parse area file directory
39        assert self.fp is not None
40
41        s = self.fp.read(256)
42        if not _accept(s) or len(s) != 256:
43            msg = "not an McIdas area file"
44            raise SyntaxError(msg)
45
46        self.area_descriptor_raw = s
47        self.area_descriptor = w = [0, *struct.unpack("!64i", s)]
48
49        # get mode
50        if w[11] == 1:
51            mode = rawmode = "L"
52        elif w[11] == 2:
53            mode = rawmode = "I;16B"
54        elif w[11] == 4:
55            # FIXME: add memory map support
56            mode = "I"
57            rawmode = "I;32B"
58        else:
59            msg = "unsupported McIdas format"
60            raise SyntaxError(msg)
61
62        self._mode = mode
63        self._size = w[10], w[9]
64
65        offset = w[34] + w[15]
66        stride = w[15] + w[10] * w[11] * w[14]
67
68        self.tile = [
69            ImageFile._Tile("raw", (0, 0) + self.size, offset, (rawmode, stride, 1))
70        ]
71
72
73# --------------------------------------------------------------------
74# registry
75
76Image.register_open(McIdasImageFile.format, McIdasImageFile, _accept)
77
78# no default extension
79 
Aluode/PerceptionLabPortable · CoolFace