Engie File Converter
-------------- Created by by Nyerguds (04/04/2017)

This tool was originally created to allow viewing and converting image formats found in the
Nintendo 64 ROM of the Command & Conquer game. Since then, it has evolved into a more general
modding suite supporting a large range of file formats from classic 90's era games.


Special thanks to:
-Kamuix and Chad1233 for their help in testing this
-Moon Flower for the help in designing the palette manager
-MrFlibble for restoring the beta C&C95 Engineer icon.


If you like what I'm doing, and you have too much money, feel free to donate:
https://www.paypal.com/donate/?token=ATYlH9LdfgV3n-LXbIKxJh20H9NUwt85u3TECWCY-rvrECEGZ26wqf6sREWk9Cejao9-pG


Features:
--------
-Supports opening and saving all classic image types (png/gif/bmp/jpg), and a load of game formats.
-Gives a visual representation of the loaded file, with zoom and adjustable background colour.
-Can display and edit the file's colour palette, if it has one.
-Can save opened files as any supported format, though some conversions are obviously impossible;
 high-colour images can't be saved as palette, and only maps can be saved as maps.
-Preserves the color palette when saving 4-bit and 8-bit paletted images.
-Supports drag & drop for opening files.
-Has utilities to help generate height maps for missions.
-Offers save options for the types that need them.


Acknowledgements:
----------------
The study of the C&C64 files was made possible by the Universal Game Decompressor
(GEDecompressor), made by SubDrag.
http://www.goldeneyevault.com/viewfile.php?id=213

The PC C&C formats were deciphered using documentation written by Vladan Bato.

The compression code used for handling the CPS and LCW formats was gracefully provided by the
people of the RA++ project. OmniBlade has been particularly helpful.
https://redalertpp.org/

The Dynamix LZW decompression code was converted from the C++ code of VOGONS.org user tikalat's
Midi Tools. It contains no license but I assume the fact he included the code implies that he
doesn't mind it being useful to more people.
https://www.vogons.org/viewtopic.php?p=273448#p273448

The K.O.R.T. image format was decoded thanks to documentation included in the K.O.R.T. savegame
editor written by David Eriksson, aka Edison of 2GooD:
http://www.2good.nu/projekt/korted

The K.O.R.T. BMP format was mapped by TheRuler on the OldGamesItalia forum:
http://www.oldgamesitalia.net/forum/index.php?s=&showtopic=16027&view=findpost&p=360174

The code of ScummVM helped a lot to figure out the compression on the Elvira VGA format.
https://www.scummvm.org/

The Mythos Visage format's RLE compression formats were figured out by user Ceiwad of the
shikadi.net modding wiki, who posted his findings here:
http://www.shikadi.net/moddingwiki/Visage_Format
(though the topic has been thoroughly clarified by me since then :p)

The Dune II SHP format was implemented based on a document written by Ultraq, with further
assistance from OmniBlade.


Version history:
---------------
This tool started as the CnC64ImageViewer, but later got expanded to a full conversion suite.
At that point, it became the CnC64FileConverter v1.0. As of v1.3, it has been renamed to
Engie File Converter.


Engie File Converter versions:

v1.3.0: (10/08/2018 23:05)
-Renamed the project. Original projected name was "Nyerguds's Game File Converter", or "NGFC"
 which then mutated into "Engie FC", or "Engie File Converter".
-Visage Animation load chaining now also works with a png file as base for a chain.
-Fixed a problem with 16bpp formats in the "Paste on frames" function.
-Disabled command line conversion; it was unfinished and unmaintained. Might fix it up later.
-Added function to convert a single image to frames, by specifying the frame size.
-Added function to convert frames to a single image, with the option to expand the frame sizes.

CnC64FileConverter versions:

v1.2.20: (03/08/2018 12:40)
-Added better memory management in the "paste image on frames" feature.
-Optimised the paste logic for frame ranges containing palette differences.
-Files that were pasted on will no longer identify as having a common palette if they don't.
-Visage Animation continued from a previous file will now use its own palette everywhere.

v1.2.19: (03/08/2018 21:20)
-Loading a Visage Animation's initial state from PNG no longer messed up the save logic.
-Added feature to paste an image on a range of frames.

v1.2.18: (27/07/2018 20:30)
-Implemented Westwood Dune II Shape (SHP) support.
-Save options can now get disabled based on a parent option's value.
-Save options with text input can now filter out illegal characters.

v1.2.17: (19/07/2018 19:21)
-Implemented Westwood Tiberian Sun Shape (SHP) support.

v1.2.16: (16/07/2018 08:50)
-Implemented Westwood C&C1/RA1 Shape (SHP) support.
-Fixed slow loading on VDA files due to bug in colour palette handling.

