62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
# pdf-segmented: Generate PDFs using separate compression for foreground and background
|
|
# Copyright (C) 2025 Lee Yingtong Li
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
from . import CompressedLayer
|
|
|
|
from PIL import Image
|
|
|
|
from dataclasses import dataclass
|
|
import io
|
|
import struct
|
|
|
|
@dataclass
|
|
class PNGLayer(CompressedLayer):
|
|
data: bytes
|
|
|
|
def get_flate_data(self):
|
|
# Parse PNG data to get the IDAT chunks
|
|
bytesio = io.BytesIO(self.data)
|
|
bytesio.read(8) # Read PNG header
|
|
|
|
flate_data = bytearray()
|
|
|
|
while True:
|
|
# Read PNG chunks
|
|
length_bytes = bytesio.read(4)
|
|
if length_bytes == b'': # EOF
|
|
break
|
|
length = struct.unpack('>I', length_bytes)[0]
|
|
cid = bytesio.read(4)
|
|
data = bytesio.read(length)
|
|
crc = bytesio.read(4)
|
|
|
|
# IDAT chunk contains DEFLATE data
|
|
if cid == b'IDAT':
|
|
flate_data.extend(data)
|
|
|
|
return bytes(flate_data)
|
|
|
|
def png_compress_layer(layer: Image, is_foreground: bool) -> PNGLayer:
|
|
if is_foreground:
|
|
# Foreground is 1bpp
|
|
layer = layer.convert('1')
|
|
|
|
# Save image to PNG
|
|
bytesio = io.BytesIO()
|
|
layer.save(bytesio, format='png', optimize=True)
|
|
|
|
return PNGLayer(data=bytesio.getvalue())
|