<> <> DIRECTORY AIS, ColorMap, DitherAIS, DisplayMap, FS, Graphics, GraphicsBasic, GraphicsColor, GraphicsOps, IO, Menus, Rope, ViewerClasses, ViewerOps, ViewerTools; DitherImpl: MONITOR IMPORTS AIS, ColorMap, DitherAIS, DisplayMap, FS, Graphics, GraphicsColor, GraphicsOps, IO, Menus, Rope, ViewerOps, ViewerTools = BEGIN MapFileStem:Rope.ROPE _ "Std"; FileToDisplay:Rope.ROPE _ NIL; MapTable: DisplayMap.ColorTable; MapPal: DisplayMap.PalTable; DitherPaint: ViewerClasses.PaintProc = TRUSTED { IF whatChanged#NIL THEN ShowMap[context] ELSE { IF FileToDisplay = NIL THEN RETURN; Graphics.SetCP[self:context, x:50, y:0]; Graphics.DrawImage[self:context, image:GraphicsOps.NewAisImage[FileToDisplay], raw:TRUE]; }; }; SetColorMap: PROCEDURE [PMapFileStem:Rope.ROPE] = { fileName:Rope.ROPE _ Rope.Cat[PMapFileStem, ".tab"]; IF FileExists[fileName] THEN { MapFileStem _ PMapFileStem; [MapTable, MapPal] _ DisplayMap.Restore[fileName]; }; FOR palix:CARDINAL IN [0..MapPal.size) DO OPEN MapPal[palix]; ColorMap.SetRGBColor[palix, r,g,b]; ENDLOOP }; ShowMap: PROCEDURE [context:Graphics.Context] = { palix:CARDINAL _ 0; FOR i:CARDINAL IN [0..7] DO FOR j:CARDINAL IN [0..7] DO FOR k:CARDINAL IN [0..3] DO OPEN MapPal[palix]; Graphics.SetColor[context, GraphicsColor.RGBToColor[r,g,b]]; Graphics.DrawBox[context, [k*120+j*15+50, i*50+50, k*120+65+j*15, i*50+100]]; palix _ palix + 1; IF palix=MapPal.size THEN RETURN ENDLOOP; ENDLOOP; ENDLOOP; }; DitherGet: ViewerClasses.GetProc = TRUSTED { RETURN [NIL] }; FileExists: PROCEDURE [fileName:Rope.ROPE] RETURNS [answer:BOOLEAN_TRUE] = { <> s:IO.STREAM _ FS.StreamOpen[fileName:fileName ! FS.Error => TRUSTED { answer_FALSE; GOTO notThere; }]; IO.Close[s]; EXITS notThere => NULL; }; Dither: ENTRY Menus.MenuProc = TRUSTED { filestem:Rope.ROPE _ ViewerTools.GetSelectionContents[]; redFilename:Rope.ROPE _ Rope.Cat[filestem, "-red.ais"]; blueFilename:Rope.ROPE _ Rope.Cat[filestem, "-blu.ais"]; greenFilename:Rope.ROPE _ Rope.Cat[filestem, "-grn.ais"]; FSFilename:Rope.ROPE _ Rope.Cat[filestem, "-", MapFileStem, ".ais"]; ditherViewer.name _ Rope.Cat["Dithering: ", filestem, " - Map: ", MapFileStem]; ViewerOps.PaintViewer[ditherViewer, caption, FALSE, NIL]; IF NOT FileExists[FSFilename] THEN { DitherAIS.Squash[redFilename, greenFilename, blueFilename, FSFilename, MyMap ! AIS.Error => { ditherViewer.name _ Rope.Cat[FSFilename, " - Bad file name", " - Map: ", MapFileStem]; ViewerOps.PaintViewer[ditherViewer, caption, FALSE, NIL]; GOTO giveUp}]; }; FileToDisplay _ FSFilename; ditherViewer.name _ Rope.Cat[FSFilename, " - Map: ", MapFileStem]; ViewerOps.PaintViewer[ditherViewer, client, FALSE, NIL]; ViewerOps.PaintViewer[ditherViewer, caption, FALSE, NIL]; EXITS giveUp => NULL; }; Quantize: ENTRY Menus.MenuProc = TRUSTED { filestem:Rope.ROPE _ ViewerTools.GetSelectionContents[]; redFilename:Rope.ROPE _ Rope.Cat[filestem, "-red.ais"]; blueFilename:Rope.ROPE _ Rope.Cat[filestem, "-blu.ais"]; greenFilename:Rope.ROPE _ Rope.Cat[filestem, "-grn.ais"]; FSFilename:Rope.ROPE _ Rope.Cat[filestem, "-Q", MapFileStem, ".ais"]; ditherViewer.name _ Rope.Cat["Quantizing: ", filestem, " - Map: ", MapFileStem]; ViewerOps.PaintViewer[ditherViewer, caption, FALSE, NIL]; IF NOT FileExists[FSFilename] THEN { DitherAIS.Squash[redFilename, greenFilename, blueFilename, FSFilename, MyQuan ! AIS.Error => { ditherViewer.name _ Rope.Cat[FSFilename, " - Bad file name", " - Map: ", MapFileStem]; ViewerOps.PaintViewer[ditherViewer, caption, FALSE, NIL]; GOTO giveUp}]; }; FileToDisplay _ FSFilename; ditherViewer.name _ Rope.Cat[FSFilename, " - Map: ", MapFileStem]; ViewerOps.PaintViewer[ditherViewer, client, FALSE, NIL]; ViewerOps.PaintViewer[ditherViewer, caption, FALSE, NIL]; EXITS giveUp => NULL; }; Separate: ENTRY Menus.MenuProc = TRUSTED { filestem:Rope.ROPE _ ViewerTools.GetSelectionContents[]; redFilename:Rope.ROPE _ Rope.Cat[filestem, "fs-red.ais"]; blueFilename:Rope.ROPE _ Rope.Cat[filestem, "fs-blu.ais"]; greenFilename:Rope.ROPE _ Rope.Cat[filestem, "fs-grn.ais"]; FSFilename:Rope.ROPE _ Rope.Cat[filestem, "-", MapFileStem, ".ais"]; ditherViewer.name _ Rope.Cat["Separating: ", filestem, " - Map: ", MapFileStem]; ViewerOps.PaintViewer[ditherViewer, caption, FALSE, NIL]; IF FileExists[FSFilename] THEN { DitherAIS.Stretch[redFilename, greenFilename, blueFilename, FSFilename, MyUnMap ! AIS.Error => { ditherViewer.name _ Rope.Cat[FSFilename, " - Bad file name", " - Map: ", MapFileStem]; ViewerOps.PaintViewer[ditherViewer, caption, FALSE, NIL]; GOTO giveUp}]; }; ditherViewer.name _ Rope.Cat[FSFilename, " - Map: ", MapFileStem]; ViewerOps.PaintViewer[ditherViewer, caption, FALSE, NIL]; EXITS giveUp => NULL; }; QSeparate: ENTRY Menus.MenuProc = TRUSTED { filestem:Rope.ROPE _ ViewerTools.GetSelectionContents[]; redFilename:Rope.ROPE _ Rope.Cat[filestem, "Qfs-red.ais"]; blueFilename:Rope.ROPE _ Rope.Cat[filestem, "Qfs-blu.ais"]; greenFilename:Rope.ROPE _ Rope.Cat[filestem, "Qfs-grn.ais"]; FSFilename:Rope.ROPE _ Rope.Cat[filestem, "-Q", MapFileStem, ".ais"]; ditherViewer.name _ Rope.Cat["Separating: ", filestem, " - Map: ", MapFileStem]; ViewerOps.PaintViewer[ditherViewer, caption, FALSE, NIL]; IF FileExists[FSFilename] THEN { DitherAIS.Stretch[redFilename, greenFilename, blueFilename, FSFilename, MyUnMap ! AIS.Error => { ditherViewer.name _ Rope.Cat[FSFilename, " - Bad file name", " - Map: ", MapFileStem]; ViewerOps.PaintViewer[ditherViewer, caption, FALSE, NIL]; GOTO giveUp}]; }; ditherViewer.name _ Rope.Cat[FSFilename, " - Map: ", MapFileStem]; ViewerOps.PaintViewer[ditherViewer, caption, FALSE, NIL]; EXITS giveUp => NULL; }; Show: ENTRY Menus.MenuProc = TRUSTED { filestem:Rope.ROPE _ ViewerTools.GetSelectionContents[]; FSFilename:Rope.ROPE _ Rope.Cat[filestem, "-", MapFileStem, ".ais"]; IF FileExists[FSFilename] THEN { FileToDisplay _ FSFilename; ditherViewer.name _ Rope.Cat[FileToDisplay, " - Map: ", MapFileStem]; ViewerOps.PaintViewer[ditherViewer, all, FALSE, NIL]; } ELSE { ditherViewer.name _ Rope.Cat[FSFilename, " - Map: ", MapFileStem]; ViewerOps.PaintViewer[ditherViewer, caption, FALSE, NIL]; } }; SetMap:Menus.MenuProc = TRUSTED { SetColorMap[ViewerTools.GetSelectionContents[]]; ditherViewer.name _ Rope.Cat[FileToDisplay, " - Map: ", MapFileStem]; ViewerOps.PaintViewer[ditherViewer, all, FALSE, $MAP]; }; MyMap: DitherAIS.UserMapProc = { rb,gb,bb:DisplayMap.ColorMapSize; palix _ DisplayMap.GetIndex[r, g, b, MapTable]; [rb,gb,bb] _ ColorMap.GetColor[palix]; -- ought to use the table re _ r - rb; ge _ g - gb; be _ b - bb; }; MyQuan: DitherAIS.UserMapProc = { rb,gb,bb:DisplayMap.ColorMapSize; palix _ DisplayMap.GetIndex[r, g, b, MapTable]; [rb,gb,bb] _ ColorMap.GetColor[palix]; -- ought to use the table re _ 0; ge _ 0; be _ 0; }; MyUnMap: DitherAIS.UserUnmapProc = { [r, g, b] _ ColorMap.GetColor[palix]; -- ought to use the table }; << MAIN PROGRAM STARTS HERE>> <<>> DithertestClass:ViewerClasses.ViewerClass _ NEW[ViewerClasses.ViewerClassRec]; ditherViewer:ViewerClasses.Viewer; DithertestClass.paint _ DitherPaint; DithertestClass.get _ DitherGet; ViewerOps.RegisterViewerClass[$Dither, DithertestClass]; SetColorMap[MapFileStem]; ditherViewer _ ViewerOps.CreateViewer[flavor:$Dither, info:[name:"Dither test", column:color], paint:FALSE]; ditherViewer.menu _ Menus.CreateMenu[1]; Menus.AppendMenuEntry[menu:ditherViewer.menu, entry:Menus.CreateEntry [name:"Dither", proc:Dither, fork:TRUE, documentation: "Create 8 bit dithered rgb file from 8-bit r,g and b files"], line:0]; Menus.AppendMenuEntry[menu:ditherViewer.menu, entry:Menus.CreateEntry [name:"Quantize", proc:Quantize, fork:TRUE, documentation: "Create 8 bit rgb file from 8-bit r,g and b files"], line:0]; Menus.AppendMenuEntry[menu:ditherViewer.menu, entry:Menus.CreateEntry [name:"DStretch", proc:Separate, fork:TRUE, documentation: "Create 8-bit r,g and b files from 8 bit rgb file"], line:0]; Menus.AppendMenuEntry[menu:ditherViewer.menu, entry:Menus.CreateEntry [name:"QStretch", proc:QSeparate, fork:TRUE, documentation: "Create 8-bit r,g and b files from 8 bit rgb file"], line:0]; Menus.AppendMenuEntry[menu:ditherViewer.menu, entry:Menus.CreateEntry [name:"Show", proc:Show, fork:TRUE, documentation: "Show rgb file"], line:0]; Menus.AppendMenuEntry[menu:ditherViewer.menu, entry:Menus.CreateEntry [name:"ShowMap", proc:SetMap, fork:FALSE, documentation: "Display the current color map"], line:0]; ViewerOps.ComputeColumn[column: ditherViewer.column]; END.