<> <> <> <> <> <<>> DIRECTORY Commander, FS, Imager, ImagerBasic, ImagerPDExtras, IO, Rope; MebesBitsCedar: CEDAR PROGRAM IMPORTS Commander, FS, Imager, ImagerPDExtras, IO = BEGIN NotMEBESFormat: ERROR = CODE; NewCVContext: PROC [ fileName: Rope.ROPE _ NIL ] RETURNS [ context: Imager.Context ] = BEGIN IF fileName = NIL THEN fileName _ "///temp/plot0.pd"; context _ Imager.Create[$PD, ImagerPDExtras.ColorVersatec[fileName: fileName]]; END; NewDisplayContext: PROC RETURNS [ context: Imager.Context ] = BEGIN context _ Imager.Create[$LFDisplay]; context.ClipView[[0,0,600,808], FALSE]; END; scale: REAL _ 1.0; center: Imager.Pair _ [x: 4.0*4000 -- ResetContext: PROC [ context: Imager.Context ] = BEGIN surface: Imager.IntRectangle; context.Reset[]; surface _ context.GetViewBox[]; context.state.T _ Imager.Scale[1.0]; context.SetColor[Imager.white]; context.MaskRectangle[0, 0, LAST[INTEGER], LAST[INTEGER]]; context.SetColor[Imager.black]; context.TranslateT[x: surface.x+surface.w/2, y: surface.y+surface.h/2]; context.ScaleT[scale]; context.TranslateT[x: -center.x, y: -center.y]; END; CloseCVContext: PROC [ context: Imager.Context ] = {[] _ context.SpecialOp[$Close, NIL]}; ReadMebesPatternFile: PROCEDURE [ fileName: Rope.ROPE, data: REF _ NIL ] = BEGIN s: IO.STREAM = FS.StreamOpen[fileName]; ReadMebesPatternStream[s, data ! UNWIND => s.Close[]]; s.Close[]; END; stripeHeight: INT = 255; ReadMebesPatternStream: PROCEDURE [ s: IO.STREAM, data: REF _ NIL ] = BEGIN blockSize: INT = 2048; block: REF TEXT = NEW[TEXT[blockSize]]; index: INT _ 0; Byte: TYPE = [0..256); ReadMebesWord: PROC RETURNS [ w: INT ] = BEGIN IF index MOD blockSize = 0 THEN BEGIN IF s.GetBlock[block, 0, blockSize] # blockSize THEN ERROR; index _ 0; END; w _ 256*LOOPHOLE[block[index], Byte]+LOOPHOLE[block[index+1], Byte]; index _ index+2; END; stripe: INT; stripeVisible: BOOL _ TRUE; header: ARRAY [0..15] OF INT; FOR i: INT IN [0..16) DO header[i] _ ReadMebesWord[]; ENDLOOP; IF (header[0] # 1003B) AND (header[0] # 1006B) OR header[6] # 6 OR header[13] # 2 THEN ERROR NotMEBESFormat; DO t1: INT _ ReadMebesWord[]; SELECT t1 MOD 100B FROM 16 => BEGIN -- Manhattan Rectangle IF stripeVisible THEN BEGIN x, width, y, height: INT; height _ t1/100B + 1; width _ 256*LOOPHOLE[block[index], Byte]+LOOPHOLE[block[index+1], Byte]; x _ 256*LOOPHOLE[block[index+2], Byte]+LOOPHOLE[block[index+3], Byte]; y _ 256*LOOPHOLE[block[index+4], Byte]+LOOPHOLE[block[index+5], Byte]; InsertRectangle[x, width, stripe*stripeHeight+y, height, data]; END; index _ index+6; END; 17 => BEGIN -- Parallelogram x, width, leftEdge: INT; y, height, tmp1: INT; height _ t1/100B + 1; width _ ReadMebesWord[]; x _ ReadMebesWord[]; y _ ReadMebesWord[]; leftEdge _ ReadMebesWord[]; tmp1 _ ReadMebesWord[]; width _ width + (tmp1 MOD 40B)*200000B; tmp1 _ tmp1/40B; x _ x + (tmp1 MOD 40B)*200000B; tmp1 _ tmp1/40B; leftEdge _ leftEdge + (tmp1 MOD 100B)*200000B + (IF tmp1/40B # 0 THEN 37760000000B ELSE 0); InsertTrapezoid[x, width, stripe*stripeHeight+y, height, leftEdge, leftEdge, data]; END; 18 => BEGIN -- Trapezoid 1 x, width, leftEdge: INT; y, height, tmp1: INT; height _ t1/100B + 1; width _ ReadMebesWord[]; x _ ReadMebesWord[]; y _ ReadMebesWord[]; leftEdge _ ReadMebesWord[]; tmp1 _ ReadMebesWord[]; width _ width + (tmp1 MOD 40B)*200000B; tmp1 _ tmp1/40B; x _ x + (tmp1 MOD 40B)*200000B; tmp1 _ tmp1/40B; leftEdge _ leftEdge + (tmp1 MOD 100B)*200000B + (IF tmp1/40B # 0 THEN 37760000000B ELSE 0); InsertTrapezoid[x, width, stripe*stripeHeight+y, height, leftEdge, 0, data]; END; 19 => BEGIN -- Trapezoid 2 x, width, rightEdge: INT; y, height, tmp1: INT; height _ t1/100B + 1; width _ ReadMebesWord[]; x _ ReadMebesWord[]; y _ ReadMebesWord[]; rightEdge _ ReadMebesWord[]; tmp1 _ ReadMebesWord[]; width _ width + (tmp1 MOD 40B)*200000B; tmp1 _ tmp1/40B; x _ x + (tmp1 MOD 40B)*200000B; tmp1 _ tmp1/40B; rightEdge _ rightEdge + (tmp1 MOD 100B)*200000B + (IF tmp1/40B # 0 THEN 37760000000B ELSE 0); InsertTrapezoid[x, width, stripe*stripeHeight+y, height, 0, rightEdge, data]; END; 20 => BEGIN -- Trapezoid 3 x, width, leftEdge, rightEdge: INT; y, height, tmp1: INT; height _ t1/100B + 1; width _ ReadMebesWord[]; x _ ReadMebesWord[]; y _ ReadMebesWord[]; leftEdge _ ReadMebesWord[]; rightEdge _ ReadMebesWord[]; tmp1 _ ReadMebesWord[]; width _ width + (tmp1 MOD 40B)*200000B; tmp1 _ tmp1/40B; x _ x + (tmp1 MOD 40B)*200000B; tmp1 _ tmp1/40B; leftEdge _ leftEdge + (tmp1 MOD 100B)*200000B + (IF tmp1/40B # 0 THEN 37760000000B ELSE 0); tmp1 _ y/2000B; y _ y MOD 2000B; rightEdge _ rightEdge + (tmp1 MOD 100B)*200000B + (IF tmp1/40B # 0 THEN 37760000000B ELSE 0); InsertTrapezoid[x, width, stripe*stripeHeight+y, height, leftEdge, rightEdge, data]; END; 9 => index _ blockSize; -- end of buffer 4 => RETURN; -- end of drawing 7 => BEGIN -- start of stripe stripe _ t1/256; stripeVisible _ StripeVisible[stripe, data]; END; 8 => NULL; -- end of stripe 3, 6 => FOR i: INT IN [1..16) DO [] _ ReadMebesWord[]; ENDLOOP; -- header ENDCASE => ERROR NotMEBESFormat; ENDLOOP; END; seeEverything: BOOL _ FALSE; StripeVisible: PROC [ stripe: INT, data: REF _ NIL ] RETURNS [ visible: BOOL ] = BEGIN SELECT TRUE FROM seeEverything => visible _ TRUE; data = NIL => visible _ FALSE; ENDCASE => BEGIN context: Imager.Context = NARROW[data]; visible _ context.TestRectangle[x: 0, y: stripe*stripeHeight, w: LAST[INTEGER], h: stripeHeight] # invisible; END; END; InsertTrapezoid: PROCEDURE [ x, width, y, height, leftEdge, rightEdge: INT, data: REF _ NIL] = BEGIN FOR pos: INT IN [0..height) DO le: INT = x + (pos*leftEdge)/height; re: INT = x + (pos*rightEdge)/height + width - 1; left: INT = (le + 8)/16; right: INT = (re + 7)/16; IF right < left THEN LOOP; InsertRectangle[left, right - left + 1, y + pos, 1, data]; ENDLOOP; END; InsertRectangle: PROCEDURE [x, width, y, height: INT, data: REF _ NIL] = BEGIN IF width # 0 AND height # 0 AND data # NIL THEN BEGIN context: Imager.Context = NARROW[data]; context.IntegerMaskRectangle[x: x, y: y, w: width, h: height]; END; END; DoNothing: PROC [ cmd: Commander.Handle ] RETURNS [ result: REF _ NIL, msg: Rope.ROPE _ NIL ] -- Commander.CommandProc -- = {NULL}; Commander.Register["MebesBitsCedar", DoNothing, "Plots .mebes files with the Imager. User interface through Cedar interpreter."]; END.