v1.2.15: (10/07/2018 18:35)
-Saving Mythos VDA no longer allows frames after 0 to have pixels of colour 255 on new locations.
-Disallowed saving frame types as palettes since some types don't have a global palette.
-Fixed "File not found" error when trying to load a Mythos VDA file without VDX.
-Added the ability to change the alpha value on palette indices. PNG supports such palette-alpha.
-Mythos animations now show the total chunk count in the file on the index frame.
-Added more detailed save options for the Mythos animations compression algorithm.

v1.2.14: (28/06/2018 13:50)
-A VDA chained from png will now indicate its first frame the same way as a normally loaded png.
-Fixed bug that could let WSA load chaining skip missing files (which wouldn't load correctly).
-Fixed chunk overflow check mechanism in the VDA animations save process.
-Indexed images saved as png get a save option asking to save without transparency.
 This option defaults to true on types with a fixed transparency index determined by the game.
-Fixed bug in Mythos collapsed-transparency compression that made it save a wrong header size.
-Changed Windows icon handling so it no longer shows up as a type that supports frame input.
-Fixed focus issues in the save options window.

v1.2.13: (26/06/2018 13:04)
-Added check on maximum compressed length in Mythos RLE format; it can't exceed 0xFFFF.
-Added check on maximum amount of chunks when saving Mythos VDA format; it can't exceed 0x7FFF.
-Added a leniency (of 8/256) to the palette match check in Mythos VDA chaining logic.
-Added ability to override normal chaining with a single png image of the same name.
-Added proper cleanup of temporary images in memory loaded in the chaining logics.
-Added fixes for some unlikely fringe cases in VDA chaining that could crash the program.

v1.2.12: (24/06/2018 19:25)
-Changed load chaining on the Mythos animation format so the found base frame is added.
 Westwood WSA format reserves an empty frame for this anyway, so it remains unchanged.

v1.2.11: (24/06/2018 17:00)
-Changed chunk merging on Mythos VDA format to a save option.
-Improved multiple-extensions support in saving dialog.
-Optimised Mythos animation VDX writing to avoid using expensive 'new offset' commands.
-Fixed bug that made Mythos animations not save from loaded image file frames.
-Added load chaining for Mythos VDA and Westwood WSA format.
-Improved support for opening Windows icon files.

v1.2.10: (23/06/2018 01:00)
-Fixed transparency support in several supported types.
-Added identical-tile optimising in C&C tileset saving.
-Fixed bug in PixelFormatter that corrupted colour writing by mixing up the channels order.
-Fixed bug in the Mythos RLE compression when saving flag bytes near the end of a line.
-Implemented loading and saving of Mythos animation and palette files.

v1.2.9: (06/06/2018 14:40)
-Added Westwood palette stretch tables as new supported type.
-When zooming in, the image panel will stay centered on one point.
-Unloaded images are now properly cleaned up and removed from memory.
-Upgraded the general pixel format converter used for N64 16-bit images.
-Changed name of "Mythos VGS format" to "Mythos Visage format".
-Fixed bug that removed transparency from the last frame in Mythos Visage format.
-Implemented full compression loading and saving support for Mythos Visage format.

v1.2.8: (25/01/2018 18:50)
-Added vertical image scrolling with shift + scrollwheel.
-Image types that can natively be opened by the .Net framework but are not specifically
 supported in the editor (like .tif) will now open as general type "Image".
-The disabled palette dropdown will now correctly display the reason why it is disabled.
-When loading a file, the zoom factor will automatically adjust to the loaded image.

v1.2.7: (15/11/2017 07:36)
-Fixed background handling in MA8 chunk data by switching index 00 and FF on the image.

v1.2.6: (15/11/2017 14:32)
-Added support for MA8 format used in the Macintosh versions of the Dynamix images.

v1.2.5: (09/11/2017 13:47)
-Fixed the X and Y trimming functions to make WSA save work correctly.

v1.2.4: (20/10/2017 13:37)
-Fixed some checks in the file load to make them fail properly instead of giving internal errors.
-Added Westwood WSA format, though the detection might not be 100% correct yet.

v1.2.3: (26/09/2017 19:05)
-Fixed oversight in the KotB format. The written files now work correctly in the game.

v1.2.2: (24/09/2017 16:50)
-Fixed main chunks in Dynamix files not saving as container chunks.

v1.2.1: (23/09/2017 21:23)
-Fixed crash when saving images with uneven width in Dynamix VGA/BIN format.
-Added tighter checks on Dynamix image input; they can only handle widths divisible by 8.
-Exporting to frames now always takes png as default format.

v1.2: (22/09/2017 15:30)
-Added new Save Options dialog for types that need extra options for saving.
-Added very efficient automatic filter system to determine what types a file can be saved as.
-Removed "export" function; it was no longer useful.
-Fixed errors in loading and saving code for Kings of the Beach .PAK format.
-Restricted Kings of the Beach .PAK format to 320x200 for better autodetect accuracy.
-Changed form size to be able to display a 640x480 image without needing a resize.
-Optimised, simplified and unified all the different RLE compression methods.
-Added support for loading and saving Westwood CPS using the standard LZW algorithm.
-Removed automatic grayscale conversion on paletteless C&C64 images.
-Detects most specific type when opening a general format like "Image" through the Open menu.
-Type detection is restricted to the types contained in the chosen type in the Open File menu.

v1.1.4: (13/09/2017 09:27)
-Improved file detection checks and error message handling in several file types.

v1.1.3: (12/09/2017 21:20)
-Optimised RLE compression for Elvira VGA format to compress better than the original.
-Added checks on empty data before saving.
-Changed frames handling so types can have frames without being seen as a frames container.
-Added support for viewing the 4-bit version of Dynamix SCR files using new frames system.
-Disabled support for Dynamix BMP files; the image data can't actually be viewed anyway.
-When opening an empty file, the program will not not attempt type detection.
-Importing a range of frames from numbered files now also works when opening an empty file.
-Saving frames data without a full image filters out non-frames types in the Save dialog.
-Fixed bug where the stride wasn't collapsed to the width on grayscale C&C64 images.
-Added support for King of the Beach .PAK image format.

v1.1.2: (07/09/2017 15:03)
-Added better support for frames; when opening an image with a numeric suffix that is part of
 a range of images, the tool will ask to load it as frames.
-Added Elvira VGA writing support.

v1.1.1: (10/08/2017 15:53)
-Added support for different compression types in CPS files.

v1.1.0: (05/08/2017 19:43)
-Added support for the KORT_DAT.### images of "King Arthur's Knights of the Round Table"
-For formats with optional compression, (like SCR), hold shift while clicking "save"
 to save as uncompressed.

v1.0.9: (31/07/2017 00:24)
-Fixed confusing naming in internal toolsets, and simplified byte reading/writing methods.
-Fixed bug where the height map conversion tools caused files to lose their file name,
 which caused crashes when trying to save the result.
-Fixed errors in CPS reading.
-Added Dynamix SCR reading and writing, though only the VGA/BIN type is supported.

v1.0.8: (05/05/2017 17:53)
-Fixed bugs in opening logic that made the program give errors on opening .IMG files.
-Fixed oversight in .IMG saving logics that didn't pass on the palette length.
-Fixed bug in the N64 8-bit palette data that gave it the .pa4 extension.
-Added support for reading and writing LCW-compressed images.

v1.0.7: (01/05/2017 20:08)
-Added an option to copy the image to the clipboard.
-Added palette support for paletteless file formats.
-Added the ability to open C&C template (tileset) files.

v1.0.6: (07/04/2017 22:34)
-Fixed bug in the file open logic that prevented opening certain types through the menus.

v1.0.5: (07/04/2017 21:21)
-Fixed a bug that prevented full clearing of the left map edge
-Fixed a minor bug in the terrain type definitions.
-When an ini file dropped into the program it will check if a map can be found for it.
-UI options now get disabled when not applicable.

v1.0.4: (07/04/2017 17:32)
-The converted 65x65 height maps will immediately be loaded in the program as paletteless IMG
 format, so they can more easily be saved into that format.

v1.0.3: (07/04/2017 15:30)
-Fixed small palette import bug in tilesets
-Added tools to generate height maps from missions.

v1.0.2: (04/04/2017 23:53)
-Fixed png export of map files.

v1.0.1: (04/04/2017 23:33)
-Fixed behaviour of map import to allow PC maps saved by CCMAP.

v1.0: (04/04/2017 22:33)
-Removed img type debug info (internal data size) from the user interface.
-Can now open normal images (png/gif/bmp/jpg).
-Can now open both PC and N64 format C&C maps, visualizing them as 64x64 image with terrain types,
 and allowing saving as the other type (or as image).
-Added code to correctly open paletted png images that contain transparency.
-Added code to correctly open and save paletted png images with incomplete colour palettes.
-Added theater detection for maps. If there's an ini, it'll read it from that.
-Added support for PC and N64 coluor palettes.
-Added support for saving N64 IMG format, and specifically paletteless greyscale IMG.
-Added read support for terrain tiles; the're triplets of DA4/ND4/PA4 or DA8/ND8/PA8 files.


CnC64ImageViewer versions:

v1.2.2: (03/02/2017 08:00)
-Fixed a crash in the zooming panel arrow keys scrolling.

v1.2.1: (20/11/2016 16:00)
-Added error checking and feedback on failure to load.
-Optimized palette viewer, and added an indicator for transparency.
-Version number is shown on the title bar.
-Fixed a bug in the viewer that made it shift the image slightly up.

v1.2: (19/11/2016 17:21)
-Identified last unknown value in the header as part of the "number of colours" value.
-Added palette viewer.
-Added icon.

v1.1: (19/11/2016 14:35)
-Tool now shows the full header data and other file info.
-Implemented zoom on the viewer.
-Transparency colour in the viewer can be customized.
-Added classic Open / Save buttons with dialogs.

v1.0: (19/11/2016 01:04)
-Very rudimentary decoder and viewer.
-Double-clicking an image saves it to disk as png in folder of origin.
