Skip to content

Commit

Permalink
Merge pull request #10 from fnands/update_mojo_24.5
Browse files Browse the repository at this point in the history
Update mojo 24.5
  • Loading branch information
fnands authored Oct 2, 2024
2 parents 068752d + 624f402 commit 6344001
Show file tree
Hide file tree
Showing 8 changed files with 662 additions and 496 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## Unreleased

# 0.1.3

### Changed
- Migrated to Mojo 24.5

# 0.1.2

### Changed
Expand Down
1,114 changes: 635 additions & 479 deletions magic.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion mimage/PngImagePlugin.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ struct PNGImage(Copyable, Movable):
determine_file_type(self.raw_data) == "PNG",
"File is not a PNG. Only PNGs are supported",
)

var read_head = 8

var header_chunk = parse_next_chunk(self.raw_data, read_head)
Expand Down
1 change: 1 addition & 0 deletions mimage/__init__.mojo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from tensor import Tensor
from pathlib import Path
from mimage.PngImagePlugin import PNGImage
from collections import Dict


# PNGImage opens these cases as well, should I just do one convert here?
Expand Down
4 changes: 2 additions & 2 deletions mimage/utils/binary.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ fn bytes_to_uint32_be(owned list: List[UInt8]) raises -> List[UInt32]:
# This avoids copying and makes sure only one List owns a pointer to the underlying address.
var ptr_to_uint8 = list.steal_data()
var ptr_to_uint32 = ptr_to_uint8.bitcast[UInt32]()
var dtype_ptr = DTypePointer[DType.uint32](Pointer[UInt32](ptr_to_uint32.address))
var dtype_ptr = UnsafePointer[Scalar[DType.uint32]](ptr_to_uint32.address)

# vectorize byte_swap over DTypePointer
# vectorize byte_swap over UnsafePointer
@parameter
fn _byte_swap[_width: Int](i: Int):
# call byte_swap on a batch of UInt32 values
Expand Down
21 changes: 11 additions & 10 deletions mimage/utils/compression.mojo
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from sys import ffi
from memory import memset_zero


alias Bytef = Scalar[DType.uint8]
alias uLong = UInt64
alias zlib_type = fn (
_out: Pointer[Bytef],
_out_len: Pointer[UInt64],
_in: Pointer[Bytef],
_out: UnsafePointer[Bytef],
_out_len: UnsafePointer[UInt64],
_in: UnsafePointer[Bytef],
_in_len: uLong,
) -> Int


fn _log_zlib_result(Z_RES: Int, compressing: Bool = True) raises -> NoneType:
fn _log_zlib_result(Z_RES: Int, compressing: Bool = True) raises -> None:
var prefix: String = ""
if not compressing:
prefix = "un"
Expand Down Expand Up @@ -39,15 +41,15 @@ fn uncompress(data: List[UInt8], quiet: Bool = True) raises -> List[UInt8]:
Error: If the zlib operation fails.
"""
var data_memory_amount: Int = len(data) * 10
var handle = ffi.DLHandle("")
var handle = ffi.DLHandle(".magic/envs/default/lib/libz.so.1")
var zlib_uncompress = handle.get_function[zlib_type]("uncompress")

var uncompressed = Pointer[Bytef].alloc(data_memory_amount)
var compressed = Pointer[Bytef].alloc(len(data))
var uncompressed_len = Pointer[uLong].alloc(1)
var uncompressed = UnsafePointer[Bytef].alloc(data_memory_amount)
var compressed = UnsafePointer[Bytef].alloc(len(data))
var uncompressed_len = UnsafePointer[uLong].alloc(1)
memset_zero(uncompressed, data_memory_amount)
memset_zero(uncompressed_len, 1)
uncompressed_len[0] = data_memory_amount
uncompressed_len.init_pointee_copy(data_memory_amount)
for i in range(len(data)):
compressed.store(i, data[i])

Expand All @@ -60,7 +62,6 @@ fn uncompress(data: List[UInt8], quiet: Bool = True) raises -> List[UInt8]:

if not quiet:
_log_zlib_result(Z_RES, compressing=False)
print("Uncompressed length: " + str(uncompressed_len[0]))
# Can probably do something more efficient here with pointers, but eh.
var res = List[UInt8]()
for i in range(uncompressed_len[0]):
Expand Down
10 changes: 7 additions & 3 deletions mojoproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "mimage"
version = "0.1.2"
version = "0.1.3"
description = "A Mojo library for parsing images"
authors = ["Ferdinand Schenck <[email protected]>"]
channels = ["conda-forge", "https://conda.modular.com/max"]
Expand All @@ -9,8 +9,12 @@ platforms = ["linux-64"]
[tasks]

[dependencies]
max = ">=24.4.0dev7,<24.5"
max = ">=24.5,<24.6"

[feature.dev.dependencies]
pillow = ">=10.4.0,<10.5"
pre-commit = ">=3.7.1,<3.8"
pre-commit = ">=3.8.0,<4"

[environments]
default = ["dev"]
dev = ["dev"]
2 changes: 1 addition & 1 deletion scripts/check-docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def main():
]
result = subprocess.run(command, capture_output=True)
if result.stderr or result.returncode != 0:
print(f"Docstring issue found in the stdlib: ")
print(f"Docstring issue found in mimage: ")
print(result.stderr.decode())
sys.exit(1)

Expand Down

0 comments on commit 6344001

Please sign in to comment.