mcidas improvement draft

This commit is contained in:
Deekshu Kare 2024-06-21 16:10:03 +02:00
parent 90c0661e8d
commit 27390e0451

View File

@ -1,79 +1,92 @@
from __future__ import annotations from __future__ import annotations
import pytest
from PIL import Image, McIdasImagePlugin
from .helper import assert_image_equal_tofile
from io import BytesIO
import struct import struct
import pytest
def create_mock_mcidas_file(w11_value: int) -> BytesIO: from PIL import Image, McIdasImagePlugin
"""
Creates a mock McIdas file with a given w[11] value. from .helper import assert_image_equal_tofile
"""
header = struct.pack(
"!64i", def create_mock_file(filename: str, w11_value: int, width: int, height: int) -> None:
0, 0, 0, 0, 0, 0, 0, 4, # Prefix (first 8 bytes) """
0, 400, 1800, # Width, Height (w[9], w[10]) Create a mock McIdas area file with specified w[11] value, width, and height.
w11_value, # Mode (w[11]) """
0, 0, 0, # w[12] to w[14] area_descriptor = [0] * 64
16, # w[15] (assuming some stride value) area_descriptor[10] = width
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # w[16] to w[25] area_descriptor[9] = height
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # w[26] to w[35] area_descriptor[11] = w11_value
256, # w[34] (assuming some offset) area_descriptor[34] = 256 # arbitrary valid offset
) area_descriptor[15] = 0 # stride (arbitrary valid value)
# Ensure header length is 256 bytes header = b"\x00" * 8 + struct.pack("!64i", *area_descriptor)
header = b"\x00" * (256 - len(header)) + header
with open(filename, "wb") as f:
f.write(header)
# Write image data (size based on width, height, and mode)
if w11_value == 1:
f.write(b"\x00" * (width * height)) # 8-bit data for mode L
elif w11_value == 2:
f.write(b"\x00" * (2 * width * height)) # 16-bit data for mode I;16B
elif w11_value == 4:
f.write(b"\x00" * (4 * width * height)) # 32-bit data for mode I;32B
# Create a valid file
return BytesIO(header)
def test_invalid_file() -> None: def test_invalid_file() -> None:
invalid_file = "Tests/images/flower.jpg" invalid_file = "Tests/images/flower.jpg"
with pytest.raises(SyntaxError): with pytest.raises(SyntaxError):
McIdasImagePlugin.McIdasImageFile(invalid_file) McIdasImagePlugin.McIdasImageFile(invalid_file)
def test_valid_file() -> None: def test_valid_file() -> None:
# Arrange # Arrange
# https://ghrc.nsstc.nasa.gov/hydro/details/cmx3g8
# https://ghrc.nsstc.nasa.gov/pub/fieldCampaigns/camex3/cmx3g8/browse/
test_file = "Tests/images/cmx3g8_wv_1998.260_0745_mcidas.ara" test_file = "Tests/images/cmx3g8_wv_1998.260_0745_mcidas.ara"
saved_file = "Tests/images/cmx3g8_wv_1998.260_0745_mcidas.tiff" saved_file = "Tests/images/cmx3g8_wv_1998.260_0745_mcidas.tiff"
# Act # Act
with Image.open(test_file) as im: with Image.open(test_file) as im:
im.load() im.load()
# Assert # Assert
assert im.format == "MCIDAS" assert im.format == "MCIDAS"
assert im.mode == "I" assert im.mode == "I"
assert im.size == (1800, 400) assert im.size == (1800, 400)
assert_image_equal_tofile(im, saved_file) assert_image_equal_tofile(im, saved_file)
def test_8bit_file() -> None:
# Arrange
test_file = create_mock_mcidas_file(1)
# Act def test_mode_L() -> None:
test_file = "Tests/images/mcidas_mode_L.ara"
# Create mock file for mode L
create_mock_file(test_file, w11_value=1, width=100, height=100)
with Image.open(test_file) as im: with Image.open(test_file) as im:
im.load() im.load()
# Assert
assert im.format == "MCIDAS" assert im.format == "MCIDAS"
assert im.mode == "L" assert im.mode == "L"
assert im.size == (1800, 400) assert im.size == (100, 100)
def test_32bit_file() -> None:
# Arrange
test_file = create_mock_mcidas_file(4)
# Act def test_mode_I_32B() -> None:
test_file = "Tests/images/mcidas_mode_I_32B.ara"
# Create mock file for mode I;32B
create_mock_file(test_file, w11_value=4, width=100, height=100)
with Image.open(test_file) as im: with Image.open(test_file) as im:
im.load() im.load()
# Assert
assert im.format == "MCIDAS" assert im.format == "MCIDAS"
assert im.mode == "I" assert im.mode == "I"
assert im.size == (1800, 400) assert im.size == (100, 100)
def test_unsupported_file() -> None:
# Arrange
test_file = create_mock_mcidas_file(3)
# Act & Assert def test_unsupported_format() -> None:
test_file = "Tests/images/mcidas_unsupported.ara"
# Create mock file for unsupported format
create_mock_file(test_file, w11_value=3, width=100, height=100)
with pytest.raises(SyntaxError, match="unsupported McIdas format"): with pytest.raises(SyntaxError, match="unsupported McIdas format"):
Image.open(test_file) McIdasImagePlugin.McIdasImageFile(test_file)