title: Directly manipulating PNG colormaps
body: <p>I'm working on a project converting images packed into a proprietary container format into individual PNG images which then get repacked into a more flexible container format.  One of the odder restrictions for this is that the order of the colormap entries are critical for proper operation of the end application (the palettes of some images depend on the palettes of other images currently displayed).</p>
  <p>Is there some way I can, preferably in Perl, directly manipulate the colormap of a PNG?  I'm using PerlMagick to generate the PNG files pixel-by-pixel.  Please let me know if it would be helpful for me to more directly describe what I'm up to.</p>
  <p>(expanding on this because of Mark's suggestion)</p>
  <p>The screen is filled first with background elements, like a decorative frame and a compass rose.  Parts of the compass rose light up to indicate which direction the player can go.  These lit up sections are separate images which take their coloring cues from specific slots in the colormaps for the background element images.  Therefore the content and order of the colormaps for the background elements are critical.</p>
  <p>(even more expanding)</p>
  <p>I'll lay out exactly what I'm doing now.  The original container format was created by Infocom in the late 1980s for its graphical text adventures.  For each of the graphical games, three graphics files were made, one for CGA, EGA, and MCGA.  Some EGA graphics files were spread across two containers.  For play on modern Z-machine interpreters, the format was reverse-engineered and the graphics within were converted to PNG and repacked into Blorb containers (<a href="https://www.eblong.com/zarf/blorb/" rel="nofollow noreferrer">https://www.eblong.com/zarf/blorb/</a>).  Section 11.3 talks about the color-changing behavior.  Just the MCGA packs were repacked like this.  I want to create packs for the CGA and EGA packs.  Unfortunately, I've been unable to contact the person who prepared these Blorb files to get information about this colormap problem.  The reverse-engineering of Infocom's format seems to be lost.  So I took existing tools, mainly <code>pix2gif.c</code> from Ztools (<a href="http://ifarchive.org/indexes/if-archive/infocom/tools/ztools/" rel="nofollow noreferrer">http://ifarchive.org/indexes/if-archive/infocom/tools/ztools/</a>) and translated it to Perl to better understand it.  With this Perl program, Imanaged to extract the EGA and CGA data.  I've modified my own tools (<a href="https://gitlab.com/DavidGriffith/blorbtools" rel="nofollow noreferrer">https://gitlab.com/DavidGriffith/blorbtools</a>) to support the special requirements of these games.</p>
  <p>Starting off with the MCGA-targeted graphics, I cannot get to the point of replicating the look of playing a game with the older Blorb files available here: <a href="http://ifarchive.org/indexes/if-archive/infocom/media/blorb" rel="nofollow noreferrer">http://ifarchive.org/indexes/if-archive/infocom/media/blorb</a>.  The images with changing palettes are drawn with the wrong colors.  There are other problems with the EGA and CGA graphics, but have to do with resolution and are not relevant here.</p>
  <p>These the specific games involved are Infocom's games &quot;Arthur&quot;, &quot;Shogun&quot;, &quot;Journey&quot;, and &quot;Zork Zero&quot;.  I maintain one of the Z-machine interpreters capable of playing these games, which can be found at <a href="https://gitlab.com/DavidGriffith/frotz" rel="nofollow noreferrer">https://gitlab.com/DavidGriffith/frotz</a>.</p>