File: SVImageImpl.mesa
Last edited by Bier on August 7, 1987 3:24:08 pm PDT
Copyright © 1984 by Xerox Corporation. All rights reserved.
Contents: Routines for manipulating 24 bit per pixel color images
DIRECTORY
AIS, AtomButtonsTypes, Feedback, FS, Imager, ImagerColor, ImagerColorPrivate, ImagerPixelArray, ImagerTransformation, Real, Rope, SVShading, SV2d, SVBasicTypes, SVFiles, SVImage, SVModelTypes, SVVector2d, ViewerClasses;
SVImageImpl: CEDAR PROGRAM
IMPORTS AIS, Feedback, FS, Imager, ImagerColor, ImagerColorPrivate, ImagerPixelArray, ImagerTransformation, Real, Rope, SVShading, SVFiles, SVVector2d
EXPORTS SVImage =
BEGIN
BoundBox: TYPE = REF BoundBoxObj;
BoundBoxObj: TYPE = SVBasicTypes.BoundBoxObj;
Color: TYPE = Imager.Color;
FeedbackData: TYPE = AtomButtonsTypes.FeedbackData;
FRef: TYPE = AIS.FRef;
Image: TYPE = REF ImageObj;
ImageObj: TYPE = SVImage.ImageObj;
Point2d: TYPE = SV2d.Point2d;
Viewer: TYPE = ViewerClasses.Viewer;
WRef: TYPE = AIS.WRef;
IsEven: PRIVATE PROC [n: NAT] RETURNS [BOOL] = {
RETURN [(n/2)*2 = n];
};
RoundDown: PRIVATE PROC [r: REAL] RETURNS [rDown: INTEGER] = {
find the largest integer less than r (or equal to if r is positive).
IF r = 0 THEN RETURN[0];
IF r > 0 THEN {
rDown ← Real.Fix[r];
RETURN}
ELSE {
r ← -r;-- make it positive
rDown ← Real.Fix[r];
rDown ← rDown + 1;
rDown ← - rDown;
};
};
RoundUp: PRIVATE PROC [r: REAL] RETURNS [rUp: INTEGER] = {
find the smallest integer greater than r (or equal to if r is negative).
IF r = 0 THEN RETURN[0];
IF r > 0 THEN {
rUp ← Real.Fix[r];
rUp ← rUp + 1;
RETURN}
ELSE {
r ← -r;-- make it positive
rUp ← Real.Fix[r];
rUp ← - rUp;
};
};
CountSamples: PUBLIC PROC [realMinX, realMinY, realMaxX, realMaxY, resolution: REAL] RETURNS [numberOfSamplesX, numberOfSamplesY: NAT] = {
We are given a minimum bounding box to be filled. Calculate the number of samples in the x direction as follows: Find the number of samples which will just fit in the alloted x extent. Now add one (Since <number of inter-sample distances> + 1 = number of samples, we actually add two). If the resulting number is odd, add 1 again. Do the same for the y direction.
(Because of unimplemented features in some of the printing software, it is useful to round all ais file sizes to even numbers of pixels.)
xExtent/72 = x extent in inches. (xExtent/72)*resolution = samples which will fit. Round this down to the nearest integer.
xExtent, yExtent, realSamplesX, realSamplesY: REAL;
xExtent ← realMaxX - realMinX; -- x extent in screen dots
yExtent ← realMaxY - realMinY; -- y extent in screen dots
realSamplesX ← (xExtent*resolution)/72.0;
realSamplesY ← (yExtent*resolution)/72.0;
numberOfSamplesX ← Real.Fix[realSamplesX] + 2;
numberOfSamplesY ← Real.Fix[realSamplesY] + 2;
IF NOT IsEven[numberOfSamplesX] THEN numberOfSamplesX ← numberOfSamplesX + 1;
IF NOT IsEven[numberOfSamplesY] THEN numberOfSamplesY ← numberOfSamplesY + 1;
}; -- end of CountSamples
OpenImage: PUBLIC PROC [aisRope: Rope.ROPE, bAndWOnly: BOOL, realMinX, realMinY, realMaxX, realMaxY, resolution: REAL, feedback: FeedbackData] RETURNS [I: Image, numberOfSamplesX, numberOfSamplesY: NAT] = {
Bound box is in camera coordinates
f: AIS.FRef;
raster: AIS.Raster;
I ← NEW[ImageObj];
[numberOfSamplesX, numberOfSamplesY] ← CountSamples[realMinX, realMinY, realMaxX, realMaxY, resolution];
raster ← NEW[AIS.RasterPart ← [numberOfSamplesY, numberOfSamplesX, rd, 8, -1, 65535]];
IF bAndWOnly THEN {
I.bAndWOnly ← TRUE;
f ← AIS.CreateFile[aisRope, raster];
I.bwWindow ← AIS.OpenWindow[f];
Feedback.PutF[feedback, oneLiner, " New %g created.", [rope[aisRope]]];
}
ELSE {
redRope: Rope.ROPE ← Rope.Concat[SVFiles.FilenameMinusExtension[aisRope], "-red.ais"];
greenRope: Rope.ROPE ← Rope.Concat[SVFiles.FilenameMinusExtension[aisRope], "-grn.ais"];
blueRope: Rope.ROPE ← Rope.Concat[SVFiles.FilenameMinusExtension[aisRope], "-blu.ais"];
bwRope: Rope.ROPE ← aisRope;
I.bAndWOnly ← FALSE;
f ← AIS.CreateFile[redRope, raster];
I.redWindow ← AIS.OpenWindow[f];
f ← AIS.CreateFile[greenRope, raster];
I.greenWindow ← AIS.OpenWindow[f];
f ← AIS.CreateFile[blueRope, raster];
I.blueWindow ← AIS.OpenWindow[f];
f ← AIS.CreateFile[bwRope, raster];
I.bwWindow ← AIS.OpenWindow[f];
Feedback.PutF[feedback, end, " New color %g's created.", [rope[aisRope]]];
};
}; -- end of OpenImage
DeleteFile: PROC [name: Rope.ROPE, feedback: FeedbackData] = {
AIS.DeleteFile[name];
Feedback.PutF[feedback, begin, "%g... ", [rope[name]]];
}; -- end of DeleteFile
Truncate: PROC [value: REAL] RETURNS [m: CARDINAL] = {
Truncate the REAL value to 0-255
SELECT value FROM
<0 => ERROR;
>255.0 => m ← 255;
ENDCASE => m ← Real.Fix[value];
};
PutImage2: PROC [I: Image, i, j: INTEGER, color: Color, minX, maxX, minY, maxY: INTEGER] = {
i is row. j is column. i = maxY maps to 0. i = minY maps to maxY-minY.
j = maxX maps to maxX-minX. j = minX maps to 0.
intRed, intGreen, intBlue, intIntensity: CARDINAL;
red, green, blue, intensity: REAL;
[red, green, blue] ← SVShading.ExtractRGB[color];
intensity ← ImagerColorPrivate.IntensityFromColor[NARROW[color]];
intRed ← Truncate[red*255.0];
intGreen ← Truncate[green*255.0];
intBlue ← Truncate[blue*255.0];
intIntensity ← Truncate[intensity*255.0];
AIS.WriteSample[I.redWindow, intRed, maxY - i, j - minX];
AIS.WriteSample[I.greenWindow, intGreen, maxY - i, j - minX];
AIS.WriteSample[I.blueWindow, intBlue, maxY - i, j - minX];
AIS.WriteSample[I.bwWindow, intIntensity, maxY - i, j - minX];
};
RGBTo8Bits: PUBLIC PROC [r,g,b: REAL] RETURNS [red, green, blue, black: NAT] = {
k: REAL;
red ← Real.Fix[r*255.0];
green ← Real.Fix[g*255.0];
blue ← Real.Fix[b*255.0];
k ← IntensityFromRGB[r,g,b];
black ← Real.Fix[k*255.0];
};
IntensityFromRGB: PROC [r,g,b: REAL] RETURNS [REAL] ~ {
Stolen from ImagerColorImpl.mesa by Bier on March 26, 1986.
Y: REAL ~ 0.30*r+0.59*g+0.11*b;
IF Y<=0 THEN RETURN[0];
IF Y>=1 THEN RETURN[1];
RETURN[Y];
};
PutImage: PUBLIC PROC [I: Image, i, j: INTEGER, color: Color, xSamples, ySamples: NAT] = {
i is row. j is column. i = ySamples maps to 0. i = 1 maps to ySamples-1.
j = xSamples-1 maps to xSamples-1. j = 0 maps to 0.
IF I.bAndWOnly THEN {
intIntensity: CARDINAL;
intensity: REAL;
intensity ← ImagerColorPrivate.IntensityFromColor[NARROW[color]];
intIntensity ← Truncate[intensity*255.0];
AIS.WriteSample[I.bwWindow, intIntensity, ySamples-i, j];
}
ELSE {
intRed, intGreen, intBlue, intIntensity: CARDINAL;
red, green, blue, intensity: REAL;
[red, green, blue] ← SVShading.ExtractRGB[color];
intensity ← ImagerColorPrivate.IntensityFromColor[NARROW[color]];
intRed ← Truncate[red*255.0];
intGreen ← Truncate[green*255.0];
intBlue ← Truncate[blue*255.0];
intIntensity ← Truncate[intensity*255.0];
AIS.WriteSample[I.redWindow, intRed, ySamples-i, j];
AIS.WriteSample[I.greenWindow, intGreen, ySamples-i, j];
AIS.WriteSample[I.blueWindow, intBlue, ySamples-i, j];
AIS.WriteSample[I.bwWindow, intIntensity, ySamples-i, j];
};
};
CloseImage: PUBLIC PROC [I: Image, aisRope: Rope.ROPE, comment: Rope.ROPE, feedback: FeedbackData] = {
f: AIS.FRef;
IF I.bAndWOnly THEN {
f ← I.bwWindow.fref;
AIS.CloseWindow[I.bwWindow];
AIS.WriteComment[f, comment];
AIS.CloseFile[f];
Feedback.PutF[feedback, end, "%g now closed", [rope[aisRope]]];
Feedback.Blink[feedback];
}
ELSE {
f ← I.blueWindow.fref;
AIS.CloseWindow[I.blueWindow];
AIS.WriteComment[f, comment];
AIS.CloseFile[f];
f ← I.bwWindow.fref;
AIS.CloseWindow[I.bwWindow];
AIS.WriteComment[f, comment];
AIS.CloseFile[f];
f ← I.redWindow.fref;
AIS.CloseWindow[I.redWindow];
AIS.WriteComment[f, comment];
AIS.CloseFile[f];
f ← I.greenWindow.fref;
AIS.CloseWindow[I.greenWindow];
AIS.WriteComment[f, comment];
AIS.CloseFile[f];
Feedback.PutF[feedback, end, " (red grn blu BandW) %g now closed.", [rope[aisRope]]];
Feedback.Blink[feedback];
};
}; -- end of CloseImage
BoundBoxOfImage: PRIVATE PROC [image: ImagerPixelArray.PixelArray, resolution: REAL ← 72.0] RETURNS [boundBox: BoundBox] = {
xExtent, yExtent: REAL;
xExtent, yExtent are now in units of samples. Resolution is in samples per inch.
There are 72 screen dots per inch. We wish to normalize as if xmin = ymin = 0;
xExtent ← (image.fSize/resolution)*72.0;
yExtent ← (image.sSize/resolution)*72.0;
boundBox ← NEW[BoundBoxObj];
boundBox.loX ← boundBox.loY ← 0;
boundBox.hiX ← xExtent; boundBox.hiY ← yExtent;
};
NewBlackAndWhiteImage: PROC [imageName: Rope.ROPE, feedback: FeedbackData] RETURNS [image: ImagerPixelArray.PixelArray, success: BOOL] = {
bwName: Rope.ROPE ← Rope.Concat[SVFiles.FilenameMinusExtension[imageName],".ais"];
success ← TRUE;
image ← ImagerPixelArray.FromAIS[imageName
!FS.Error => {success ← FALSE; CONTINUE}];
IF NOT success THEN {
Feedback.Append[feedback, "Open AIS file failed", oneLiner];
Feedback.Blink[feedback];
RETURN};
};
NewColorImage: PUBLIC PROC [imageName: Rope.ROPE, feedback: FeedbackData] RETURNS [image: ImagerPixelArray.PixelArray, success: BOOL] = {
redName: Rope.ROPE ← Rope.Concat[SVFiles.FilenameMinusExtension[imageName],"-red.ais"];
greenName: Rope.ROPE ← Rope.Concat[SVFiles.FilenameMinusExtension[imageName],"-grn.ais"];
blueName: Rope.ROPE ← Rope.Concat[SVFiles.FilenameMinusExtension[imageName],"-blu.ais"];
success ← TRUE;
image ← ImagerPixelArray.Join3AIS[redName, greenName, blueName
!FS.Error => {success ← FALSE; CONTINUE}];
IF NOT success THEN {
Feedback.Append[feedback, "Open AIS file failed", oneLiner];
Feedback.Blink[feedback];
RETURN};
};
DrawAlignedBlackAndWhiteImage: PUBLIC PROC [dc: Imager.Context, imageName: Rope.ROPE, resolution: REAL, screenWRTCamera: Point2d, boundBox: BoundBox, feedback: FeedbackData] = {
Count how many samples there are in the x and y directions of the image. Look at the resolution (samples per inch. default is 72.0). This gives the actual size of the image, in inches. Frame it evenly around the clipBox. The clipBox is in Camera coordinates. Strategy: Find the center of the clipBox in camera coordinates. Convert to screen coordinates. Find the width of the ais image (in screen coordinates -- just use 72.0 as a magic number). Find the origin (xStart, yStart) in screen coordinates.
stepSize, xStart, yStart: REAL;
samplesX, samplesY: NAT;
aisFile: AIS.FRef;
raster: AIS.Raster;
originScreen, screenExtent: Point2d;
originCameraX, originCameraY: REAL;
DrawAlignedBlackAndWhiteImageAux: SAFE PROC = {
Imager.TranslateT[dc, [xStart, yStart]];
DrawBlackAndWhiteImage[dc, imageName, screenExtent, resolution, feedback];
};
aisFile ← AIS.OpenFile[imageName, FALSE];
raster ← AIS.ReadRaster[aisFile];
AIS.CloseFile[aisFile];
resolution ← ReadAIS.ResolutionFromInfo[info]; -- for now, receive as argument
Compute xStart and yStart.
samplesX ← raster.scanLength;
samplesY ← raster.scanCount;
stepSize ← 72.0/resolution; -- in screen dots per sample
IF boundBox = NIL THEN {
originCameraX ← 0.0;
originCameraY ← 0.0;
}
ELSE {
originCameraX ← (boundBox.hiX + boundBox.loX)/2.0;
originCameraY ← (boundBox.hiY + boundBox.loY)/2.0;
};
originScreen ← SVVector2d.Sub[[originCameraX, originCameraY], screenWRTCamera];
screenExtent[1] ← Real.Float[samplesX-1]*stepSize; -- screen dots
screenExtent[2] ← Real.Float[samplesY-1]*stepSize; -- screen dots
xStart ← originScreen[1] - screenExtent[1]/2.0;
yStart ← originScreen[2] - screenExtent[2]/2.0;
xStart ← xStart - stepSize/2.0;
yStart ← yStart - stepSize/2.0;
Imager.DoSaveAll[dc, DrawAlignedBlackAndWhiteImageAux];
};
DrawAlignedColorImage: PUBLIC PROC [dc: Imager.Context, imageName: Rope.ROPE, resolution: REAL, screenWRTCamera: Point2d, boundBox: BoundBox, feedback: FeedbackData] = {
Count how many samples there are in the x and y directions of the image. Look at the resolution (default is 72.0). This gives the actual size of the image. Frame it evenly around the clipBox.
stepSize, xStart, yStart: REAL;
samplesX, samplesY: NAT;
aisFile: AIS.FRef;
raster: AIS.Raster;
originScreen, screenExtent: Point2d;
originCameraX, originCameraY: REAL;
redName: Rope.ROPE ← Rope.Concat[SVFiles.FilenameMinusExtension[imageName],"-red.ais"];
DrawAlignedColorImageAux: SAFE PROC = {
Imager.TranslateT[dc, [xStart, yStart]];
DrawColorImage[dc, imageName, screenExtent, resolution, feedback];
};
aisFile ← AIS.OpenFile[redName, FALSE];
raster ← AIS.ReadRaster[aisFile];
AIS.CloseFile[aisFile];
resolution ← ReadAIS.ResolutionFromInfo[info];
Compute xStart and yStart.
samplesX ← raster.scanLength;
samplesY ← raster.scanCount;
stepSize ← 72.0/resolution; -- in screen dots per sample
IF boundBox = NIL THEN {
originCameraX ← 0.0;
originCameraY ← 0.0;
}
ELSE {
originCameraX ← (boundBox.hiX + boundBox.loX)/2.0;
originCameraY ← (boundBox.hiY + boundBox.loY)/2.0;
};
originScreen ← SVVector2d.Sub[[originCameraX, originCameraY], screenWRTCamera];
screenExtent[1] ← Real.Float[samplesX-1]*stepSize; -- screen dots
screenExtent[2] ← Real.Float[samplesY-1]*stepSize; -- screen dots
xStart ← originScreen[1] - screenExtent[1]/2.0;
yStart ← originScreen[2] - screenExtent[2]/2.0;
Imager.DoSaveAll[dc, DrawAlignedColorImageAux];
};
DrawBlackAndWhiteAtOrigin: PUBLIC PROC [dc: Imager.Context, imageName: Rope.ROPE, resolution: REAL ← 72.0, feedback: FeedbackData] = {
stepSize: REAL;
samplesX, samplesY: NAT;
aisFile: AIS.FRef;
raster: AIS.Raster;
screenExtent: Point2d;
aisFile ← AIS.OpenFile[imageName, FALSE];
raster ← AIS.ReadRaster[aisFile];
AIS.CloseFile[aisFile];
samplesX ← raster.scanLength;
samplesY ← raster.scanCount;
stepSize ← 72.0/resolution; -- in screen dots per sample
screenExtent[1] ← Real.Float[samplesX-1]*stepSize; -- screen dots
screenExtent[2] ← Real.Float[samplesY-1]*stepSize; -- screen dots
DrawBlackAndWhiteImage[dc, imageName, screenExtent, resolution, feedback];
};
DrawColorImageAtOrigin: PUBLIC PROC [dc: Imager.Context, imageName: Rope.ROPE, resolution: REAL ← 72.0, feedback: FeedbackData] = {
stepSize: REAL;
samplesX, samplesY: NAT;
aisFile: AIS.FRef;
redName: Rope.ROPE ← Rope.Concat[SVFiles.FilenameMinusExtension[imageName],"-red.ais"];
raster: AIS.Raster;
screenExtent: Point2d;
aisFile ← AIS.OpenFile[redName, FALSE];
raster ← AIS.ReadRaster[aisFile];
AIS.CloseFile[aisFile];
samplesX ← raster.scanLength;
samplesY ← raster.scanCount;
stepSize ← 72.0/resolution; -- in screen dots per sample
screenExtent[1] ← Real.Float[samplesX-1]*stepSize; -- screen dots
screenExtent[2] ← Real.Float[samplesY-1]*stepSize; -- screen dots
DrawColorImage[dc, imageName, screenExtent, resolution, feedback];
};
DrawBlackAndWhiteImage: PROC [dc: Imager.Context, imageName: Rope.ROPE, screenExtent: Point2d, resolution: REAL ← 72.0, feedback: FeedbackData] = {
clipBox in units of screen dots, in coordinates whose origin is the lower left hand corner of the ais image.
resolution is in dots per inch (72 is screen resolution).
Draws the ais file of given resolution such that its lower left hand corner is at the current origin of dc.
This involves several steps.
1) Find the total size of the clipBox. If no clipBox is given, use the whole image size.
2) Find the scaling factors needed to interpret the image as having the stated resolution
3) Translate appropriately
4) Draw.
minX, maxX, minY, maxY: REAL;
image: ImagerPixelArray.PixelArray;
success: BOOL;
scale: REAL;
colors: Imager.ColorOperator ← ImagerColor.NewColorOperatorGrayLinear[255, 0];
DrawBlackAndWhiteImageAux: SAFE PROC = {
Imager.SetSampledColor[dc, image, ImagerTransformation.Scale[scale], colors];
Imager.MaskBox[dc, [minX, minY, maxX, maxY]];
};
OPEN THE IMAGE
[image, success] ← NewBlackAndWhiteImage[imageName, feedback];
IF NOT success THEN RETURN;
Now find the scaling factors for the given resolution.
scale ← 72.0/resolution;
box bounds in screen coordinates
minX ← 0.0;
minY ← 0.0;
maxX ← screenExtent[1];
maxY ← screenExtent[2];
Imager.DoSaveAll[dc, DrawBlackAndWhiteImageAux];
};
DrawColorImage: PROC [dc: Imager.Context, imageName: Rope.ROPE, screenExtent: Point2d, resolution: REAL ← 72.0, feedback: FeedbackData] = {
See comment for DrawBlackAndWhiteImage
minX, maxX, minY, maxY: REAL;
image: ImagerPixelArray.PixelArray;
success: BOOL;
scale: REAL;
colors: Imager.ColorOperator ← ImagerColor.RGBLinearColorModel[255];
colors: Imager.ColorOperator ← ImagerColor.NewColorOperatorRGB[255];
DrawColorImageAux: SAFE PROC = {
Imager.SetSampledColor[dc, image, ImagerTransformation.Scale[scale], colors];
Imager.MaskBox[dc, [minX, minY, maxX, maxY]];
};
OPEN THE IMAGE
[image, success] ← NewColorImage[imageName, feedback];
IF NOT success THEN RETURN;
Now find the scaling factors for the given resolution.
scale ← 72.0/resolution;
box bounds in screen coordinates
minX ← 0.0;
minY ← 0.0;
maxX ← screenExtent[1];
maxY ← screenExtent[2];
Imager.DoSaveAll[dc, DrawColorImageAux];
};
END.