public static void Main() { // image params const double aspectRatio = 16.0 / 9.0; const int width = 400; const int height = (int)(width / aspectRatio); // camera params float viewportHeight = 2.0f; float viewportWidth = (float)(aspectRatio * viewportHeight); float focalLength = 1.0f; var origin = new Vector3(0, 0, 0); var horizontal = new Vector3(viewportWidth, 0, 0); var vertical = new Vector3(0, viewportHeight, 0); var lowerLeftCorner = origin - horizontal / 2 - vertical / 2 - new Vector3(0, 0, focalLength); var imagePath = "test.ppm"; var ppmImage = new PPMImage(width, height, origin, horizontal, vertical, lowerLeftCorner); //var ppmImage = new PPMImage(256, 256); ppmImage.Save(imagePath); }
public Bitmap LoadPPMImage(string fileName) { using (var ppmReader = new PPMReader(fileName)) { try { _image = ppmReader.ReadFile(); } catch (Exception ex) { MessageBox.Show(ex.Message); return(null); } } Bitmap bitmap; if (_image.Type == PPMImageType.P6) { int maxR = 0, maxG = 0, maxB = 0; bitmap = PPM_P6Parser.Parse(_image, ref maxR, ref maxG, ref maxB); _image.MaxR = maxR; _image.MaxG = maxG; _image.MaxB = maxB; } else { bitmap = PPM_P3Parser.Parse(_image); } return(bitmap); }
public void Scale(PPMImage image) { Bitmap bitmap; if (image.Type == PPMImageType.P3) { bitmap = PPM_P3Parser.Scale(image); } else { bitmap = PPM_P6Parser.Scale(image); } ImageCanvas.Source = BitmapConverter.GetBitmapSource(bitmap); }
private async void OpenImageMenuFlyoutItem_Click(object sender, RoutedEventArgs e) { var picker = new FileOpenPicker { ViewMode = PickerViewMode.Thumbnail, SuggestedStartLocation = PickerLocationId.PicturesLibrary }; picker.FileTypeFilter.Add(".bmp"); picker.FileTypeFilter.Add(".heic"); picker.FileTypeFilter.Add(".heif"); picker.FileTypeFilter.Add(".jpg"); picker.FileTypeFilter.Add(".jpeg"); picker.FileTypeFilter.Add(".png"); picker.FileTypeFilter.Add(".gif"); picker.FileTypeFilter.Add(".tif"); picker.FileTypeFilter.Add(".tiff"); picker.FileTypeFilter.Add(".ppm"); picker.FileTypeFilter.Add("*"); StorageFile inputFile = await picker.PickSingleFileAsync(); if (inputFile != null) { if (!(IsKnownFile(inputFile.FileType) || IsKnownFileMime(inputFile.ContentType))) { if (!(await ShowFileTryOpenDialog())) { return; } } ContentFrame_Reset(); ImageFileTextBox.Text = inputFile.Path; if (InputImageStream != null) { InputImageStream.Dispose(); InputImageStream = null; } if (inputFile.FileType == ".ppm") { try { PPMImage ppmImage = await PPMImage.Open(inputFile); WriteableOutputImage = ppmImage.ConvertToWriteableBitmap(); } catch (Exception ex) { await ShowErrorDialog("An error occured during file open. Damaged file!\nError message: " + ex.Message); return; } SoftwareBitmap softWriteableOutputImage = SoftwareBitmap.CreateCopyFromBuffer(WriteableOutputImage.PixelBuffer, BitmapPixelFormat.Bgra8, WriteableOutputImage.PixelWidth, WriteableOutputImage.PixelHeight); InputImageStream = await GetRandomAccessStreamFromSoftwareBitmap(softWriteableOutputImage, BitmapEncoder.PngEncoderId); await LoadInputVirtualBitmap(); await UpdateOutputImage(); } else { try { using (IRandomAccessStream stream = await inputFile.OpenAsync(FileAccessMode.Read)) { WriteableBitmap writeableInputImage; BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); SoftwareBitmap softwareBitmap1 = await decoder.GetSoftwareBitmapAsync(); ImageResolution.Text = softwareBitmap1.PixelWidth + " x " + softwareBitmap1.PixelHeight; writeableInputImage = new WriteableBitmap(softwareBitmap1.PixelWidth, softwareBitmap1.PixelHeight); writeableInputImage.SetSource(stream); InputImageStream = stream; await LoadInputVirtualBitmap(); } // inputImage.Source = writeableInputImage; using (IRandomAccessStream stream = await inputFile.OpenAsync(FileAccessMode.Read)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); SoftwareBitmap softwareBitmap1 = await decoder.GetSoftwareBitmapAsync(); WriteableOutputImage = new WriteableBitmap(softwareBitmap1.PixelWidth, softwareBitmap1.PixelHeight); WriteableOutputImage.SetSource(stream); WriteableOutputImageCopy = WriteableOutputImage.Clone(); PrevOutputs.Push(WriteableOutputImage.Clone()); UndoFlyoutItem.IsEnabled = false; await UpdateOutputImage(); } } catch (Exception) { await ShowErrorDialog("An error occured during file open. Damaged file!"); return; } } //outputImage.Source = writableOutputImage; } else { //Operation cancelled return; } ReopenFlyoutItem.IsEnabled = true; SaveMenuFlyoutItem.IsEnabled = true; EditMenuBarItem.IsEnabled = true; AdvancedToolsMenuBarItem.IsEnabled = true; ToolsMenuBarItem.IsEnabled = true; ZoomCommandBar.IsEnabled = true; //await Open(); }
/// <summary> /// Converts a PDF document with (multiple) pages to one or multiple bitmaps if the document doesn't fit in a single one /// </summary> /// <param name="filePath">Path to the Ps file</param> /// <param name="ct">the cancellation token</param> /// <exception cref="ConversionFailedException">thrown if something went wrong</exception> public static BitmapSource[] ConvertPDFToBitmaps(string filePath, CancellationToken ct, ProgressReporter reporter) { if (File.Exists(filePath)) { // retrieve number of pages int pageCount; try { pageCount = XPDF.GetPageCount(filePath); } catch { throw new ConversionFailedException("Could not read number of pages"); } ct.ThrowIfCancellationRequested(); reporter.ReportProgress($"Processing page 1 of {pageCount}"); // read first page List <List <byte> > finalImages = new List <List <byte> >(); finalImages.Add(new List <byte>()); string pipeName = "BPfON"; PPMImage currentImage; try { currentImage = XPDF.GetPageAsPPM(filePath, 1, DPI, false, false, pipeName); } catch { throw new ConversionFailedException("Page 1 couldn't be read"); } int width = currentImage.Width; int height = currentImage.Height; int bytesPerPixel = currentImage.BytesPerPixel; int stride = currentImage.Width * bytesPerPixel; ct.ThrowIfCancellationRequested(); // load next image asynchronously PPMImage nextImage = null; int nextFirstNonWhiteRow = 0; int nextLastNonWhiteRow = 0; Task task = null; if (pageCount > 1) { try { task = Task.Run(() => nextImage = XPDF.GetPageAsPPM(filePath, 2, DPI, false, false, pipeName)); } catch { throw new ConversionFailedException("Page 2 couldn't be read"); } } // Add first image without white borders to final list int firstNonWhiteRow = FirstNonWhiteRow(currentImage.Pixels, width, height, bytesPerPixel); ct.ThrowIfCancellationRequested(); int lastNonWhiteRow = LastNonWhiteRow(currentImage.Pixels, width, height, bytesPerPixel); ct.ThrowIfCancellationRequested(); finalImages[0].AddRange(currentImage.Pixels[(firstNonWhiteRow * stride)..(lastNonWhiteRow * stride)]);