Advertisement
Guest User

panofilefromice.ps1

a guest
Aug 30th, 2014
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3. Exports a panorama loaded in Microsoft ICE as a .pano file.
  4.  
  5. .DESCRIPTION
  6. 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.
  7.  
  8. .EXAMPLE
  9. ./panofilefromice.ps1 ice.pano
  10. #>
  11.  
  12. param([Parameter(Mandatory=$true)]$OutputPath)
  13.  
  14. Add-Type -AssemblyName WindowsBase
  15. Add-Type -AssemblyName System.IO.Compression
  16. Add-Type -AssemblyName System.IO.Compression.FileSystem
  17.  
  18. $inputPath = (Get-ChildItem -Directory (Join-Path $env:TEMP "Photosynther\ICE") | sort -Property CreationTime -Descending | select -First 1).FullName
  19.  
  20. function New-Uri([string]$Uri, [switch]$Relative)
  21. {
  22.     if (-not $Relative)
  23.     {
  24.         New-Object System.Uri $Uri
  25.     }
  26.     else
  27.     {
  28.         New-Object System.Uri ($Uri, ([System.UriKind]::Relative))
  29.     }
  30. }
  31.  
  32. function Get-Thumbnail
  33. {
  34.     $thumbnailTilePaths = (Get-ChildItem -Recurse -File -Filter "*.jpg" (Join-Path $inputPath "thumbnail_files")).FullName
  35.  
  36.     $biggestTile =
  37.         ($thumbnailTilePaths |
  38.         group { [int][System.IO.Path]::GetFileName([System.IO.Path]::GetDirectoryName($_)) } |
  39.         where Count -EQ 1 |
  40.         sort Name -Descending |
  41.         select -First 1).Group
  42.  
  43.     [System.IO.File]::OpenRead($biggestTile)
  44. }
  45.  
  46. function Get-Info
  47. {
  48.     $thumbnailXml = [xml](Get-Content -Raw (Join-Path $inputPath "thumbnail.xml"))
  49.  
  50.     $tileSize = [int]$thumbnailXml.Image.TileSize
  51.  
  52.     $panoramaJson = ConvertFrom-Json (Get-Content -Raw (Join-Path $inputPath "panorama.json"))
  53.    
  54.     $cubemap = ($panoramaJson.collections.PSObject.Properties.Value | select -First 1).coord_systems."0".cubemaps."0"
  55.  
  56.     $fieldOfViewBounds =
  57.         ($cubemap.field_of_view_bounds[0] + 360),
  58.         $cubemap.field_of_view_bounds[1],
  59.         -$cubemap.field_of_view_bounds[3],
  60.         -$cubemap.field_of_view_bounds[2]
  61.  
  62.     $clips = @{}
  63.     $faces = "left", "front", "right", "back", "top", "bottom"
  64.  
  65.     foreach ($face in $faces)
  66.     {
  67.         if ($cubemap.$face -ne $null)
  68.         {
  69.             $faceSize = $cubemap.$face.image_size[0]
  70.  
  71.             $clipVertices = $cubemap.$face.clip.vertices
  72.  
  73.             $clips.$face = @{
  74.                 left=$clipVertices[0];
  75.                 top=$clipVertices[1];
  76.                 right=$clipVertices[4];
  77.                 bottom=$clipVertices[5]
  78.             }
  79.         }
  80.     }
  81.  
  82.     $tileSize, $fieldOfViewBounds, $faceSize, $clips
  83. }
  84.  
  85. [System.Environment]::CurrentDirectory = Get-Location
  86.  
  87. try
  88. {
  89.     $package = [System.IO.Packaging.Package]::Open($OutputPath, "Create")
  90.  
  91.     $package.CreateRelationship((New-Uri -Relative "/properties/thumbnail.jpg"), "Internal",
  92.         "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail") | Out-Null
  93.  
  94.     $thumbnailPart = $package.CreatePart([System.IO.Packaging.PackUriHelper]::CreatePartUri((New-Uri -Relative "/properties/thumbnail.jpg")), "image/jpeg")
  95.  
  96.     try
  97.     {
  98.         $stream = $thumbnailPart.GetStream()
  99.         $thumbnail = Get-Thumbnail
  100.         $thumbnail.CopyTo($stream)
  101.     }
  102.     finally
  103.     {
  104.         $stream.Dispose()
  105.         $thumbnail.Dispose()
  106.     }
  107.  
  108.     $atlasPart = $package.CreatePart([System.IO.Packaging.PackUriHelper]::CreatePartUri((New-Uri -Relative "/formats/cubemap/atlas.jpg")), "image/jpeg")
  109.  
  110.     try
  111.     {
  112.         $stream = $atlasPart.GetStream()
  113.         $atlas = [System.IO.File]::OpenRead((Join-Path $inputPath "atlas.jpg"))
  114.         $atlas.CopyTo($stream)
  115.     }
  116.     finally
  117.     {
  118.         $stream.Dispose()
  119.         $thumbnail.Dispose()
  120.     }
  121.  
  122.     $cubemapZipPaths = (Get-ChildItem -File -Filter "*.zip" $inputPath).FullName
  123.  
  124.     foreach ($cubemapZipPath in $cubemapZipPaths)
  125.     {        
  126.         try
  127.         {
  128.             $zipArchive = [System.IO.Compression.ZipFile]::OpenRead($cubemapZipPath)
  129.  
  130.             foreach ($entry in $zipArchive.Entries)
  131.             {
  132.                 $uri = New-Uri -Relative "formats/cubemap/$([System.IO.Path]::GetFileNameWithoutExtension($cubemapZipPath))/$($entry.FullName)"
  133.  
  134.                 $tilePart = $package.CreatePart([System.IO.Packaging.PackUriHelper]::CreatePartUri($uri), "image/jpeg")
  135.            
  136.                 try
  137.                 {
  138.                     $stream = $tilePart.GetStream()
  139.                     $entryStream = $entry.Open()
  140.            
  141.                     $entryStream.CopyTo($stream)
  142.                 }
  143.                 finally
  144.                 {
  145.                     $entryStream.Dispose()
  146.                     $stream.Dispose()
  147.                 }
  148.             }
  149.         }
  150.         finally
  151.         {
  152.             $zipArchive.Dispose()
  153.         }
  154.     }
  155.  
  156.     $cubemapJsonPart = $package.CreatePart([System.IO.Packaging.PackUriHelper]::CreatePartUri((New-Uri -Relative "/formats/cubemap/cubemap.json")), "application/json")
  157.  
  158.     $tileSize, $fieldOfViewBounds, $faceSize, $clips = Get-Info
  159.  
  160.     $cubemapJson = @{
  161.         cubemap_json_version=1.0;
  162.         field_of_view_bounds=$fieldOfViewBounds;
  163.         face_size=$faceSize;
  164.         tile_size=$tileSize;
  165.         initial_look_direction=(([System.Math]::PI/2),0.0);
  166.         orientation=(0.0,0.0,0.0) }
  167.  
  168.     foreach ($cubemapZipPath in $cubemapZipPaths)
  169.     {
  170.         $faceName = [System.IO.Path]::GetFileNameWithoutExtension($cubemapZipPath)
  171.  
  172.         $cubemapJson.$faceName = @{ tile_boundaries=$clips.$faceName }
  173.     }
  174.  
  175.     try
  176.     {
  177.         $streamWriter = New-Object System.IO.StreamWriter $cubemapJsonPart.GetStream()
  178.         $streamWriter.Write((ConvertTo-Json -Compress $cubemapJson))
  179.     }
  180.     finally
  181.     {
  182.         $streamWriter.Dispose()
  183.     }
  184. }
  185. finally
  186. {
  187.     $package.Dispose()
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement