/// <summary> /// Read an image from a stream pointing to a .pfm file /// </summary> /// <param name="inputStream"> A stream pointing to a .pfm file.</param> public void readPfm(Stream inputStream) { string magic = readLine(inputStream); if (magic != "PF") { throw new InvalidPfmFileFormat("Invalid magic PFM line!"); } string whLine = readLine(inputStream); List <int> w_h = parseImageSize(whLine); this = new HdrImage(w_h[0], w_h[1]); string endianLine = readLine(inputStream); bool lEnd = isLittleEndian(endianLine); Color temp = new Color(); for (int i = 0; i < this.height; i++) { for (int j = 0; j < this.width; j++) { temp.r = readFloat(inputStream, lEnd); temp.g = readFloat(inputStream, lEnd); temp.b = readFloat(inputStream, lEnd); pixel[pixelOffset(j, this.height - 1 - i)] = temp; } } }
/// <summary> /// Basic constructor for the class. /// </summary> /// <param name="i"><see cref="HdrImage"/> input parameter</param> /// <param name="c"><see cref="Camera"/> input parameter</param> public ImageTracer(HdrImage i, Camera c, int sps = 0) { this.image = i; this.camera = c; this.pcg = new PCG(); this.samplesPerSide = sps; }
public static IPigment parsePigment(InputStream inputFile, Scene scene) { KeywordEnum key = inputFile.expectKeywords(new List <KeywordEnum>() { KeywordEnum.Uniform, KeywordEnum.Checkered, KeywordEnum.Image }); inputFile.expectSymbol("("); IPigment result; if (key == KeywordEnum.Uniform) { Color color = parseColor(inputFile, scene); result = new UniformPigment(color: color); } else if (key == KeywordEnum.Checkered) { Color col1 = parseColor(inputFile, scene); inputFile.expectSymbol(","); Color col2 = parseColor(inputFile, scene); inputFile.expectSymbol(","); // optional parameter? int steps = (int)inputFile.expectNumber(scene); result = new CheckeredPigment(col1, col2, steps); } else if (key == KeywordEnum.Image) { string fileName = inputFile.expectString(); using (Stream imageStream = File.OpenRead(fileName)) { HdrImage img = new HdrImage(imageStream); result = new ImagePigment(img); } } else { throw new GrammarError(inputFile.location, "This line should be unreachable"); } inputFile.expectSymbol(")"); return(result); }
/// <summary> /// It saves the image in .pfm format into a stream /// </summary> /// <param name="outputStream"> Output Stream (either Memory- or FileStream).</param> public void savePfm(Stream outputStream) { var endiannessValue = BitConverter.IsLittleEndian ? "-1.0" : "1.0"; var header = Encoding.ASCII.GetBytes($"PF\n{this.width} {this.height}\n{endiannessValue}\n"); outputStream.Write(header); var img = new HdrImage(7, 4); for (int x = 0; x < this.height; x++) { for (int y = 0; y < this.width; y++) { // Bottom left to top right Color col = this.getPixel(y, this.height - 1 - x); _writeFloat(outputStream, col.r); _writeFloat(outputStream, col.g); _writeFloat(outputStream, col.b); } } return; }
public ImagePigment(HdrImage i) { this.image = i; }