Skip to content

Commit

Permalink
[fix] Remove reference to System.Drawing assembly
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
RangHo committed Nov 28, 2023
1 parent e208360 commit 364f734
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 39 deletions.
18 changes: 0 additions & 18 deletions Sources/Classes/Color.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,6 @@ class Color : Vector3
-SecondValue { param([double]$Value) $this.Z = $Value }
}

[System.Drawing.Color]ToDrawingColor()
{
[int]$ir = $this.R * 255 -as [int]
[int]$ig = $this.G * 255 -as [int]
[int]$ib = $this.B * 255 -as [int]
return [System.Drawing.Color]::FromArgb($ir, $ig, $ib)
}

static [System.Drawing.Color]op_Implicit([Color]$color)
{
return $color.ToDrawingColor()
}

static [System.Drawing.Color]op_Explicit([Color]$color)
{
return $color.ToDrawingColor()
}

static [Color]op_Addition([Color]$a, [Color]$b)
{
return [Color]::new(
Expand Down
3 changes: 0 additions & 3 deletions Sources/PSRaytracing.psm1
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Load needed assemblies
using assembly System.Drawing

# Load user-defined types
using module .\Classes\Vector3.psm1
using module .\Classes\Color.psm1
Expand Down
39 changes: 21 additions & 18 deletions Sources/Public/Invoke-Raytracing.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ implementation of raytracing techniques.
None. For now, the scene is hard-coded in the script.
.OUTPUTS
None. This cmdlet creates a PNG file in the current directory.
None. This cmdlet creates a PPM file in the current directory.
.LINK
https://github.com/RangHo/powershell-raytracing
Expand All @@ -43,7 +43,7 @@ function Invoke-Raytracing

# Name of the output file.
[Parameter()]
[string]$OutputFile = "output.png"
[string]$OutputFile = "output.ppm"
)

begin
Expand Down Expand Up @@ -77,9 +77,9 @@ function Invoke-Raytracing
}

# File name settings
if (-not $OutputFile.EndsWith(".png"))
if (-not $OutputFile.EndsWith(".ppm"))
{
$OutputFile = $OutputFile + ".png"
$OutputFile = $OutputFile + ".ppm"
}

# Scene objects
Expand All @@ -91,19 +91,24 @@ function Invoke-Raytracing

process
{
# Create a bitmap to hold the image
$bitmap = New-Object System.Drawing.Bitmap($ImageWidth, $ImageHeight)
# Create a new text file to store the PPM output
New-Item -Path $OutputFile -ItemType File -Force

# Save the bitmap to the output file
foreach ($y in 0..($ImageHeight - 1))
# Write the PPM header
Add-Content -Path $OutputFile -Value "P3"
Add-Content -Path $OutputFile -Value "$ImageWidth $ImageHeight"
Add-Content -Path $OutputFile -Value "255"

# Render the image
foreach ($y in ($ImageHeight - 1)..0)
{
foreach ($x in 0..($ImageWidth - 1))
{
# Write the current process
Write-Progress `
-Activity "Rendering image" `
-Status "Rendering pixel ($x, $y)" `
-PercentComplete (($y * $ImageWidth + $x) / ($ImageWidth * $ImageHeight) * 100)
-PercentComplete ((($ImageHeight - 1 - $y) * $ImageWidth + $x) / ($ImageWidth * $ImageHeight) * 100)

$pixel = New-Color 0 0 0
foreach ($aa in 0..($Samples - 1))
Expand All @@ -117,16 +122,14 @@ function Invoke-Raytracing
# Average the color
$pixel /= $Samples

# Set the pixels at the current location
# Note that the y-coordinate is flipped as the bitmap is stored
# top-to-bottom, unlike the world coordinate
$bitmap.SetPixel(
$x,
$ImageHeight - $y - 1,
$pixel
)
# Convert the color to 0-255 range integer 3-tuple
$ir = [Math]::Min(255, [Math]::Floor(256 * $pixel.R))
$ig = [Math]::Min(255, [Math]::Floor(256 * $pixel.G))
$ib = [Math]::Min(255, [Math]::Floor(256 * $pixel.B))

# Write the pixel to the PPM file
Add-Content -Path $OutputFile -Value "$ir $ig $ib"
}
}
$bitmap.Save($OutputFile, [System.Drawing.Imaging.ImageFormat]::Png)
}
}

0 comments on commit 364f734

Please sign in to comment.