Sirds AppleScript sample

Sirds lets you create Single Image Random Dot Stereogram (SIRDS) and Single Image Stereogram (SIS) images.
Post Reply
katsura
Posts: 25
Joined: Sat May 23, 2020 6:16 pm

Sirds AppleScript sample

Post by katsura »

Code: Select all

(*   drop generate SIRDS AppleScript droplet for Sirds 

   Copyright (c) 2006-2009 Katsura Shareware. All rights reserved. 
    
   generates stereogram image files from image files/folders dropped on to this AppleScript droplet. 
*) 

property useCrossEyedView : false 

on processFile(aFile) 
    
   tell application "Sirds" 
       
      -- set all the preferences first 
      set pattern mode to random 
      set random dot color to full color 
      set random dot shape to random shapes 
      set resolution to 85 
      if useCrossEyedView is true then 
         set view mode to cross eyed -- Sirds version 1.1 or later 
         set observer distance to 28 
         set maximum depth to 14 
      else 
         set view mode to wall eyed -- Sirds version 1.1 or later 
         set observer distance to 14 
         set maximum depth to 14 
      end if 
      set eye separation to 2.5 
      set oversampling to off 
      set show guide to false 
      set invert depth to false 
       
      set anExtension to name extension of (info for aFile) 
      set aFilePOSIXPath to (POSIX path of aFile) 
      set theOffset to offset of ("." & anExtension) in aFilePOSIXPath 
      set anExportFilePath to characters 1 through (theOffset - 1) of aFilePOSIXPath 
      set anExportFilePath to (anExportFilePath as string) & "_stereo.png" 
       
      open aFilePOSIXPath 
      tell document 1 
         export stereogram to (POSIX file anExportFilePath) as "png" 
      end tell 
      close window 1 
   end tell 
end processFile 

on processFolder(aFolder) 
   set itemList to list folder aFolder without invisibles 
   repeat with anItem in itemList 
      set aFilePath to ((aFolder as text) & ":" & anItem) 
      set aFile to alias aFilePath 
      set itemInfo to info for aFile 
      if folder of itemInfo is true then 
         processFolder(aFile) 
      else 
         processFile(aFile) 
      end if 
   end repeat 
end processFolder 

-- dropped file or folder gets passed to this "on open" action 
on open aList 
   repeat with anItem in aList 
      set itemInfo to info for anItem 
      if folder of itemInfo is true then 
         -- process a folder 
         processFolder(anItem) 
      else 
         -- process a file 
         processFile(anItem) 
      end if 
       
   end repeat 
    
end open
katsura
Posts: 25
Joined: Sat May 23, 2020 6:16 pm

Re: Sirds AppleScript sample

Post by katsura »

random color image frames cannot be encoded in high quality due to the bandwidth limitation. and it doesn't look so nice visually either. i recommend using black and white patterns for stereogram animation.

in JavaScript/AppleScript:

Code: Select all

//   drop generate SIRDS AppleScript/JavaScript droplet for Sirds 
//
//   Copyright (c) 2006-2021 Katsura Shareware. All rights reserved. 
//    
//   generates stereogram image files from .png depth map image files/folders dropped on to this AppleScript/JavaScript droplet. 

// wall-eyed:  set useCrossEyedView to false
// cross-eyed: set useCrossEyedView to true
var useCrossEyedView = false

// random dot:    set usePatternImage to false
// pattern image: set usePatternImage to true
var usePatternImage = false
var patternImage = null

var fileManager = $.NSFileManager.defaultManager
var app = Application.currentApplication()
app.includeStandardAdditions = true

//
// input:
//   path: "abc.png",
//   replacement: "_bw.png" (black and white) or "_pi.png" (pattern image)
// output:
//   "abc_bw.png" or "abc_pi.png"
//
function replaceExtension(path, replacement) {
  return path.substring(0, path.lastIndexOf(".")) + replacement
}

function processFile(aFile) {
   var Sirds = Application("Sirds")
   var suffix

   // set all the preferences first
   if (usePatternImage) {
      Sirds.patternMode = "image file"
	  Sirds.pattern = patternImage
	  suffix = "_pi.png"
   } else {
      Sirds.patternMode = "random"
      Sirds.randomDotColor = "black and white"
      Sirds.randomDotShape = "random shapes"
	  suffix = "_bw.png"
   }
   Sirds.resolution = 85
   if (useCrossEyedView) {
     Sirds.viewMode = "cross eyed"
	 Sirds.observerDistance = 28
	 Sirds.maximumDepth = 14
   } else {
     Sirds.viewMode = "wall eyed"
	 Sirds.observerDistance = 14
	 Sirds.maximumDepth = 14
   }
   Sirds.eyeSeparation = 2.5
   Sirds.oversampling = "four times"
   Sirds.showGuide = false
   Sirds.invertDepth = false

   var exportFilePath = Path(replaceExtension(aFile.toString(), suffix))
   
   console.log(aFile.toString())
   console.log(exportFilePath.toString())

   var doc = Sirds.open(aFile)
   doc.exportStereogram({to: exportFilePath, as: "png"})
   doc.close()
} 

function processFolder(folder) {
   var folderString = folder.toString()
   var folderItems = app.listFolder(folder, { invisibles: false })
   for (var item of folderItems) {
     var currentItem = `${folderString}/${item}`
     openDocuments([currentItem])
   }
}

// dropped file or folder gets passed to this "on open" action 
function openDocuments(aList) {
   if (usePatternImage && !patternImage) {
      patternImage = app.chooseFile({
         withPrompt: "Please select a pattern image file:"
      })
   }
   for (var item of aList) {
      var isDir = Ref()
      if (fileManager.fileExistsAtPathIsDirectory(item.toString(), isDir) && isDir[0]) {
          processFolder(item)
      }
      else {
          processFile(item)
      }
   }
}

// for debugging, "run" this script in Script Editor and select a depth map .png file
function run() {
   var file = app.chooseFile({withPrompt:"Please select a depth map image file or folder:"})
   openDocuments([file])
}
Post Reply