diff --git a/modules/tga_converter.py b/modules/tga_converter.py index 8753099..bde868d 100644 --- a/modules/tga_converter.py +++ b/modules/tga_converter.py @@ -188,15 +188,29 @@ class TGAConverter: """ try: with open(tga_file.filepath, 'rb') as f: - # Skip header - f.seek(18) + # Read header to get ID length + header = f.read(18) + if len(header) < 18: + logger.warning(f"Invalid TGA header: {tga_file.filepath}") + return None + + id_length = header[0] + color_map_type = header[1] + image_type = header[2] # Skip ID field if present - header = f.read(18) - id_length = header[0] if len(header) >= 18 else 0 if id_length > 0: f.seek(18 + id_length) + # Skip color map if present + if color_map_type == 1: + # Read color map spec + color_map_offset = 3 # After id_length, color_map_type, image_type + color_map_length = struct.unpack(' {png_path.name}") + return png_path + + except Exception as pil_error: + logger.debug(f"PIL failed, trying manual: {pil_error}") + + # Fallback to manual TGA reading + tga_file = self.read_tga_header(tga_path) + if not tga_file: + return None + + logger.info(f"Converting: {tga_path.name} ({tga_file})") + + pixels = self.read_tga_pixels(tga_file) + if pixels is None: + return None + + # Create PIL Image + if pixels.shape[2] == 4: + image = Image.fromarray(pixels, 'RGBA') + else: + image = Image.fromarray(pixels, 'RGB') + + # Determine output path + if output_name is None: + output_name = tga_path.stem + + png_path = self.output_dir / f"{output_name}.png" + image.save(png_path, 'PNG') + + logger.info(f"Converted (manual): {tga_path.name} -> {png_path.name}") + return png_path + except Exception as e: logger.error(f"Error converting {tga_path}: {e}") return None