<> <> <> <> <> <<>> <> <<>> DIRECTORY ConvertToRasters, Imager, Interpress, ImagerSmoothContext, ImagerSmoothContextExtras, ImagerSample, ImagerPixel, Rope, PrintColor, ImagerBackdoor, CedarProcess, VM, ConvertRasterObject, SF, ImagerPixelArray, ImagerInterpress, ImagerColor, ImagerBox, ImagerColorFns, ImagerTransformation, ImagerMaskCapture, VMSideDoor; ConvertToRastersImpl: CEDAR PROGRAM IMPORTS Interpress, ImagerSmoothContext, ImagerPixel, ImagerSmoothContextExtras, Imager, ImagerBackdoor, CedarProcess, VM, ConvertRasterObject, Rope, ImagerPixelArray, ImagerInterpress, ImagerColor, ImagerColorFns, ImagerTransformation, ImagerMaskCapture, ImagerBox, VMSideDoor EXPORTS ConvertToRasters ~ BEGIN OPEN ConvertToRasters; ROPE: TYPE ~ Rope.ROPE; Context: TYPE ~ Imager.Context; PixelMap: TYPE ~ ImagerPixel.PixelMap; ColorCorrection: TYPE = PrintColor.ColorCorrection; <> Convert: PUBLIC PROC [action: PROC[Imager.Context], rasterSpec: RasterSpec, proc: RasterProc] RETURNS [nBands: NAT] = { pm: PixelMap _ NIL; inch: REAL = 0.0254; -- inches->meters conversion factor nLines: NAT ~ rasterSpec.sSize; line: NAT _ 0; stop: BOOLEAN _ FALSE; ctx: Context; samplesPerPixel: NAT _ SELECT rasterSpec.type FROM rgb => 3, gray => 1, cmyk => 4, ENDCASE => ERROR; pixels: ImagerPixel.PixelBuffer _ ImagerPixel.ObtainScratchPixels[samplesPerPixel, rasterSpec.fSize]; CedarProcess.SetPriority[background]; nBands _ 1; WHILE line VMSideDoor.rmPages/4 THEN failed _ TRUE ELSE [ctx, pm] _ CreateContext[rasterSpec, pm, 1 ! VM.CantAllocate => {failed _ TRUE; CONTINUE}]; IF failed THEN { nBands _ nBands+1; ctx _ NIL; } ELSE EXIT; ENDLOOP; Imager.TranslateT[ctx, [0,(line-nLines+bandSize)]]; Imager.DoSave[ctx, do]; FOR j: NAT IN [0..bandLines) DO ImagerPixel.GetPixels[self: pm, initIndex: [s: j, f: 0], delta: [s: 0, f: 1], pixels: pixels, start: 0, count: rasterSpec.fSize]; IF proc[pixels] THEN {stop _ TRUE; EXIT}; ENDLOOP; IF stop THEN EXIT; line _ line+bandLines; ENDLOOP; ImagerPixel.ReleaseScratchPixels[pixels]; RETURN[nBands]; }; <<>> ToPixelMap: PUBLIC PROC [action: PROC[Imager.Context], rasterSpec: RasterSpec] RETURNS[PixelMap] = { ctx: Context; pm: PixelMap; CedarProcess.SetPriority[background]; [ctx, pm] _ CreateContext[rasterSpec, NIL, 1]; action[ctx]; RETURN[pm]; }; CreateContext: PROC [rs: RasterSpec, pm: PixelMap _ NIL, defaultScale: REAL] RETURNS [Context, PixelMap] ~ { context: Context _ ImagerSmoothContext.Create[ size: [s: rs.sSize, f: rs.fSize], scanMode: rs.scanMode, initialScale: defaultScale, cacheFonts: TRUE, surfaceUnitsPerPixel: rs.surfaceUnitsPerPixel]; samplesPerPixel: NAT; components: LIST OF ATOM ; maxSample: ImagerPixel.PixelProc = {RETURN[rs.maxPixel]}; SELECT rs.type FROM gray => {samplesPerPixel _ 1; components _ LIST[$Intensity]}; rgb => {samplesPerPixel _ 3; components _ LIST[$Red, $Green, $Blue]}; cmyk => {samplesPerPixel _ 4; components _ LIST[$Cyan, $Magenta, $Yellow, $Black]}; ENDCASE => ERROR; IF pm=NIL THEN --allows us to reuse the pixelmap pm _ ImagerPixel.NewPixelMap[samplesPerPixel, [[0,0],[s: rs.sSize, f: rs.fSize]], maxSample]; ImagerSmoothContext.SetOutputBuffer[context, pm, components]; ImagerSmoothContextExtras.SetColorCorrection[context, rs.colorCorrection]; Imager.SetColor[context, Imager.MakeGray[0]]; --set the master to white Imager.MaskRectangle[context, ImagerBackdoor.GetBounds[context]]; Imager.SetColor[context, Imager.MakeGray[1]]; --set the current color to black RETURN[context, pm]; }; <> FromIP: PUBLIC PROC [ip: ROPE, ppiF,ppiS: REAL, page: NAT _ 1, rasterSpec: RasterSpec, proc: RasterProc, x,y,w,h: REAL _ 0] RETURNS[nBands: NAT] = { log: Interpress.LogProc = {}; master: Interpress.Master _ Interpress.Open[ip, log]; do: PROC[context: Imager.Context] = { Imager.Scale2T[context, [x: ppiF/metersPerInch, y: ppiS/metersPerInch]]; IF w#0 AND h#0 THEN Imager.ClipRectangle[context, [0,0,w*metersPerInch,h*metersPerInch], FALSE]; Imager.TranslateT[context, [-x*metersPerInch,-y*metersPerInch]]; Interpress.DoPage[master, page, context, log]; }; nBands _ Convert[do, rasterSpec, proc]; Interpress.Close[master]; }; <<>> metersPerInch: REAL = 0.0254; FromIPToPixelMap: PUBLIC PROC [ip: ROPE, ppiF,ppiS: REAL, page: NAT _ 1, rasterSpec: RasterSpec, x,y,w,h: REAL _ 0] RETURNS[PixelMap] = { log: Interpress.LogProc = {}; master: Interpress.Master _ Interpress.Open[ip, log]; do: PROC[context: Imager.Context] = { Imager.Scale2T[context, [x: ppiF/metersPerInch, y: ppiS/metersPerInch]]; IF w#0 AND h#0 THEN Imager.ClipRectangle[context, [0,0,w*metersPerInch,h*metersPerInch], FALSE]; Imager.TranslateT[context, [-x*metersPerInch,-y*metersPerInch]]; Interpress.DoPage[master, page, context, log]; }; pm: PixelMap _ ToPixelMap[do, rasterSpec]; Interpress.Close[master]; RETURN[pm]; }; AISFromPixelMap: PUBLIC PROC[aisRoot: ROPE, pm: PixelMap] = { SELECT pm.samplesPerPixel FROM 1 => ConvertRasterObject.AISFromPixelMap[Rope.Cat[aisRoot, ".ais"], pm, 0]; 3 => { ConvertRasterObject.AISFromPixelMap[Rope.Cat[aisRoot, "-red.ais"], pm, 0]; ConvertRasterObject.AISFromPixelMap[Rope.Cat[aisRoot, "-grn.ais"], pm, 1]; ConvertRasterObject.AISFromPixelMap[Rope.Cat[aisRoot, "-blu.ais"], pm, 2]; }; 4 => { ConvertRasterObject.AISFromPixelMap[Rope.Cat[aisRoot, "-cyan.ais"], pm, 0]; ConvertRasterObject.AISFromPixelMap[Rope.Cat[aisRoot, "-magenta.ais"], pm, 1]; ConvertRasterObject.AISFromPixelMap[Rope.Cat[aisRoot, "-yellow.ais"], pm, 2]; ConvertRasterObject.AISFromPixelMap[Rope.Cat[aisRoot, "-black.ais"], pm, 3]; }; ENDCASE => ERROR; }; pointsPerInch: REAL = Imager.pointsPerInch; IPToRasterIP: PUBLIC PROC[ipIn, ipOut: ROPE, ppiF,ppiS: REAL, page: NAT _ 1, rasterSpec: RasterSpec, x,y,w,h: REAL _ 0, tx,ty: REAL _ 0] = { <> <> pm: PixelMap _ FromIPToPixelMap[ipIn, ppiF,ppiS, page, rasterSpec, x,y,w,h]; pa: ImagerPixelArray.PixelArray _ ImagerPixelArray.FromPixelMap[pm, pm.box, rasterSpec.scanMode, TRUE]; ip: ImagerInterpress.Ref _ ImagerInterpress.Create[ipOut]; op: ImagerColor.ColorOperator _ SELECT rasterSpec.type FROM rgb => ImagerColor.NewColorOperatorRGB[maxIn: rasterSpec.maxPixel+1], gray => ImagerColor.NewColorOperatorGrayLinear[sWhite: 255, sBlack: 0, sampleTableSize: rasterSpec.maxPixel+1, sampleTableProc: NIL], cmyk => ImagerColorFns.NewColorOperatorCMYK[maxIn: rasterSpec.maxPixel+1], ENDCASE => ERROR; action: PROC[context: Imager.Context] = { Imager.TranslateT[context, [tx*pointsPerInch,ty*pointsPerInch]]; Imager.SetSampledColor[context, pa, NIL, op]; Imager.MaskRectangle[context, [x: 0, y: 0,w: rasterSpec.fSize, h: rasterSpec.sSize]]; }; ImagerInterpress.DoPage[ip, action, Imager.metersPerPoint]; ImagerInterpress.Close[ip]; }; IPWindow: PUBLIC PROC[ip: ROPE, page: NAT _ 1, tol: REAL _ 0.02] RETURNS [x,y,w,h: REAL] = { ppi: REAL _ 1/tol; m: Imager.Transformation _ ImagerTransformation.Scale[ppi/0.0254]; n: Imager.Transformation _ ImagerTransformation.PostScale[m, 0.0254]; -- back to inches master: Interpress.Master _ Interpress.Open[ip, NIL]; Operator: PROC [context: Imager.Context] ~ { Imager.SetColor[context, Imager.black]; Imager.SetStrokeEnd[context, square]; Imager.SetStrokeWidth[context, 0.0]; Imager.SetAmplifySpace[context, 1.0]; Interpress.DoPage[master, page, context, NIL]; }; b: SF.Box _ ImagerMaskCapture.CaptureBounds[Operator, m ! ImagerMaskCapture.Cant => RESUME]; r: ImagerBox.Rectangle _ ImagerTransformation.InverseTransformRectangle[ n, ImagerBox.RectangleFromBox[[b.min.s, b.min.f, b.max.s, b.max.f]]]; RETURN[x: r.x, y: r.y, w: r.w, h: r.h]; }; CreateRasterSpec: PUBLIC PROC [type: RasterType, fSize, sSize: NAT, colorCorrection: ColorCorrection, surfaceUnitsPerPixel: NAT _ 5, scanMode: Imager.ScanMode _ Imager.defaultScanMode,maxPixel: NAT _ 255] RETURNS [RasterSpec] ~ { RETURN[[type, fSize, sSize, surfaceUnitsPerPixel, Imager.defaultScanMode,colorCorrection,maxPixel]]; }; END.