<<>> <> <> <> <> DIRECTORY AISFileFormat, AISIO, Basics, CtBasic, CtFile, Commander, FileNames, FS, Imager, ImagerBackdoor, ImagerBox, ImagerColor, ImagerPixel, ImagerPixelArray, ImagerSample, ImagerTransformation, InterpressInterpreter, IO, MessageWindow, Process, RefText, Rope, SF; CtFileImpl: CEDAR PROGRAM IMPORTS AISIO, Basics, CtBasic, FileNames, FS, ImagerSample, IO, MessageWindow, Process, RefText, Rope, SF EXPORTS CtFile ~ BEGIN SampleMap: TYPE ~ CtBasic.SampleMap; SampleMaps: TYPE ~ CtBasic.SampleMaps; PixelArray: TYPE ~ CtBasic.PixelArray; RealPair: TYPE ~ CtBasic.RealPair; FileInfo: TYPE ~ CtFile.FileInfo; FileType: TYPE ~ CtFile.FileType; Context: TYPE ~ Imager.Context; Box: TYPE ~ ImagerSample.Box; Vec: TYPE ~ ImagerSample.Vec; SampleBuffer: TYPE ~ ImagerSample.SampleBuffer; RasterSampleMap: TYPE ~ ImagerSample.RasterSampleMap; STREAM: TYPE ~ IO.STREAM; ROPE: TYPE ~ Rope.ROPE; <> WriteMapsToAIS: PUBLIC PROC [maps: SampleMaps, name: ROPE, stream: STREAM ¬ NIL] ~ { saving: ROPE ¬ IO.PutFLR["Saving (%g %g %g %g) ", LIST[IO.int[maps.x], IO.int[maps.y], IO.int[maps.w], IO.int[maps.h]]]; SELECT maps.bpp FROM 1, 8 => { IF stream # NIL THEN IO.PutF[stream, "%g%g . . . ", IO.rope[saving], IO.rope[name]]; SaveSampleMapToFile[maps[0].map, name]; IF stream # NIL THEN IO.PutRope[stream, "done\n"]; }; 24 => { IF stream # NIL THEN IO.PutRope[stream, saving]; IF stream # NIL THEN IO.PutF1[stream, "%g-red.ais . . . ", IO.rope[name]]; SaveSampleMapToFile[maps[0].map, Rope.Concat[name, "-red.ais"]]; IF stream # NIL THEN IO.PutF1[stream, "%g-grn.ais . . . ", IO.rope[name]]; SaveSampleMapToFile[maps[1].map, Rope.Concat[name, "-grn.ais"]]; IF stream # NIL THEN IO.PutF1[stream, "%g-blu.ais . . . ", IO.rope[name]]; SaveSampleMapToFile[maps[2].map, Rope.Concat[name, "-blu.ais"]]; IF stream # NIL THEN IO.PutRope[stream, "done\n"]; }; ENDCASE => NULL; }; <<>> SaveSampleMapToFile: PUBLIC PROC [ source: SampleMap, aisFileName: ROPE, comment: ROPE ¬ NIL] ~ { AISIO.Write[aisFileName, source, NIL, NIL, LIST[comment]]; }; <> Error: PUBLIC ERROR [reason: ROPE] = CODE; ReadFile: PUBLIC PROC [ baseName: ROPE, aisX, aisY: CARDINAL ¬ 0, aisW, aisH: CARDINAL ¬ LAST[CARDINAL], maps: SampleMaps ¬ NIL] RETURNS [ret: SampleMaps] ~ { i: FileInfo ¬ Parse[baseName]; ret ¬ SELECT i.type FROM bw => Read8BitFile[i.names[0], aisX, aisY, aisW, aisH, maps], color => Read24BitFile[i.names[0], i.names[1], i.names[2], aisX, aisY, aisW, aisH, maps], ENDCASE => NIL; }; ReadFileAndClip: PROC [name: ROPE, x, y, w, h: CARDINAL] RETURNS [map: SampleMap] ~ { V: PROC [c: AISFileFormat.Card16] RETURNS [CARD] ~ {RETURN[Basics.Card16FromH[c]]}; i: AISIO.Info ¬ AISIO.ReadInfo[name]; w ¬ MIN[w, V[i.raster.scanLength]]; h ¬ MIN[h, V[i.raster.scanCount]]; map ¬ ImagerSample.ZeroOrigin[ImagerSample.Clip[AISIO.Read[name], [[y, x], [y+h, x+w]]]]; }; Read8BitFile: PUBLIC PROC [ name: ROPE, aisX, aisY: CARDINAL ¬ 0, aisW, aisH: CARDINAL ¬ LAST[CARDINAL], maps: SampleMaps ¬ NIL] RETURNS [ret: SampleMaps] ~ { bw: SampleMap ¬ ReadFileAndClip[name, aisX, aisY, aisW, aisH]; size: SF.Vec ¬ ImagerSample.GetSize[bw]; IF (ret ¬ maps) = NIL OR maps.w < size.f OR maps.h < size.s THEN ret ¬ CtBasic.CreateMaps[8, 0, 0, size.f, size.s]; ImagerSample.BasicTransfer[ret[0].map, bw, ret.box.min,, size]; }; Read24BitFile: PUBLIC PROC [ rName, gName, bName: ROPE, aisX, aisY: CARDINAL ¬ 0, aisW, aisH: CARDINAL ¬ LAST[CARDINAL], maps: SampleMaps ¬ NIL] RETURNS [ret: SampleMaps] ~ { Union: PROC [box1, box2: Box] RETURNS [u: Box] ~ { u ¬ [SF.Min[box1.min, box2.min], SF.Max[box1.max, box2.max]]; }; GetBox: PROC [m: SampleMap] RETURNS [b: Box] ~ {b ¬ ImagerSample.GetBox[m]}; red: SampleMap ¬ ReadFileAndClip[rName, aisX, aisY, aisW, aisH]; grn: SampleMap ¬ ReadFileAndClip[gName, aisX, aisY, aisW, aisH]; blu: SampleMap ¬ ReadFileAndClip[bName, aisX, aisY, aisW, aisH]; size: SF.Vec ¬ SF.Size[Union[GetBox[red], Union[GetBox[grn], GetBox[blu]]]]; IF (ret ¬ maps) = NIL OR maps.bpp # 24 OR maps.w < size.f OR maps.h < size.s THEN ret ¬ CtBasic.CreateMaps[24, 0, 0, size.f, size.s]; ImagerSample.BasicTransfer[ret[0].map, red, ret.box.min,, size]; ImagerSample.BasicTransfer[ret[1].map, grn, ret.box.min,, size]; ImagerSample.BasicTransfer[ret[2].map, blu, ret.box.min,, size]; }; ReadPixelArray: PUBLIC PROC [aisFileName: ROPE] RETURNS [pa: PixelArray] ~ { pa ¬ CtBasic.PixelArrayFromSampleMap[AISIO.Read[aisFileName]]; }; <> ReadDumpFile: PUBLIC PROC [ fileName: ROPE, maps: SampleMaps ¬ NIL, ascii: BOOL ¬ FALSE, nChannels: NAT ¬ 0, width, height: NAT ¬ 0, contiguous: BOOL ¬ TRUE] RETURNS [SampleMaps] ~ { LineStatus: PROC [y: NAT] ~ { Process.CheckForAbort[]; IF y MOD 10 = 0 OR y = height-1 THEN MessageWindow.Append[IO.PutFR["line %g/%g", IO.int[y], IO.int[height-1]], TRUE]; }; ReadLine: PROC [y: NAT, b: SampleBuffer, m: SampleMap] ~ { LineStatus[y]; IF ascii THEN FOR x: NAT IN [0..width) DO b[x] ¬ IO.GetInt[s]; ENDLOOP ELSE FOR x: NAT IN [0..width) DO b[x] ¬ IO.GetByte[s]; ENDLOOP; ImagerSample.PutSamples[m, [y, 0],, b,, width]; }; box: SF.Box; bw, r, g, b: SampleBuffer; bwMap, rMap, gMap, bMap: SampleMap; s: STREAM ¬ FS.StreamOpen[fileName ! FS.Error => Error["Can't open file."]]; IF s = NIL THEN Error["Can't open file."]; IF nChannels = 0 OR width = 0 OR height = 0 THEN { IF ascii THEN { nChannels ¬ IO.GetInt[s]; width ¬ IO.GetInt[s]; height ¬ IO.GetInt[s]; } ELSE { nChannels ¬ INT[Basics.Card16FromH[IO.GetHWord[s]]]; width ¬ INT[Basics.Card16FromH[IO.GetHWord[s]]]; height ¬ INT[Basics.Card16FromH[IO.GetHWord[s]]]; }; }; box ¬ [[0, 0], [height, width]]; IF maps = NIL OR maps.w < width OR maps.h < height OR maps.nChannels # nChannels THEN maps ¬ CtBasic.CreateMaps[nChannels*8, 0, 0, width, height]; IF nChannels = 1 THEN { bwMap ¬ ImagerSample.ObtainScratchMap[box, 8]; bw ¬ ImagerSample.NewSamples[width]; } ELSE { rMap ¬ ImagerSample.ObtainScratchMap[box, 8]; gMap ¬ ImagerSample.ObtainScratchMap[box, 8]; bMap ¬ ImagerSample.ObtainScratchMap[box, 8]; r ¬ ImagerSample.NewSamples[width]; b ¬ ImagerSample.NewSamples[width]; g ¬ ImagerSample.NewSamples[width]; }; SELECT TRUE FROM nChannels = 1 => FOR y: NAT IN [0..height) DO ReadLine[y, bw, bwMap]; ENDLOOP; nChannels = 3 AND contiguous => FOR y: NAT IN [0..height) DO LineStatus[y]; IF ascii THEN FOR x: NAT IN [0..width) DO r[x] ¬ IO.GetInt[s]; g[x] ¬ IO.GetInt[s]; b[x] ¬ IO.GetInt[s]; ENDLOOP ELSE FOR x: NAT IN [0..width) DO r[x] ¬ IO.GetByte[s]; g[x] ¬ IO.GetByte[s]; b[x] ¬ IO.GetByte[s]; ENDLOOP; ImagerSample.PutSamples[rMap, [y, 0],, r,, width]; ImagerSample.PutSamples[gMap, [y, 0],, g,, width]; ImagerSample.PutSamples[bMap, [y, 0],, b,, width]; ENDLOOP; nChannels = 3 AND NOT contiguous => { FOR y: NAT IN [0..height) DO ReadLine[y, r, rMap]; ENDLOOP; FOR y: NAT IN [0..height) DO ReadLine[y, g, gMap]; ENDLOOP; FOR y: NAT IN [0..height) DO ReadLine[y, b, bMap]; ENDLOOP; }; ENDCASE; IF nChannels = 1 THEN { ImagerSample.Transfer[dst: maps[0].map, src: bwMap]; ImagerSample.ReleaseScratchMap[bwMap]; } ELSE { <> <> <> ImagerSample.Transfer[maps[0].map, rMap]; ImagerSample.Transfer[maps[1].map, gMap]; ImagerSample.Transfer[maps[2].map, bMap]; ImagerSample.ReleaseScratchMap[rMap]; ImagerSample.ReleaseScratchMap[gMap]; ImagerSample.ReleaseScratchMap[bMap]; }; RETURN[maps]; }; <> ViewFile: PUBLIC PROC [ maps: SampleMaps, baseName: ROPE, center, noPlace: BOOL ¬ FALSE, aisX, aisY: CARDINAL ¬ 0, aisW, aisH: CARDINAL ¬ LAST[CARDINAL]] RETURNS [error: ROPE ¬ NIL] ~ { <> <> <> <<[type, r, g, b] _ Parse[baseName];>> <> < ViewIpFile[r],>> < IF maps.bpp = 24>> <> <> < IF maps.bpp = 24>> <> <> < Rope.Cat["Bad name: ", baseName];>> <<};>> <> }; View8BitFile: PUBLIC PROC [ maps: SampleMaps, name: ROPE, center, noPlace: BOOL ¬ FALSE, aisX, aisY: CARDINAL ¬ 0, aisW, aisH: CARDINAL ¬ LAST[CARDINAL]] RETURNS [error: ROPE ¬ NIL] ~ { <> <> < {error _ reason; GOTO Bad}];>> <> <> <> <> <
shift _ [(h-size.s)/2, (w-size.f)/2];>> < map _ ImagerSample.Shift[map, [-place.yBottom, -place.xLeft]];>> <> <> <> <> <> <> <> <> <> <<};>> < error _ "File isn't 1 or 8 bits per pixel.";>> <> < NULL;>> }; View24BitFile: PUBLIC PROC [ maps: SampleMaps, rName, gName, bName: ROPE, center, noPlace: BOOL ¬ FALSE, aisX, aisY: CARDINAL ¬ 0, aisW, aisH: CARDINAL ¬ LAST[CARDINAL]] RETURNS [error: ROPE ¬ NIL] ~ { <> <> <> < {error _ reason; GOTO Bad};>> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <