/*
    Copyright 2005-2007 Adobe Systems Incorporated
    Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
    or a copy at http://stlab.adobe.com/licenses.html)
*/

sheet image_size
{
input:
    original_width          : 1600;
    original_height         : 1200;
    original_resolution     : 300;

constant:
    original_doc_width      : original_width / original_resolution;
    original_doc_height     : original_height / original_resolution;

interface:
    resample                : true;
    unlink  constrain       : true <== resample ? constrain : true;
    unlink  scale_styles    : true <== resample && constrain ? scale_styles : false;

    resample_method         : @bicubic;

    dim_width_pixels        : original_width <== resample ? round(dim_width_pixels) : original_width;
    dim_width_percent       : 100 <== resample ? dim_width_percent : 100;

    dim_height_pixels       : original_height <== resample ? round(dim_height_pixels) : original_height;
    dim_height_percent      : 100 <== resample ? dim_height_percent : 100;

    doc_width_inches        : original_doc_width;
    doc_width_percent       : 100;

    /*
        Resolution must be initialized before width and height inches to allow proportions
        to be constrained.
    */
    doc_resolution          : original_resolution;
    
    doc_height_inches       : original_doc_height;
    doc_height_percent      : 100;

    auto_quality            : @draft;
    
    screen_lpi; // initialized from doc_resolution

logic:
    relate {
        doc_width_inches    <== doc_width_percent * original_doc_width / 100;
        doc_width_percent   <== doc_width_inches * 100 / original_doc_width;
    }

    relate {
        doc_height_inches   <== doc_height_percent * original_doc_height / 100;
        doc_height_percent  <== doc_height_inches * 100 / original_doc_height;
    }

    relate {
        screen_lpi      <== doc_resolution / (auto_quality == @draft ? 1 : (auto_quality == @good ? 1.5 : 2.0));
        doc_resolution  <== screen_lpi * (auto_quality == @draft ? 1 : (auto_quality == @good ? 1.5 : 2.0));
    }

    when (resample) relate {
        dim_width_pixels    <== dim_width_percent * original_width / 100;
        dim_width_percent   <== dim_width_pixels * 100 / original_width;
    }

    when (resample) relate {
        dim_height_pixels   <== dim_height_percent * original_height / 100;
        dim_height_percent  <== dim_height_pixels * 100 / original_height;
    }

    when (resample) relate {
        doc_width_inches    <== dim_width_pixels / doc_resolution;
        dim_width_pixels    <== doc_width_inches * doc_resolution;
        doc_resolution      <== dim_width_pixels / doc_width_inches;
    }

    when (resample) relate {
        doc_height_inches   <== dim_height_pixels / doc_resolution;
        dim_height_pixels   <== doc_height_inches * doc_resolution;
        doc_resolution      <== dim_height_pixels / doc_height_inches;
    }

    when (!resample) relate {
        doc_resolution      <== original_width / doc_width_inches;
        doc_width_inches    <== original_width / doc_resolution;
    }

    when (!resample) relate {
        doc_resolution      <== original_height / doc_height_inches;
        doc_height_inches   <== original_height / doc_resolution;
    }

    when (constrain && resample) relate {
        dim_width_percent       <== dim_height_percent;
        dim_height_percent      <== dim_width_percent;
    }

 output:
    byte_count      <== dim_width_pixels * dim_height_pixels * 32;

    result          <== resample ?  {
                                        command:            @resize_image,
                                        width:              dim_width_pixels,
                                        height:             dim_height_pixels,
                                        resolution:         doc_resolution,
                                        scale_styles:       scale_styles,
                                        resample_method:    resample_method
                                    } : {
                                        command:            @set_resolution,
                                        resolution:         doc_resolution
                                    };

 invariant:
    width_max       <== dim_width_pixels <= 300000;
    height_max      <== dim_height_pixels <= 300000;
}
