| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
"""Module imageio/freeimage.py |
| 7 |
|
| 8 |
This module contains the wrapper code for the freeimage library. |
| 9 |
The functions defined in this module are relatively thin; just thin |
| 10 |
enough so that arguments and results are native Python/numpy data |
| 11 |
types. |
| 12 |
|
| 13 |
""" |
| 14 |
|
| 15 |
import os |
| 16 |
import sys |
| 17 |
import ctypes |
| 18 |
import threading |
| 19 |
import logging |
| 20 |
import numpy |
| 21 |
|
| 22 |
from ..core import ( |
| 23 |
get_remote_file, |
| 24 |
load_lib, |
| 25 |
Dict, |
| 26 |
resource_dirs, |
| 27 |
IS_PYPY, |
| 28 |
get_platform, |
| 29 |
InternetNotAllowedError, |
| 30 |
NeedDownloadError, |
| 31 |
) |
| 32 |
|
| 33 |
logger = logging.getLogger(__name__) |
| 34 |
|
| 35 |
TEST_NUMPY_NO_STRIDES = False |
| 36 |
|
| 37 |
FNAME_PER_PLATFORM = { |
| 38 |
"osx32": "libfreeimage-3.16.0-osx10.6.dylib", |
| 39 |
"osx64": "libfreeimage-3.16.0-osx10.6.dylib", |
| 40 |
"win32": "FreeImage-3.18.0-win32.dll", |
| 41 |
"win64": "FreeImage-3.18.0-win64.dll", |
| 42 |
"linux32": "libfreeimage-3.16.0-linux32.so", |
| 43 |
"linux64": "libfreeimage-3.16.0-linux64.so", |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
def download(directory=None, force_download=False): |
| 48 |
"""Download the FreeImage library to your computer. |
| 49 |
|
| 50 |
Parameters |
| 51 |
---------- |
| 52 |
directory : str | None |
| 53 |
The directory where the file will be cached if a download was |
| 54 |
required to obtain the file. By default, the appdata directory |
| 55 |
is used. This is also the first directory that is checked for |
| 56 |
a local version of the file. |
| 57 |
force_download : bool | str |
| 58 |
If True, the file will be downloaded even if a local copy exists |
| 59 |
(and this copy will be overwritten). Can also be a YYYY-MM-DD date |
| 60 |
to ensure a file is up-to-date (modified date of a file on disk, |
| 61 |
if present, is checked). |
| 62 |
""" |
| 63 |
plat = get_platform() |
| 64 |
if plat and plat in FNAME_PER_PLATFORM: |
| 65 |
fname = "freeimage/" + FNAME_PER_PLATFORM[plat] |
| 66 |
get_remote_file(fname=fname, directory=directory, force_download=force_download) |
| 67 |
fi._lib = None |
| 68 |
|
| 69 |
|
| 70 |
def get_freeimage_lib(): |
| 71 |
"""Ensure we have our version of the binary freeimage lib.""" |
| 72 |
|
| 73 |
lib = os.getenv("IMAGEIO_FREEIMAGE_LIB", None) |
| 74 |
if lib: |
| 75 |
return lib |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
plat = get_platform() |
| 80 |
if plat and plat in FNAME_PER_PLATFORM: |
| 81 |
try: |
| 82 |
return get_remote_file("freeimage/" + FNAME_PER_PLATFORM[plat], auto=False) |
| 83 |
except InternetNotAllowedError: |
| 84 |
pass |
| 85 |
except NeedDownloadError: |
| 86 |
raise NeedDownloadError( |
| 87 |
"Need FreeImage library. " |
| 88 |
"You can obtain it with either:\n" |
| 89 |
" - download using the command: " |
| 90 |
"imageio_download_bin freeimage\n" |
| 91 |
" - download by calling (in Python): " |
| 92 |
"imageio.plugins.freeimage.download()\n" |
| 93 |
) |
| 94 |
except RuntimeError as e: |
| 95 |
logger.warning(str(e)) |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
def efn(x): |
| 100 |
return x.encode(sys.getfilesystemencoding()) |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
GREY_PALETTE = numpy.arange(0, 0x01000000, 0x00010101, dtype=numpy.uint32) |
| 105 |
|
| 106 |
|
| 107 |
class FI_TYPES(object): |
| 108 |
FIT_UNKNOWN = 0 |
| 109 |
FIT_BITMAP = 1 |
| 110 |
FIT_UINT16 = 2 |
| 111 |
FIT_INT16 = 3 |
| 112 |
FIT_UINT32 = 4 |
| 113 |
FIT_INT32 = 5 |
| 114 |
FIT_FLOAT = 6 |
| 115 |
FIT_DOUBLE = 7 |
| 116 |
FIT_COMPLEX = 8 |
| 117 |
FIT_RGB16 = 9 |
| 118 |
FIT_RGBA16 = 10 |
| 119 |
FIT_RGBF = 11 |
| 120 |
FIT_RGBAF = 12 |
| 121 |
|
| 122 |
dtypes = { |
| 123 |
FIT_BITMAP: numpy.uint8, |
| 124 |
FIT_UINT16: numpy.uint16, |
| 125 |
FIT_INT16: numpy.int16, |
| 126 |
FIT_UINT32: numpy.uint32, |
| 127 |
FIT_INT32: numpy.int32, |
| 128 |
FIT_FLOAT: numpy.float32, |
| 129 |
FIT_DOUBLE: numpy.float64, |
| 130 |
FIT_COMPLEX: numpy.complex128, |
| 131 |
FIT_RGB16: numpy.uint16, |
| 132 |
FIT_RGBA16: numpy.uint16, |
| 133 |
FIT_RGBF: numpy.float32, |
| 134 |
FIT_RGBAF: numpy.float32, |
| 135 |
} |
| 136 |
|
| 137 |
fi_types = { |
| 138 |
(numpy.uint8, 1): FIT_BITMAP, |
| 139 |
(numpy.uint8, 3): FIT_BITMAP, |
| 140 |
(numpy.uint8, 4): FIT_BITMAP, |
| 141 |
(numpy.uint16, 1): FIT_UINT16, |
| 142 |
(numpy.int16, 1): FIT_INT16, |
| 143 |
(numpy.uint32, 1): FIT_UINT32, |
| 144 |
(numpy.int32, 1): FIT_INT32, |
| 145 |
(numpy.float32, 1): FIT_FLOAT, |
| 146 |
(numpy.float64, 1): FIT_DOUBLE, |
| 147 |
(numpy.complex128, 1): FIT_COMPLEX, |
| 148 |
(numpy.uint16, 3): FIT_RGB16, |
| 149 |
(numpy.uint16, 4): FIT_RGBA16, |
| 150 |
(numpy.float32, 3): FIT_RGBF, |
| 151 |
(numpy.float32, 4): FIT_RGBAF, |
| 152 |
} |
| 153 |
|
| 154 |
extra_dims = { |
| 155 |
FIT_UINT16: [], |
| 156 |
FIT_INT16: [], |
| 157 |
FIT_UINT32: [], |
| 158 |
FIT_INT32: [], |
| 159 |
FIT_FLOAT: [], |
| 160 |
FIT_DOUBLE: [], |
| 161 |
FIT_COMPLEX: [], |
| 162 |
FIT_RGB16: [3], |
| 163 |
FIT_RGBA16: [4], |
| 164 |
FIT_RGBF: [3], |
| 165 |
FIT_RGBAF: [4], |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
class IO_FLAGS(object): |
| 170 |
FIF_LOAD_NOPIXELS = 0x8000 |
| 171 |
|
| 172 |
BMP_DEFAULT = 0 |
| 173 |
BMP_SAVE_RLE = 1 |
| 174 |
CUT_DEFAULT = 0 |
| 175 |
DDS_DEFAULT = 0 |
| 176 |
EXR_DEFAULT = 0 |
| 177 |
EXR_FLOAT = 0x0001 |
| 178 |
EXR_NONE = 0x0002 |
| 179 |
EXR_ZIP = 0x0004 |
| 180 |
EXR_PIZ = 0x0008 |
| 181 |
EXR_PXR24 = 0x0010 |
| 182 |
EXR_B44 = 0x0020 |
| 183 |
|
| 184 |
EXR_LC = 0x0040 |
| 185 |
|
| 186 |
FAXG3_DEFAULT = 0 |
| 187 |
GIF_DEFAULT = 0 |
| 188 |
GIF_LOAD256 = 1 |
| 189 |
|
| 190 |
GIF_PLAYBACK = 2 |
| 191 |
|
| 192 |
HDR_DEFAULT = 0 |
| 193 |
ICO_DEFAULT = 0 |
| 194 |
ICO_MAKEALPHA = 1 |
| 195 |
|
| 196 |
IFF_DEFAULT = 0 |
| 197 |
J2K_DEFAULT = 0 |
| 198 |
JP2_DEFAULT = 0 |
| 199 |
JPEG_DEFAULT = 0 |
| 200 |
|
| 201 |
JPEG_FAST = 0x0001 |
| 202 |
|
| 203 |
JPEG_ACCURATE = 0x0002 |
| 204 |
|
| 205 |
JPEG_CMYK = 0x0004 |
| 206 |
|
| 207 |
JPEG_EXIFROTATE = 0x0008 |
| 208 |
|
| 209 |
JPEG_QUALITYSUPERB = 0x80 |
| 210 |
JPEG_QUALITYGOOD = 0x0100 |
| 211 |
JPEG_QUALITYNORMAL = 0x0200 |
| 212 |
JPEG_QUALITYAVERAGE = 0x0400 |
| 213 |
JPEG_QUALITYBAD = 0x0800 |
| 214 |
JPEG_PROGRESSIVE = 0x2000 |
| 215 |
|
| 216 |
JPEG_SUBSAMPLING_411 = 0x1000 |
| 217 |
|
| 218 |
JPEG_SUBSAMPLING_420 = 0x4000 |
| 219 |
|
| 220 |
JPEG_SUBSAMPLING_422 = 0x8000 |
| 221 |
JPEG_SUBSAMPLING_444 = 0x10000 |
| 222 |
JPEG_OPTIMIZE = 0x20000 |
| 223 |
|
| 224 |
JPEG_BASELINE = 0x40000 |
| 225 |
KOALA_DEFAULT = 0 |
| 226 |
LBM_DEFAULT = 0 |
| 227 |
MNG_DEFAULT = 0 |
| 228 |
PCD_DEFAULT = 0 |
| 229 |
PCD_BASE = 1 |
| 230 |
PCD_BASEDIV4 = 2 |
| 231 |
PCD_BASEDIV16 = 3 |
| 232 |
PCX_DEFAULT = 0 |
| 233 |
PFM_DEFAULT = 0 |
| 234 |
PICT_DEFAULT = 0 |
| 235 |
PNG_DEFAULT = 0 |
| 236 |
PNG_IGNOREGAMMA = 1 |
| 237 |
PNG_Z_BEST_SPEED = 0x0001 |
| 238 |
|
| 239 |
PNG_Z_DEFAULT_COMPRESSION = 0x0006 |
| 240 |
|
| 241 |
PNG_Z_BEST_COMPRESSION = 0x0009 |
| 242 |
|
| 243 |
PNG_Z_NO_COMPRESSION = 0x0100 |
| 244 |
PNG_INTERLACED = 0x0200 |
| 245 |
|
| 246 |
PNM_DEFAULT = 0 |
| 247 |
PNM_SAVE_RAW = 0 |
| 248 |
PNM_SAVE_ASCII = 1 |
| 249 |
PSD_DEFAULT = 0 |
| 250 |
PSD_CMYK = 1 |
| 251 |
PSD_LAB = 2 |
| 252 |
RAS_DEFAULT = 0 |
| 253 |
RAW_DEFAULT = 0 |
| 254 |
RAW_PREVIEW = 1 |
| 255 |
|
| 256 |
RAW_DISPLAY = 2 |
| 257 |
SGI_DEFAULT = 0 |
| 258 |
TARGA_DEFAULT = 0 |
| 259 |
TARGA_LOAD_RGB888 = 1 |
| 260 |
TARGA_SAVE_RLE = 2 |
| 261 |
TIFF_DEFAULT = 0 |
| 262 |
TIFF_CMYK = 0x0001 |
| 263 |
|
| 264 |
TIFF_PACKBITS = 0x0100 |
| 265 |
TIFF_DEFLATE = 0x0200 |
| 266 |
TIFF_ADOBE_DEFLATE = 0x0400 |
| 267 |
TIFF_NONE = 0x0800 |
| 268 |
TIFF_CCITTFAX3 = 0x1000 |
| 269 |
TIFF_CCITTFAX4 = 0x2000 |
| 270 |
TIFF_LZW = 0x4000 |
| 271 |
TIFF_JPEG = 0x8000 |
| 272 |
TIFF_LOGLUV = 0x10000 |
| 273 |
WBMP_DEFAULT = 0 |
| 274 |
XBM_DEFAULT = 0 |
| 275 |
XPM_DEFAULT = 0 |
| 276 |
|
| 277 |
|
| 278 |
class METADATA_MODELS(object): |
| 279 |
FIMD_COMMENTS = 0 |
| 280 |
FIMD_EXIF_MAIN = 1 |
| 281 |
FIMD_EXIF_EXIF = 2 |
| 282 |
FIMD_EXIF_GPS = 3 |
| 283 |
FIMD_EXIF_MAKERNOTE = 4 |
| 284 |
FIMD_EXIF_INTEROP = 5 |
| 285 |
FIMD_IPTC = 6 |
| 286 |
FIMD_XMP = 7 |
| 287 |
FIMD_GEOTIFF = 8 |
| 288 |
FIMD_ANIMATION = 9 |
| 289 |
|
| 290 |
|
| 291 |
class METADATA_DATATYPE(object): |
| 292 |
FIDT_BYTE = 1 |
| 293 |
FIDT_ASCII = 2 |
| 294 |
FIDT_SHORT = 3 |
| 295 |
FIDT_LONG = 4 |
| 296 |
FIDT_RATIONAL = 5 |
| 297 |
FIDT_SBYTE = 6 |
| 298 |
FIDT_UNDEFINED = 7 |
| 299 |
FIDT_SSHORT = 8 |
| 300 |
FIDT_SLONG = 9 |
| 301 |
FIDT_SRATIONAL = 10 |
| 302 |
FIDT_FLOAT = 11 |
| 303 |
FIDT_DOUBLE = 12 |
| 304 |
FIDT_IFD = 13 |
| 305 |
FIDT_PALETTE = 14 |
| 306 |
FIDT_LONG8 = 16 |
| 307 |
FIDT_SLONG8 = 17 |
| 308 |
FIDT_IFD8 = 18 |
| 309 |
|
| 310 |
dtypes = { |
| 311 |
FIDT_BYTE: numpy.uint8, |
| 312 |
FIDT_SHORT: numpy.uint16, |
| 313 |
FIDT_LONG: numpy.uint32, |
| 314 |
FIDT_RATIONAL: [("numerator", numpy.uint32), ("denominator", numpy.uint32)], |
| 315 |
FIDT_LONG8: numpy.uint64, |
| 316 |
FIDT_SLONG8: numpy.int64, |
| 317 |
FIDT_IFD8: numpy.uint64, |
| 318 |
FIDT_SBYTE: numpy.int8, |
| 319 |
FIDT_UNDEFINED: numpy.uint8, |
| 320 |
FIDT_SSHORT: numpy.int16, |
| 321 |
FIDT_SLONG: numpy.int32, |
| 322 |
FIDT_SRATIONAL: [("numerator", numpy.int32), ("denominator", numpy.int32)], |
| 323 |
FIDT_FLOAT: numpy.float32, |
| 324 |
FIDT_DOUBLE: numpy.float64, |
| 325 |
FIDT_IFD: numpy.uint32, |
| 326 |
FIDT_PALETTE: [ |
| 327 |
("R", numpy.uint8), |
| 328 |
("G", numpy.uint8), |
| 329 |
("B", numpy.uint8), |
| 330 |
("A", numpy.uint8), |
| 331 |
], |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
class Freeimage(object): |
| 336 |
"""Class to represent an interface to the FreeImage library. |
| 337 |
This class is relatively thin. It provides a Pythonic API that converts |
| 338 |
Freeimage objects to Python objects, but that's about it. |
| 339 |
The actual implementation should be provided by the plugins. |
| 340 |
|
| 341 |
The recommended way to call into the Freeimage library (so that |
| 342 |
errors and warnings show up in the right moment) is to use this |
| 343 |
object as a context manager: |
| 344 |
with imageio.fi as lib: |
| 345 |
lib.FreeImage_GetPalette() |
| 346 |
|
| 347 |
""" |
| 348 |
|
| 349 |
_API = { |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
"FreeImage_AllocateT": (ctypes.c_void_p, None), |
| 363 |
"FreeImage_FindFirstMetadata": (ctypes.c_void_p, None), |
| 364 |
"FreeImage_GetBits": (ctypes.c_void_p, None), |
| 365 |
"FreeImage_GetPalette": (ctypes.c_void_p, None), |
| 366 |
"FreeImage_GetTagKey": (ctypes.c_char_p, None), |
| 367 |
"FreeImage_GetTagValue": (ctypes.c_void_p, None), |
| 368 |
"FreeImage_CreateTag": (ctypes.c_void_p, None), |
| 369 |
"FreeImage_Save": (ctypes.c_void_p, None), |
| 370 |
"FreeImage_Load": (ctypes.c_void_p, None), |
| 371 |
"FreeImage_LoadFromMemory": (ctypes.c_void_p, None), |
| 372 |
"FreeImage_OpenMultiBitmap": (ctypes.c_void_p, None), |
| 373 |
"FreeImage_LoadMultiBitmapFromMemory": (ctypes.c_void_p, None), |
| 374 |
"FreeImage_LockPage": (ctypes.c_void_p, None), |
| 375 |
"FreeImage_OpenMemory": (ctypes.c_void_p, None), |
| 376 |
|
| 377 |
|
| 378 |
"FreeImage_GetVersion": (ctypes.c_char_p, None), |
| 379 |
"FreeImage_GetFIFExtensionList": (ctypes.c_char_p, None), |
| 380 |
"FreeImage_GetFormatFromFIF": (ctypes.c_char_p, None), |
| 381 |
"FreeImage_GetFIFDescription": (ctypes.c_char_p, None), |
| 382 |
"FreeImage_ColorQuantizeEx": (ctypes.c_void_p, None), |
| 383 |
|
| 384 |
"FreeImage_IsLittleEndian": (ctypes.c_int, None), |
| 385 |
"FreeImage_SetOutputMessage": (ctypes.c_void_p, None), |
| 386 |
"FreeImage_GetFIFCount": (ctypes.c_int, None), |
| 387 |
"FreeImage_IsPluginEnabled": (ctypes.c_int, None), |
| 388 |
"FreeImage_GetFileType": (ctypes.c_int, None), |
| 389 |
|
| 390 |
"FreeImage_GetTagType": (ctypes.c_int, None), |
| 391 |
"FreeImage_GetTagLength": (ctypes.c_int, None), |
| 392 |
"FreeImage_FindNextMetadata": (ctypes.c_int, None), |
| 393 |
"FreeImage_FindCloseMetadata": (ctypes.c_void_p, None), |
| 394 |
|
| 395 |
"FreeImage_GetFIFFromFilename": (ctypes.c_int, None), |
| 396 |
"FreeImage_FIFSupportsReading": (ctypes.c_int, None), |
| 397 |
"FreeImage_FIFSupportsWriting": (ctypes.c_int, None), |
| 398 |
"FreeImage_FIFSupportsExportType": (ctypes.c_int, None), |
| 399 |
"FreeImage_FIFSupportsExportBPP": (ctypes.c_int, None), |
| 400 |
"FreeImage_GetHeight": (ctypes.c_int, None), |
| 401 |
"FreeImage_GetWidth": (ctypes.c_int, None), |
| 402 |
"FreeImage_GetImageType": (ctypes.c_int, None), |
| 403 |
"FreeImage_GetBPP": (ctypes.c_int, None), |
| 404 |
"FreeImage_GetColorsUsed": (ctypes.c_int, None), |
| 405 |
"FreeImage_ConvertTo32Bits": (ctypes.c_void_p, None), |
| 406 |
"FreeImage_GetPitch": (ctypes.c_int, None), |
| 407 |
"FreeImage_Unload": (ctypes.c_void_p, None), |
| 408 |
} |
| 409 |
|
| 410 |
def __init__(self): |
| 411 |
|
| 412 |
self._lib = None |
| 413 |
|
| 414 |
|
| 415 |
self._lock = threading.RLock() |
| 416 |
|
| 417 |
|
| 418 |
self._messages = [] |
| 419 |
|
| 420 |
|
| 421 |
if sys.platform.startswith("win"): |
| 422 |
functype = ctypes.WINFUNCTYPE |
| 423 |
else: |
| 424 |
functype = ctypes.CFUNCTYPE |
| 425 |
|
| 426 |
|
| 427 |
@functype(None, ctypes.c_int, ctypes.c_char_p) |
| 428 |
def error_handler(fif, message): |
| 429 |
message = message.decode("utf-8") |
| 430 |
self._messages.append(message) |
| 431 |
while (len(self._messages)) > 256: |
| 432 |
self._messages.pop(0) |
| 433 |
|
| 434 |
|
| 435 |
self._error_handler = error_handler |
| 436 |
|
| 437 |
@property |
| 438 |
def lib(self): |
| 439 |
if self._lib is None: |
| 440 |
try: |
| 441 |
self.load_freeimage() |
| 442 |
except OSError as err: |
| 443 |
self._lib = "The freeimage library could not be loaded: " |
| 444 |
self._lib += str(err) |
| 445 |
if isinstance(self._lib, str): |
| 446 |
raise RuntimeError(self._lib) |
| 447 |
return self._lib |
| 448 |
|
| 449 |
def has_lib(self): |
| 450 |
try: |
| 451 |
self.lib |
| 452 |
except Exception: |
| 453 |
return False |
| 454 |
return True |
| 455 |
|
| 456 |
def load_freeimage(self): |
| 457 |
"""Try to load the freeimage lib from the system. If not successful, |
| 458 |
try to download the imageio version and try again. |
| 459 |
""" |
| 460 |
|
| 461 |
success = False |
| 462 |
try: |
| 463 |
|
| 464 |
|
| 465 |
self._load_freeimage() |
| 466 |
self._register_api() |
| 467 |
if self.lib.FreeImage_GetVersion().decode("utf-8") >= "3.15": |
| 468 |
success = True |
| 469 |
except OSError: |
| 470 |
pass |
| 471 |
|
| 472 |
if not success: |
| 473 |
|
| 474 |
get_freeimage_lib() |
| 475 |
self._load_freeimage() |
| 476 |
self._register_api() |
| 477 |
|
| 478 |
|
| 479 |
self.lib.FreeImage_SetOutputMessage(self._error_handler) |
| 480 |
self.lib_version = self.lib.FreeImage_GetVersion().decode("utf-8") |
| 481 |
|
| 482 |
def _load_freeimage(self): |
| 483 |
|
| 484 |
lib_names = ["freeimage", "libfreeimage"] |
| 485 |
exact_lib_names = [ |
| 486 |
"FreeImage", |
| 487 |
"libfreeimage.dylib", |
| 488 |
"libfreeimage.so", |
| 489 |
"libfreeimage.so.3", |
| 490 |
] |
| 491 |
|
| 492 |
res_dirs = resource_dirs() |
| 493 |
plat = get_platform() |
| 494 |
if plat: |
| 495 |
fname = FNAME_PER_PLATFORM[plat] |
| 496 |
for dir in res_dirs: |
| 497 |
exact_lib_names.insert(0, os.path.join(dir, "freeimage", fname)) |
| 498 |
|
| 499 |
|
| 500 |
lib = os.getenv("IMAGEIO_FREEIMAGE_LIB", None) |
| 501 |
if lib is not None: |
| 502 |
exact_lib_names.insert(0, lib) |
| 503 |
|
| 504 |
|
| 505 |
try: |
| 506 |
lib, fname = load_lib(exact_lib_names, lib_names, res_dirs) |
| 507 |
except OSError as err: |
| 508 |
err_msg = str(err) + "\nPlease install the FreeImage library." |
| 509 |
raise OSError(err_msg) |
| 510 |
|
| 511 |
|
| 512 |
self._lib = lib |
| 513 |
self.lib_fname = fname |
| 514 |
|
| 515 |
def _register_api(self): |
| 516 |
|
| 517 |
for f, (restype, argtypes) in self._API.items(): |
| 518 |
func = getattr(self.lib, f) |
| 519 |
func.restype = restype |
| 520 |
func.argtypes = argtypes |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
def __enter__(self): |
| 525 |
self._lock.acquire() |
| 526 |
return self.lib |
| 527 |
|
| 528 |
def __exit__(self, *args): |
| 529 |
self._show_any_warnings() |
| 530 |
self._lock.release() |
| 531 |
|
| 532 |
def _reset_log(self): |
| 533 |
"""Reset the list of output messages. Call this before |
| 534 |
loading or saving an image with the FreeImage API. |
| 535 |
""" |
| 536 |
self._messages = [] |
| 537 |
|
| 538 |
def _get_error_message(self): |
| 539 |
"""Get the output messages produced since the last reset as |
| 540 |
one string. Returns 'No known reason.' if there are no messages. |
| 541 |
Also resets the log. |
| 542 |
""" |
| 543 |
if self._messages: |
| 544 |
res = " ".join(self._messages) |
| 545 |
self._reset_log() |
| 546 |
return res |
| 547 |
else: |
| 548 |
return "No known reason." |
| 549 |
|
| 550 |
def _show_any_warnings(self): |
| 551 |
"""If there were any messages since the last reset, show them |
| 552 |
as a warning. Otherwise do nothing. Also resets the messages. |
| 553 |
""" |
| 554 |
if self._messages: |
| 555 |
logger.warning("imageio.freeimage warning: " + self._get_error_message()) |
| 556 |
self._reset_log() |
| 557 |
|
| 558 |
def get_output_log(self): |
| 559 |
"""Return a list of the last 256 output messages |
| 560 |
(warnings and errors) produced by the FreeImage library. |
| 561 |
""" |
| 562 |
|
| 563 |
return [m for m in self._messages] |
| 564 |
|
| 565 |
def getFIF(self, filename, mode, bb=None): |
| 566 |
"""Get the freeimage Format (FIF) from a given filename. |
| 567 |
If mode is 'r', will try to determine the format by reading |
| 568 |
the file, otherwise only the filename is used. |
| 569 |
|
| 570 |
This function also tests whether the format supports reading/writing. |
| 571 |
""" |
| 572 |
with self as lib: |
| 573 |
|
| 574 |
ftype = -1 |
| 575 |
if mode not in "rw": |
| 576 |
raise ValueError('Invalid mode (must be "r" or "w").') |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
if mode == "r": |
| 582 |
if bb is not None: |
| 583 |
fimemory = lib.FreeImage_OpenMemory(ctypes.c_char_p(bb), len(bb)) |
| 584 |
ftype = lib.FreeImage_GetFileTypeFromMemory( |
| 585 |
ctypes.c_void_p(fimemory), len(bb) |
| 586 |
) |
| 587 |
lib.FreeImage_CloseMemory(ctypes.c_void_p(fimemory)) |
| 588 |
if (ftype == -1) and os.path.isfile(filename): |
| 589 |
ftype = lib.FreeImage_GetFileType(efn(filename), 0) |
| 590 |
|
| 591 |
if ftype == -1: |
| 592 |
ftype = lib.FreeImage_GetFIFFromFilename(efn(filename)) |
| 593 |
|
| 594 |
|
| 595 |
if ftype == -1: |
| 596 |
raise ValueError('Cannot determine format of file "%s"' % filename) |
| 597 |
elif mode == "w" and not lib.FreeImage_FIFSupportsWriting(ftype): |
| 598 |
raise ValueError('Cannot write the format of file "%s"' % filename) |
| 599 |
elif mode == "r" and not lib.FreeImage_FIFSupportsReading(ftype): |
| 600 |
raise ValueError('Cannot read the format of file "%s"' % filename) |
| 601 |
return ftype |
| 602 |
|
| 603 |
def create_bitmap(self, filename, ftype, flags=0): |
| 604 |
"""create_bitmap(filename, ftype, flags=0) |
| 605 |
Create a wrapped bitmap object. |
| 606 |
""" |
| 607 |
return FIBitmap(self, filename, ftype, flags) |
| 608 |
|
| 609 |
def create_multipage_bitmap(self, filename, ftype, flags=0): |
| 610 |
"""create_multipage_bitmap(filename, ftype, flags=0) |
| 611 |
Create a wrapped multipage bitmap object. |
| 612 |
""" |
| 613 |
return FIMultipageBitmap(self, filename, ftype, flags) |
| 614 |
|
| 615 |
|
| 616 |
class FIBaseBitmap(object): |
| 617 |
def __init__(self, fi, filename, ftype, flags): |
| 618 |
self._fi = fi |
| 619 |
self._filename = filename |
| 620 |
self._ftype = ftype |
| 621 |
self._flags = flags |
| 622 |
self._bitmap = None |
| 623 |
self._close_funcs = [] |
| 624 |
|
| 625 |
def __del__(self): |
| 626 |
self.close() |
| 627 |
|
| 628 |
def close(self): |
| 629 |
if (self._bitmap is not None) and self._close_funcs: |
| 630 |
for close_func in self._close_funcs: |
| 631 |
try: |
| 632 |
with self._fi: |
| 633 |
fun = close_func[0] |
| 634 |
fun(*close_func[1:]) |
| 635 |
except Exception: |
| 636 |
pass |
| 637 |
self._close_funcs = [] |
| 638 |
self._bitmap = None |
| 639 |
|
| 640 |
def _set_bitmap(self, bitmap, close_func=None): |
| 641 |
"""Function to set the bitmap and specify the function to unload it.""" |
| 642 |
if self._bitmap is not None: |
| 643 |
pass |
| 644 |
if close_func is None: |
| 645 |
close_func = self._fi.lib.FreeImage_Unload, bitmap |
| 646 |
|
| 647 |
self._bitmap = bitmap |
| 648 |
if close_func: |
| 649 |
self._close_funcs.append(close_func) |
| 650 |
|
| 651 |
def get_meta_data(self): |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
models = [ |
| 657 |
(name[5:], number) |
| 658 |
for name, number in METADATA_MODELS.__dict__.items() |
| 659 |
if name.startswith("FIMD_") |
| 660 |
] |
| 661 |
|
| 662 |
|
| 663 |
metadata = Dict() |
| 664 |
tag = ctypes.c_void_p() |
| 665 |
|
| 666 |
with self._fi as lib: |
| 667 |
|
| 668 |
for model_name, number in models: |
| 669 |
|
| 670 |
mdhandle = lib.FreeImage_FindFirstMetadata( |
| 671 |
number, self._bitmap, ctypes.byref(tag) |
| 672 |
) |
| 673 |
mdhandle = ctypes.c_void_p(mdhandle) |
| 674 |
if mdhandle: |
| 675 |
|
| 676 |
more = True |
| 677 |
while more: |
| 678 |
|
| 679 |
tag_name = lib.FreeImage_GetTagKey(tag).decode("utf-8") |
| 680 |
tag_type = lib.FreeImage_GetTagType(tag) |
| 681 |
byte_size = lib.FreeImage_GetTagLength(tag) |
| 682 |
char_ptr = ctypes.c_char * byte_size |
| 683 |
data = char_ptr.from_address(lib.FreeImage_GetTagValue(tag)) |
| 684 |
|
| 685 |
tag_bytes = bytes(bytearray(data)) |
| 686 |
|
| 687 |
tag_val = tag_bytes |
| 688 |
|
| 689 |
if tag_type == METADATA_DATATYPE.FIDT_ASCII: |
| 690 |
tag_val = tag_bytes.decode("utf-8", "replace") |
| 691 |
elif tag_type in METADATA_DATATYPE.dtypes: |
| 692 |
dtype = METADATA_DATATYPE.dtypes[tag_type] |
| 693 |
if IS_PYPY and isinstance(dtype, (list, tuple)): |
| 694 |
pass |
| 695 |
else: |
| 696 |
try: |
| 697 |
tag_val = numpy.frombuffer( |
| 698 |
tag_bytes, dtype=dtype |
| 699 |
).copy() |
| 700 |
if len(tag_val) == 1: |
| 701 |
tag_val = tag_val[0] |
| 702 |
except Exception: |
| 703 |
pass |
| 704 |
|
| 705 |
subdict = metadata.setdefault(model_name, Dict()) |
| 706 |
subdict[tag_name] = tag_val |
| 707 |
|
| 708 |
more = lib.FreeImage_FindNextMetadata( |
| 709 |
mdhandle, ctypes.byref(tag) |
| 710 |
) |
| 711 |
|
| 712 |
|
| 713 |
lib.FreeImage_FindCloseMetadata(mdhandle) |
| 714 |
|
| 715 |
|
| 716 |
return metadata |
| 717 |
|
| 718 |
def set_meta_data(self, metadata): |
| 719 |
|
| 720 |
models = {} |
| 721 |
for name, number in METADATA_MODELS.__dict__.items(): |
| 722 |
if name.startswith("FIMD_"): |
| 723 |
models[name[5:]] = number |
| 724 |
|
| 725 |
|
| 726 |
def get_tag_type_number(dtype): |
| 727 |
for number, numpy_dtype in METADATA_DATATYPE.dtypes.items(): |
| 728 |
if dtype == numpy_dtype: |
| 729 |
return number |
| 730 |
else: |
| 731 |
return None |
| 732 |
|
| 733 |
with self._fi as lib: |
| 734 |
for model_name, subdict in metadata.items(): |
| 735 |
|
| 736 |
number = models.get(model_name, None) |
| 737 |
if number is None: |
| 738 |
continue |
| 739 |
|
| 740 |
for tag_name, tag_val in subdict.items(): |
| 741 |
|
| 742 |
tag = lib.FreeImage_CreateTag() |
| 743 |
tag = ctypes.c_void_p(tag) |
| 744 |
|
| 745 |
try: |
| 746 |
|
| 747 |
is_ascii = False |
| 748 |
if isinstance(tag_val, str): |
| 749 |
try: |
| 750 |
tag_bytes = tag_val.encode("ascii") |
| 751 |
is_ascii = True |
| 752 |
except UnicodeError: |
| 753 |
pass |
| 754 |
if is_ascii: |
| 755 |
tag_type = METADATA_DATATYPE.FIDT_ASCII |
| 756 |
tag_count = len(tag_bytes) |
| 757 |
else: |
| 758 |
if not hasattr(tag_val, "dtype"): |
| 759 |
tag_val = numpy.array([tag_val]) |
| 760 |
tag_type = get_tag_type_number(tag_val.dtype) |
| 761 |
if tag_type is None: |
| 762 |
logger.warning( |
| 763 |
"imageio.freeimage warning: Could not " |
| 764 |
"determine tag type of %r." % tag_name |
| 765 |
) |
| 766 |
continue |
| 767 |
tag_bytes = tag_val.tobytes() |
| 768 |
tag_count = tag_val.size |
| 769 |
|
| 770 |
lib.FreeImage_SetTagKey(tag, tag_name.encode("utf-8")) |
| 771 |
lib.FreeImage_SetTagType(tag, tag_type) |
| 772 |
lib.FreeImage_SetTagCount(tag, tag_count) |
| 773 |
lib.FreeImage_SetTagLength(tag, len(tag_bytes)) |
| 774 |
lib.FreeImage_SetTagValue(tag, tag_bytes) |
| 775 |
|
| 776 |
tag_key = lib.FreeImage_GetTagKey(tag) |
| 777 |
lib.FreeImage_SetMetadata(number, self._bitmap, tag_key, tag) |
| 778 |
|
| 779 |
except Exception as err: |
| 780 |
logger.warning( |
| 781 |
"imagio.freeimage warning: Could not set tag " |
| 782 |
"%r: %s, %s" |
| 783 |
% (tag_name, self._fi._get_error_message(), str(err)) |
| 784 |
) |
| 785 |
finally: |
| 786 |
lib.FreeImage_DeleteTag(tag) |
| 787 |
|
| 788 |
|
| 789 |
class FIBitmap(FIBaseBitmap): |
| 790 |
"""Wrapper for the FI bitmap object.""" |
| 791 |
|
| 792 |
def allocate(self, array): |
| 793 |
|
| 794 |
assert isinstance(array, numpy.ndarray) |
| 795 |
shape = array.shape |
| 796 |
dtype = array.dtype |
| 797 |
|
| 798 |
|
| 799 |
r, c = shape[:2] |
| 800 |
if len(shape) == 2: |
| 801 |
n_channels = 1 |
| 802 |
elif len(shape) == 3: |
| 803 |
n_channels = shape[2] |
| 804 |
else: |
| 805 |
n_channels = shape[0] |
| 806 |
|
| 807 |
|
| 808 |
try: |
| 809 |
fi_type = FI_TYPES.fi_types[(dtype.type, n_channels)] |
| 810 |
self._fi_type = fi_type |
| 811 |
except KeyError: |
| 812 |
raise ValueError("Cannot write arrays of given type and shape.") |
| 813 |
|
| 814 |
|
| 815 |
with self._fi as lib: |
| 816 |
bpp = 8 * dtype.itemsize * n_channels |
| 817 |
bitmap = lib.FreeImage_AllocateT(fi_type, c, r, bpp, 0, 0, 0) |
| 818 |
bitmap = ctypes.c_void_p(bitmap) |
| 819 |
|
| 820 |
|
| 821 |
if not bitmap: |
| 822 |
raise RuntimeError( |
| 823 |
"Could not allocate bitmap for storage: %s" |
| 824 |
% self._fi._get_error_message() |
| 825 |
) |
| 826 |
self._set_bitmap(bitmap, (lib.FreeImage_Unload, bitmap)) |
| 827 |
|
| 828 |
def load_from_filename(self, filename=None): |
| 829 |
if filename is None: |
| 830 |
filename = self._filename |
| 831 |
|
| 832 |
with self._fi as lib: |
| 833 |
|
| 834 |
bitmap = lib.FreeImage_Load(self._ftype, efn(filename), self._flags) |
| 835 |
bitmap = ctypes.c_void_p(bitmap) |
| 836 |
|
| 837 |
|
| 838 |
if not bitmap: |
| 839 |
raise ValueError( |
| 840 |
'Could not load bitmap "%s": %s' |
| 841 |
% (self._filename, self._fi._get_error_message()) |
| 842 |
) |
| 843 |
self._set_bitmap(bitmap, (lib.FreeImage_Unload, bitmap)) |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
def save_to_filename(self, filename=None): |
| 863 |
if filename is None: |
| 864 |
filename = self._filename |
| 865 |
|
| 866 |
ftype = self._ftype |
| 867 |
bitmap = self._bitmap |
| 868 |
fi_type = self._fi_type |
| 869 |
|
| 870 |
with self._fi as lib: |
| 871 |
|
| 872 |
if fi_type == FI_TYPES.FIT_BITMAP: |
| 873 |
can_write = lib.FreeImage_FIFSupportsExportBPP( |
| 874 |
ftype, lib.FreeImage_GetBPP(bitmap) |
| 875 |
) |
| 876 |
else: |
| 877 |
can_write = lib.FreeImage_FIFSupportsExportType(ftype, fi_type) |
| 878 |
if not can_write: |
| 879 |
raise TypeError("Cannot save image of this format to this file type") |
| 880 |
|
| 881 |
|
| 882 |
res = lib.FreeImage_Save(ftype, bitmap, efn(filename), self._flags) |
| 883 |
|
| 884 |
if res is None: |
| 885 |
raise RuntimeError( |
| 886 |
f"Could not save file `{self._filename}`: {self._fi._get_error_message()}" |
| 887 |
) |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
def get_image_data(self): |
| 927 |
dtype, shape, bpp = self._get_type_and_shape() |
| 928 |
array = self._wrap_bitmap_bits_in_array(shape, dtype, False) |
| 929 |
with self._fi as lib: |
| 930 |
isle = lib.FreeImage_IsLittleEndian() |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
def n(arr): |
| 936 |
|
| 937 |
if arr.ndim == 1: |
| 938 |
return arr[::-1].T |
| 939 |
elif arr.ndim == 2: |
| 940 |
return arr[:, ::-1].T |
| 941 |
elif arr.ndim == 3: |
| 942 |
return arr[:, :, ::-1].T |
| 943 |
elif arr.ndim == 4: |
| 944 |
return arr[:, :, :, ::-1].T |
| 945 |
|
| 946 |
if len(shape) == 3 and isle and dtype.type == numpy.uint8: |
| 947 |
b = n(array[0]) |
| 948 |
g = n(array[1]) |
| 949 |
r = n(array[2]) |
| 950 |
if shape[0] == 3: |
| 951 |
return numpy.dstack((r, g, b)) |
| 952 |
elif shape[0] == 4: |
| 953 |
a = n(array[3]) |
| 954 |
return numpy.dstack((r, g, b, a)) |
| 955 |
else: |
| 956 |
raise ValueError("Cannot handle images of shape %s" % shape) |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
a = n(array).copy() |
| 961 |
return a |
| 962 |
|
| 963 |
def set_image_data(self, array): |
| 964 |
|
| 965 |
assert isinstance(array, numpy.ndarray) |
| 966 |
shape = array.shape |
| 967 |
dtype = array.dtype |
| 968 |
with self._fi as lib: |
| 969 |
isle = lib.FreeImage_IsLittleEndian() |
| 970 |
|
| 971 |
|
| 972 |
r, c = shape[:2] |
| 973 |
if len(shape) == 2: |
| 974 |
n_channels = 1 |
| 975 |
w_shape = (c, r) |
| 976 |
elif len(shape) == 3: |
| 977 |
n_channels = shape[2] |
| 978 |
w_shape = (n_channels, c, r) |
| 979 |
else: |
| 980 |
n_channels = shape[0] |
| 981 |
|
| 982 |
def n(arr): |
| 983 |
return arr[::-1].T |
| 984 |
|
| 985 |
wrapped_array = self._wrap_bitmap_bits_in_array(w_shape, dtype, True) |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
if len(shape) == 3 and isle and dtype.type == numpy.uint8: |
| 992 |
R = array[:, :, 0] |
| 993 |
G = array[:, :, 1] |
| 994 |
B = array[:, :, 2] |
| 995 |
wrapped_array[0] = n(B) |
| 996 |
wrapped_array[1] = n(G) |
| 997 |
wrapped_array[2] = n(R) |
| 998 |
if shape[2] == 4: |
| 999 |
A = array[:, :, 3] |
| 1000 |
wrapped_array[3] = n(A) |
| 1001 |
else: |
| 1002 |
wrapped_array[:] = n(array) |
| 1003 |
if self._need_finish: |
| 1004 |
self._finish_wrapped_array(wrapped_array) |
| 1005 |
|
| 1006 |
if len(shape) == 2 and dtype.type == numpy.uint8: |
| 1007 |
with self._fi as lib: |
| 1008 |
palette = lib.FreeImage_GetPalette(self._bitmap) |
| 1009 |
palette = ctypes.c_void_p(palette) |
| 1010 |
if not palette: |
| 1011 |
raise RuntimeError("Could not get image palette") |
| 1012 |
try: |
| 1013 |
palette_data = GREY_PALETTE.ctypes.data |
| 1014 |
except Exception: |
| 1015 |
palette_data = GREY_PALETTE.__array_interface__["data"][0] |
| 1016 |
ctypes.memmove(palette, palette_data, 1024) |
| 1017 |
|
| 1018 |
def _wrap_bitmap_bits_in_array(self, shape, dtype, save): |
| 1019 |
"""Return an ndarray view on the data in a FreeImage bitmap. Only |
| 1020 |
valid for as long as the bitmap is loaded (if single page) / locked |
| 1021 |
in memory (if multipage). This is used in loading data, but |
| 1022 |
also during saving, to prepare a strided numpy array buffer. |
| 1023 |
|
| 1024 |
""" |
| 1025 |
|
| 1026 |
with self._fi as lib: |
| 1027 |
pitch = lib.FreeImage_GetPitch(self._bitmap) |
| 1028 |
bits = lib.FreeImage_GetBits(self._bitmap) |
| 1029 |
|
| 1030 |
|
| 1031 |
height = shape[-1] |
| 1032 |
byte_size = height * pitch |
| 1033 |
itemsize = dtype.itemsize |
| 1034 |
|
| 1035 |
|
| 1036 |
if len(shape) == 3: |
| 1037 |
strides = (itemsize, shape[0] * itemsize, pitch) |
| 1038 |
else: |
| 1039 |
strides = (itemsize, pitch) |
| 1040 |
|
| 1041 |
|
| 1042 |
data = (ctypes.c_char * byte_size).from_address(bits) |
| 1043 |
try: |
| 1044 |
self._need_finish = False |
| 1045 |
if TEST_NUMPY_NO_STRIDES: |
| 1046 |
raise NotImplementedError() |
| 1047 |
return numpy.ndarray(shape, dtype=dtype, buffer=data, strides=strides) |
| 1048 |
except NotImplementedError: |
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
if save: |
| 1053 |
self._need_finish = True |
| 1054 |
return numpy.zeros(shape, dtype=dtype) |
| 1055 |
else: |
| 1056 |
bb = bytes(bytearray(data)) |
| 1057 |
array = numpy.frombuffer(bb, dtype=dtype).copy() |
| 1058 |
|
| 1059 |
if len(shape) == 3: |
| 1060 |
array = array.reshape(shape[2], strides[-1] // shape[0], shape[0]) |
| 1061 |
array2 = array[: shape[2], : shape[1], : shape[0]] |
| 1062 |
array = numpy.zeros(shape, dtype=array.dtype) |
| 1063 |
for i in range(shape[0]): |
| 1064 |
array[i] = array2[:, :, i].T |
| 1065 |
else: |
| 1066 |
array = array.reshape(shape[1], strides[-1]) |
| 1067 |
array = array[: shape[1], : shape[0]].T |
| 1068 |
return array |
| 1069 |
|
| 1070 |
def _finish_wrapped_array(self, array): |
| 1071 |
"""Hardcore way to inject numpy array in bitmap.""" |
| 1072 |
|
| 1073 |
with self._fi as lib: |
| 1074 |
pitch = lib.FreeImage_GetPitch(self._bitmap) |
| 1075 |
bits = lib.FreeImage_GetBits(self._bitmap) |
| 1076 |
bpp = lib.FreeImage_GetBPP(self._bitmap) |
| 1077 |
|
| 1078 |
nchannels = bpp // 8 // array.itemsize |
| 1079 |
realwidth = pitch // nchannels |
| 1080 |
|
| 1081 |
extra = realwidth - array.shape[-2] |
| 1082 |
assert 0 <= extra < 10 |
| 1083 |
|
| 1084 |
newshape = array.shape[-1], realwidth, nchannels |
| 1085 |
array2 = numpy.zeros(newshape, array.dtype) |
| 1086 |
if nchannels == 1: |
| 1087 |
array2[:, : array.shape[-2], 0] = array.T |
| 1088 |
else: |
| 1089 |
for i in range(nchannels): |
| 1090 |
array2[:, : array.shape[-2], i] = array[i, :, :].T |
| 1091 |
|
| 1092 |
data_ptr = array2.__array_interface__["data"][0] |
| 1093 |
ctypes.memmove(bits, data_ptr, array2.nbytes) |
| 1094 |
del array2 |
| 1095 |
|
| 1096 |
def _get_type_and_shape(self): |
| 1097 |
bitmap = self._bitmap |
| 1098 |
|
| 1099 |
|
| 1100 |
with self._fi as lib: |
| 1101 |
w = lib.FreeImage_GetWidth(bitmap) |
| 1102 |
h = lib.FreeImage_GetHeight(bitmap) |
| 1103 |
self._fi_type = fi_type = lib.FreeImage_GetImageType(bitmap) |
| 1104 |
if not fi_type: |
| 1105 |
raise ValueError("Unknown image pixel type") |
| 1106 |
|
| 1107 |
|
| 1108 |
bpp = None |
| 1109 |
dtype = FI_TYPES.dtypes[fi_type] |
| 1110 |
|
| 1111 |
if fi_type == FI_TYPES.FIT_BITMAP: |
| 1112 |
with self._fi as lib: |
| 1113 |
bpp = lib.FreeImage_GetBPP(bitmap) |
| 1114 |
has_pallette = lib.FreeImage_GetColorsUsed(bitmap) |
| 1115 |
if has_pallette: |
| 1116 |
|
| 1117 |
if has_pallette == 256: |
| 1118 |
palette = lib.FreeImage_GetPalette(bitmap) |
| 1119 |
palette = ctypes.c_void_p(palette) |
| 1120 |
p = (ctypes.c_uint8 * (256 * 4)).from_address(palette.value) |
| 1121 |
p = numpy.frombuffer(p, numpy.uint32).copy() |
| 1122 |
if (GREY_PALETTE == p).all(): |
| 1123 |
extra_dims = [] |
| 1124 |
return numpy.dtype(dtype), extra_dims + [w, h], bpp |
| 1125 |
|
| 1126 |
newbitmap = lib.FreeImage_ConvertTo32Bits(bitmap) |
| 1127 |
newbitmap = ctypes.c_void_p(newbitmap) |
| 1128 |
self._set_bitmap(newbitmap) |
| 1129 |
return self._get_type_and_shape() |
| 1130 |
elif bpp == 8: |
| 1131 |
extra_dims = [] |
| 1132 |
elif bpp == 24: |
| 1133 |
extra_dims = [3] |
| 1134 |
elif bpp == 32: |
| 1135 |
extra_dims = [4] |
| 1136 |
else: |
| 1137 |
|
| 1138 |
|
| 1139 |
newbitmap = lib.FreeImage_ConvertTo32Bits(bitmap) |
| 1140 |
newbitmap = ctypes.c_void_p(newbitmap) |
| 1141 |
self._set_bitmap(newbitmap) |
| 1142 |
return self._get_type_and_shape() |
| 1143 |
else: |
| 1144 |
extra_dims = FI_TYPES.extra_dims[fi_type] |
| 1145 |
|
| 1146 |
|
| 1147 |
return numpy.dtype(dtype), extra_dims + [w, h], bpp |
| 1148 |
|
| 1149 |
def quantize(self, quantizer=0, palettesize=256): |
| 1150 |
"""Quantize the bitmap to make it 8-bit (paletted). Returns a new |
| 1151 |
FIBitmap object. |
| 1152 |
Only for 24 bit images. |
| 1153 |
""" |
| 1154 |
with self._fi as lib: |
| 1155 |
|
| 1156 |
bitmap = lib.FreeImage_ColorQuantizeEx( |
| 1157 |
self._bitmap, quantizer, palettesize, 0, None |
| 1158 |
) |
| 1159 |
bitmap = ctypes.c_void_p(bitmap) |
| 1160 |
|
| 1161 |
|
| 1162 |
if not bitmap: |
| 1163 |
raise ValueError( |
| 1164 |
'Could not quantize bitmap "%s": %s' |
| 1165 |
% (self._filename, self._fi._get_error_message()) |
| 1166 |
) |
| 1167 |
|
| 1168 |
new = FIBitmap(self._fi, self._filename, self._ftype, self._flags) |
| 1169 |
new._set_bitmap(bitmap, (lib.FreeImage_Unload, bitmap)) |
| 1170 |
new._fi_type = self._fi_type |
| 1171 |
return new |
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
class FIMultipageBitmap(FIBaseBitmap): |
| 1196 |
"""Wrapper for the multipage FI bitmap object.""" |
| 1197 |
|
| 1198 |
def load_from_filename(self, filename=None): |
| 1199 |
if filename is None: |
| 1200 |
filename = self._filename |
| 1201 |
|
| 1202 |
|
| 1203 |
create_new = False |
| 1204 |
read_only = True |
| 1205 |
keep_cache_in_memory = False |
| 1206 |
|
| 1207 |
|
| 1208 |
with self._fi as lib: |
| 1209 |
|
| 1210 |
multibitmap = lib.FreeImage_OpenMultiBitmap( |
| 1211 |
self._ftype, |
| 1212 |
efn(filename), |
| 1213 |
create_new, |
| 1214 |
read_only, |
| 1215 |
keep_cache_in_memory, |
| 1216 |
self._flags, |
| 1217 |
) |
| 1218 |
multibitmap = ctypes.c_void_p(multibitmap) |
| 1219 |
|
| 1220 |
|
| 1221 |
if not multibitmap: |
| 1222 |
err = self._fi._get_error_message() |
| 1223 |
raise ValueError( |
| 1224 |
'Could not open file "%s" as multi-image: %s' |
| 1225 |
% (self._filename, err) |
| 1226 |
) |
| 1227 |
self._set_bitmap(multibitmap, (lib.FreeImage_CloseMultiBitmap, multibitmap)) |
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
def save_to_filename(self, filename=None): |
| 1249 |
if filename is None: |
| 1250 |
filename = self._filename |
| 1251 |
|
| 1252 |
|
| 1253 |
create_new = True |
| 1254 |
read_only = False |
| 1255 |
keep_cache_in_memory = False |
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
with self._fi as lib: |
| 1260 |
multibitmap = lib.FreeImage_OpenMultiBitmap( |
| 1261 |
self._ftype, |
| 1262 |
efn(filename), |
| 1263 |
create_new, |
| 1264 |
read_only, |
| 1265 |
keep_cache_in_memory, |
| 1266 |
0, |
| 1267 |
) |
| 1268 |
multibitmap = ctypes.c_void_p(multibitmap) |
| 1269 |
|
| 1270 |
|
| 1271 |
if not multibitmap: |
| 1272 |
msg = 'Could not open file "%s" for writing multi-image: %s' % ( |
| 1273 |
self._filename, |
| 1274 |
self._fi._get_error_message(), |
| 1275 |
) |
| 1276 |
raise ValueError(msg) |
| 1277 |
self._set_bitmap(multibitmap, (lib.FreeImage_CloseMultiBitmap, multibitmap)) |
| 1278 |
|
| 1279 |
def __len__(self): |
| 1280 |
with self._fi as lib: |
| 1281 |
return lib.FreeImage_GetPageCount(self._bitmap) |
| 1282 |
|
| 1283 |
def get_page(self, index): |
| 1284 |
"""Return the sub-bitmap for the given page index. |
| 1285 |
Please close the returned bitmap when done. |
| 1286 |
""" |
| 1287 |
with self._fi as lib: |
| 1288 |
|
| 1289 |
bitmap = lib.FreeImage_LockPage(self._bitmap, index) |
| 1290 |
bitmap = ctypes.c_void_p(bitmap) |
| 1291 |
if not bitmap: |
| 1292 |
raise ValueError( |
| 1293 |
"Could not open sub-image %i in %r: %s" |
| 1294 |
% (index, self._filename, self._fi._get_error_message()) |
| 1295 |
) |
| 1296 |
|
| 1297 |
|
| 1298 |
bm = FIBitmap(self._fi, self._filename, self._ftype, self._flags) |
| 1299 |
bm._set_bitmap( |
| 1300 |
bitmap, (lib.FreeImage_UnlockPage, self._bitmap, bitmap, False) |
| 1301 |
) |
| 1302 |
return bm |
| 1303 |
|
| 1304 |
def append_bitmap(self, bitmap): |
| 1305 |
"""Add a sub-bitmap to the multi-page bitmap.""" |
| 1306 |
with self._fi as lib: |
| 1307 |
|
| 1308 |
lib.FreeImage_AppendPage(self._bitmap, bitmap._bitmap) |
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
fi = Freeimage() |