<?xml version="1.0" ?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:cs="http://www.censhare.com/xml/3.0.0/xpath-functions"
  xmlns:corpus="http://www.censhare.com/xml/3.0.0/corpus">

  <!-- returns a transformed image of the given asset by the given parameters -->

  <!-- parameters -->
  <xsl:param name="max-width-pixels" select="500"/>  <!-- target width of the image in pixels -->
  <xsl:param name="max-height-pixels" select="500"/> <!-- target height of the image in pixels -->
  <xsl:param name="color-space" select="()"/>        <!-- color space, values 'rgb', 'cmyk', 'lab' -->
  <xsl:param name="jpeg-quality" select="()"/>       <!-- jpeg quality, values 0 (bad) to 100 (best) -->
  <xsl:param name="sharpen" select="()"/>            <!-- sharpen, values 'true', 'false' -->
  <xsl:param name="format" select="'jpg'"/>          <!-- target format, values 'jpg', 'tif', 'png' -->
  
  <!-- root match -->
  <xsl:template match="/asset">
    <xsl:variable name="asset" select="."/>
    
    <!-- check parameters -->
    <xsl:variable name="maxWidthPixels" select="if ($max-width-pixels castable as xs:integer) then xs:integer($max-width-pixels) else ()"/>
    <xsl:variable name="maxHeightPixels" select="if ($max-height-pixels castable as xs:integer) then xs:integer($max-height-pixels) else ()"/>
    <xsl:variable name="colorSpace" select="if ($color-space=('rgb', 'cmyk')) then $color-space else 'rgb'"/>
    <xsl:variable name="jpegQuality" select="if ($jpeg-quality) then $jpeg-quality else '100'"/>
    <xsl:variable name="isSharpen" select="if ($sharpen=('true', 'false')) then $sharpen else 'false'"/>
    <xsl:variable name="fileExtension" select="if ($format=('jpg', 'tif', 'png')) then $format else 'jpg'"/>
  
    <!-- get master storage -->
    <xsl:variable name="masterStorage" select="$asset/storage_item[@key='master'][1]"/>
    <xsl:if test="$masterStorage">
    
      <!-- create target file -->
      <cs:command name="com.censhare.api.io.CreateVirtualFileSystem" returning="out"/>
      <xsl:variable name="destFile" select="concat($out, 'result.', $fileExtension)"/>
    
      <!-- transform -->
      <cs:command name="com.censhare.api.transformation.ImageTransformation">
        <cs:param name="source" select="$masterStorage"/>
        <cs:param name="dest" select="$destFile"/>
        <cs:param name="image-transformation">
          <image-transformation>
            <operations>
              <xsl:if test="$maxWidthPixels and $maxHeightPixels">
                <scale width="{$maxWidthPixels}" height="{$maxHeightPixels}"/>
              </xsl:if>
            </operations>
            <attributes sharpen="{$isSharpen}" out-jpeg-quality="{$jpegQuality}" out-color="{$colorSpace}"/>
          </image-transformation>
        </cs:param>
      </cs:command>

      <!-- Return the result (file locator) -->
      <cs:command name="com.censhare.api.context.SetProperty">
        <cs:param name="name" select="'censhare:result-file-locator'"/>
        <cs:param name="value" select="$destFile"/>
      </cs:command>
      <cs:command name="com.censhare.api.context.SetProperty">
        <cs:param name="name" select="'censhare:result-mimetype'"/>
        <cs:param name="value" select="'image/jpeg'"/>
      </cs:command>
      
    </xsl:if>

  </xsl:template>

</xsl:stylesheet>
