|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os |
| 4 | +import struct |
| 5 | +import argparse |
| 6 | +from PIL import Image |
| 7 | + |
| 8 | +# Standard Amiga icon palettes |
| 9 | +PALETTES = { |
| 10 | + '1.x': [ |
| 11 | + (0x55, 0xAA, 0xFF), # color 0 - light blue |
| 12 | + (0xFF, 0xFF, 0xFF), # color 1 - white |
| 13 | + (0x00, 0x00, 0x00), # color 2 - black |
| 14 | + (0xFF, 0x88, 0x00) # color 3 - orange |
| 15 | + ], |
| 16 | + '2.x': [ |
| 17 | + (0x95, 0x95, 0x95), # color 0 - gray |
| 18 | + (0x00, 0x00, 0x00), # color 1 - black |
| 19 | + (0xFF, 0xFF, 0xFF), # color 2 - white |
| 20 | + (0x3B, 0x67, 0xA2) # color 3 - blue |
| 21 | + ] |
| 22 | +} |
| 23 | + |
| 24 | +def closest_color(pixel, palette): |
| 25 | + """Find the closest color in the palette.""" |
| 26 | + return min(range(len(palette)), |
| 27 | + key=lambda i: sum((a-b)**2 for a, b in zip(pixel[:3], palette[i]))) |
| 28 | + |
| 29 | +def convert_to_bitplanes(image, palette): |
| 30 | + """Convert PIL Image to Amiga bitplane format.""" |
| 31 | + width, height = image.width, image.height |
| 32 | + padded_width = ((width + 15) >> 4) << 4 # Pad to 16-bit word boundary |
| 33 | + bitplanes = [] |
| 34 | + |
| 35 | + for plane in range(2): # 2 bitplanes = 4 colors |
| 36 | + plane_data = bytearray(padded_width * height // 8) |
| 37 | + for y in range(height): |
| 38 | + for x in range(width): |
| 39 | + pixel = image.getpixel((x, y)) |
| 40 | + color_index = closest_color(pixel, palette) |
| 41 | + |
| 42 | + # Check if this color's bit is set in this bitplane |
| 43 | + if color_index & (1 << plane): |
| 44 | + byte_index = (y * padded_width + x) // 8 |
| 45 | + bit_offset = 7 - (x % 8) |
| 46 | + plane_data[byte_index] |= (1 << bit_offset) |
| 47 | + |
| 48 | + bitplanes.append(plane_data) |
| 49 | + |
| 50 | + return padded_width, bitplanes |
| 51 | + |
| 52 | +def create_amiga_icon(input_image_path, icon_type=3, icon_width=48, icon_height=48, output_path=None, palette_version=2): |
| 53 | + """Create an Amiga Workbench icon from an input image.""" |
| 54 | + # Open input image and resize/convert |
| 55 | + img = Image.open(input_image_path).convert('RGB') |
| 56 | + |
| 57 | + # Resize image to specified icon size |
| 58 | + img = img.resize((icon_width, icon_height), Image.LANCZOS) |
| 59 | + |
| 60 | + # Select palette based on version |
| 61 | + palette_key = f'{palette_version}.x' |
| 62 | + if palette_key not in PALETTES: |
| 63 | + raise ValueError(f"Invalid palette version. Choose 1 or 2.") |
| 64 | + palette = PALETTES[palette_key] |
| 65 | + |
| 66 | + # Convert image to bitplanes |
| 67 | + padded_width, bitplanes = convert_to_bitplanes(img, palette) |
| 68 | + |
| 69 | + # Prepare icon file path |
| 70 | + if output_path is None: |
| 71 | + # Default behavior: base name of input image with .info extension |
| 72 | + icon_path = os.path.splitext(input_image_path)[0] + '.info' |
| 73 | + else: |
| 74 | + # Use specified output path |
| 75 | + icon_path = output_path |
| 76 | + |
| 77 | + # Create icon file |
| 78 | + with open(icon_path, 'wb') as f: |
| 79 | + # DiskObject structure |
| 80 | + # Magic number and version |
| 81 | + f.write(struct.pack('>H', 0xE310)) # do_Magic |
| 82 | + f.write(struct.pack('>H', 1)) # do_Version |
| 83 | + |
| 84 | + # Gadget details |
| 85 | + f.write(struct.pack('>I', 0)) # do_Gadget.NextGadget (unused) |
| 86 | + f.write(struct.pack('>h', 0)) # do_Gadget.LeftEdge |
| 87 | + f.write(struct.pack('>h', 0)) # do_Gadget.TopEdge |
| 88 | + f.write(struct.pack('>H', icon_width)) # do_Gadget.Width |
| 89 | + f.write(struct.pack('>H', icon_height)) # do_Gadget.Height |
| 90 | + f.write(struct.pack('>H', 5)) # do_Gadget.Flags |
| 91 | + f.write(struct.pack('>H', 3)) # do_Gadget.Activation |
| 92 | + f.write(struct.pack('>H', 1)) # do_Gadget.GadgetType |
| 93 | + f.write(struct.pack('>I', 1)) # do_Gadget.GadgetRender (non-zero) |
| 94 | + f.write(struct.pack('>I', 0)) # do_Gadget.SelectRender |
| 95 | + f.write(struct.pack('>I', 0)) # do_Gadget.GadgetText |
| 96 | + f.write(struct.pack('>I', 0)) # do_Gadget.MutualExclude |
| 97 | + f.write(struct.pack('>I', 0)) # do_Gadget.SpecialInfo |
| 98 | + f.write(struct.pack('>H', 0)) # do_Gadget.GadgetID |
| 99 | + f.write(struct.pack('>I', 1)) # do_Gadget.UserData (OS 2.x revision) |
| 100 | + |
| 101 | + # Icon type |
| 102 | + f.write(struct.pack('>B', icon_type)) |
| 103 | + f.write(b'\x00') # padding |
| 104 | + |
| 105 | + # No default tool or tooltypes |
| 106 | + f.write(struct.pack('>I', 0)) # do_DefaultTool |
| 107 | + f.write(struct.pack('>I', 0)) # do_ToolTypes |
| 108 | + |
| 109 | + # Icon position |
| 110 | + f.write(struct.pack('>i', 0)) # do_CurrentX |
| 111 | + f.write(struct.pack('>i', 0)) # do_CurrentY |
| 112 | + |
| 113 | + # No DrawerData |
| 114 | + f.write(struct.pack('>I', 0)) # do_DrawerData |
| 115 | + |
| 116 | + # No ToolWindow |
| 117 | + f.write(struct.pack('>I', 0)) # do_ToolWindow |
| 118 | + |
| 119 | + # Default stack size |
| 120 | + f.write(struct.pack('>I', 4096)) # do_StackSize |
| 121 | + |
| 122 | + # Image structure for normal state |
| 123 | + f.write(struct.pack('>h', 0)) # LeftEdge |
| 124 | + f.write(struct.pack('>h', 0)) # TopEdge |
| 125 | + f.write(struct.pack('>H', icon_width)) # Width |
| 126 | + f.write(struct.pack('>H', icon_height)) # Height |
| 127 | + f.write(struct.pack('>H', 2)) # Depth (2 bitplanes) |
| 128 | + f.write(struct.pack('>I', 1)) # ImageData presence flag |
| 129 | + f.write(struct.pack('>B', 0b11)) # PlanePick |
| 130 | + f.write(struct.pack('>B', 0)) # PlaneOnOff |
| 131 | + f.write(struct.pack('>I', 0)) # NextImage |
| 132 | + |
| 133 | + # Write bitplane data |
| 134 | + for bitplane in bitplanes: |
| 135 | + f.write(bitplane) |
| 136 | + |
| 137 | + print(f"Amiga Workbench icon created at: {icon_path}") |
| 138 | + |
| 139 | +def main(): |
| 140 | + parser = argparse.ArgumentParser(description='Convert image to Amiga Workbench icon') |
| 141 | + parser.add_argument('input_image', help='Path to input image file') |
| 142 | + |
| 143 | + # Make icon type optional with default of 3 (Tool) |
| 144 | + parser.add_argument('--type', type=int, choices=range(1, 9), default=3, |
| 145 | + help='Icon type (1: Disk, 2: Drawer, 3: Tool, 4: Project, 5: Trashcan, 6: Device, 7: Kickstart ROM, 8: Appicon, default: 3)') |
| 146 | + |
| 147 | + # Add configurable icon size arguments with defaults |
| 148 | + parser.add_argument('--width', type=int, default=48, |
| 149 | + help='Icon width in pixels (default: 48)') |
| 150 | + parser.add_argument('--height', type=int, default=48, |
| 151 | + help='Icon height in pixels (default: 48)') |
| 152 | + |
| 153 | + # Add optional output path argument |
| 154 | + parser.add_argument('--output', type=str, |
| 155 | + help='Optional output path for the .info file') |
| 156 | + |
| 157 | + # Add optional palette version argument |
| 158 | + parser.add_argument('--palette', type=int, choices=[1, 2], default=2, |
| 159 | + help='Palette version (1 for 1.x, 2 for 2.x, default: 2)') |
| 160 | + |
| 161 | + args = parser.parse_args() |
| 162 | + |
| 163 | + create_amiga_icon( |
| 164 | + args.input_image, |
| 165 | + args.type, |
| 166 | + args.width, |
| 167 | + args.height, |
| 168 | + args.output, |
| 169 | + args.palette |
| 170 | + ) |
| 171 | + |
| 172 | +if __name__ == '__main__': |
| 173 | + main() |
0 commit comments