Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Exports a panorama loaded in Microsoft ICE as a .pano file.
- .DESCRIPTION
- First create the panorama in Microsoft ICE and click on "Publish to Web". After ICE finishes and the Photosynth creation window is displayed run the script. When the script completes you can cancel Photosynth.
- .EXAMPLE
- ./panofilefromice.ps1 ice.pano
- #>
- param([Parameter(Mandatory=$true)]$OutputPath)
- Add-Type -AssemblyName WindowsBase
- Add-Type -AssemblyName System.IO.Compression
- Add-Type -AssemblyName System.IO.Compression.FileSystem
- $inputPath = (Get-ChildItem -Directory (Join-Path $env:TEMP "Photosynther\ICE") | sort -Property CreationTime -Descending | select -First 1).FullName
- function New-Uri([string]$Uri, [switch]$Relative)
- {
- if (-not $Relative)
- {
- New-Object System.Uri $Uri
- }
- else
- {
- New-Object System.Uri ($Uri, ([System.UriKind]::Relative))
- }
- }
- function Get-Thumbnail
- {
- $thumbnailTilePaths = (Get-ChildItem -Recurse -File -Filter "*.jpg" (Join-Path $inputPath "thumbnail_files")).FullName
- $biggestTile =
- ($thumbnailTilePaths |
- group { [int][System.IO.Path]::GetFileName([System.IO.Path]::GetDirectoryName($_)) } |
- where Count -EQ 1 |
- sort Name -Descending |
- select -First 1).Group
- [System.IO.File]::OpenRead($biggestTile)
- }
- function Get-Info
- {
- $thumbnailXml = [xml](Get-Content -Raw (Join-Path $inputPath "thumbnail.xml"))
- $tileSize = [int]$thumbnailXml.Image.TileSize
- $panoramaJson = ConvertFrom-Json (Get-Content -Raw (Join-Path $inputPath "panorama.json"))
- $cubemap = ($panoramaJson.collections.PSObject.Properties.Value | select -First 1).coord_systems."0".cubemaps."0"
- $fieldOfViewBounds =
- ($cubemap.field_of_view_bounds[0] + 360),
- $cubemap.field_of_view_bounds[1],
- -$cubemap.field_of_view_bounds[3],
- -$cubemap.field_of_view_bounds[2]
- $clips = @{}
- $faces = "left", "front", "right", "back", "top", "bottom"
- foreach ($face in $faces)
- {
- if ($cubemap.$face -ne $null)
- {
- $faceSize = $cubemap.$face.image_size[0]
- $clipVertices = $cubemap.$face.clip.vertices
- $clips.$face = @{
- left=$clipVertices[0];
- top=$clipVertices[1];
- right=$clipVertices[4];
- bottom=$clipVertices[5]
- }
- }
- }
- $tileSize, $fieldOfViewBounds, $faceSize, $clips
- }
- [System.Environment]::CurrentDirectory = Get-Location
- try
- {
- $package = [System.IO.Packaging.Package]::Open($OutputPath, "Create")
- $package.CreateRelationship((New-Uri -Relative "/properties/thumbnail.jpg"), "Internal",
- "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail") | Out-Null
- $thumbnailPart = $package.CreatePart([System.IO.Packaging.PackUriHelper]::CreatePartUri((New-Uri -Relative "/properties/thumbnail.jpg")), "image/jpeg")
- try
- {
- $stream = $thumbnailPart.GetStream()
- $thumbnail = Get-Thumbnail
- $thumbnail.CopyTo($stream)
- }
- finally
- {
- $stream.Dispose()
- $thumbnail.Dispose()
- }
- $atlasPart = $package.CreatePart([System.IO.Packaging.PackUriHelper]::CreatePartUri((New-Uri -Relative "/formats/cubemap/atlas.jpg")), "image/jpeg")
- try
- {
- $stream = $atlasPart.GetStream()
- $atlas = [System.IO.File]::OpenRead((Join-Path $inputPath "atlas.jpg"))
- $atlas.CopyTo($stream)
- }
- finally
- {
- $stream.Dispose()
- $thumbnail.Dispose()
- }
- $cubemapZipPaths = (Get-ChildItem -File -Filter "*.zip" $inputPath).FullName
- foreach ($cubemapZipPath in $cubemapZipPaths)
- {
- try
- {
- $zipArchive = [System.IO.Compression.ZipFile]::OpenRead($cubemapZipPath)
- foreach ($entry in $zipArchive.Entries)
- {
- $uri = New-Uri -Relative "formats/cubemap/$([System.IO.Path]::GetFileNameWithoutExtension($cubemapZipPath))/$($entry.FullName)"
- $tilePart = $package.CreatePart([System.IO.Packaging.PackUriHelper]::CreatePartUri($uri), "image/jpeg")
- try
- {
- $stream = $tilePart.GetStream()
- $entryStream = $entry.Open()
- $entryStream.CopyTo($stream)
- }
- finally
- {
- $entryStream.Dispose()
- $stream.Dispose()
- }
- }
- }
- finally
- {
- $zipArchive.Dispose()
- }
- }
- $cubemapJsonPart = $package.CreatePart([System.IO.Packaging.PackUriHelper]::CreatePartUri((New-Uri -Relative "/formats/cubemap/cubemap.json")), "application/json")
- $tileSize, $fieldOfViewBounds, $faceSize, $clips = Get-Info
- $cubemapJson = @{
- cubemap_json_version=1.0;
- field_of_view_bounds=$fieldOfViewBounds;
- face_size=$faceSize;
- tile_size=$tileSize;
- initial_look_direction=(([System.Math]::PI/2),0.0);
- orientation=(0.0,0.0,0.0) }
- foreach ($cubemapZipPath in $cubemapZipPaths)
- {
- $faceName = [System.IO.Path]::GetFileNameWithoutExtension($cubemapZipPath)
- $cubemapJson.$faceName = @{ tile_boundaries=$clips.$faceName }
- }
- try
- {
- $streamWriter = New-Object System.IO.StreamWriter $cubemapJsonPart.GetStream()
- $streamWriter.Write((ConvertTo-Json -Compress $cubemapJson))
- }
- finally
- {
- $streamWriter.Dispose()
- }
- }
- finally
- {
- $package.Dispose()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement