~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to libddmsimple/ddmsimple.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Aqsis
 
2
// Copyright ļæ½ 1997 - 2001, Paul C. Gregory
 
3
//
 
4
// Contact: pgregory@aqsis.com
 
5
//
 
6
// This library is free software; you can redistribute it and/or
 
7
// modify it under the terms of the GNU General Public
 
8
// License as published by the Free Software Foundation; either
 
9
// version 2 of the License, or (at your option) any later version.
 
10
//
 
11
// This library is distributed in the hope that it will be useful,
 
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
// General Public License for more details.
 
15
//
 
16
// You should have received a copy of the GNU General Public
 
17
// License along with this library; if not, write to the Free Software
 
18
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 
 
20
 
 
21
/** \file
 
22
                \brief Simple example display device manager.
 
23
                \author Paul C. Gregory (pgregory@aqsis.com)
 
24
*/
 
25
 
 
26
 
 
27
#include        "aqsis.h"
 
28
 
 
29
#include        "sstring.h"
 
30
#include        "irenderer.h"
 
31
#include        "ddmsimple.h"
 
32
#include        "imagebuffer.h"
 
33
#include        "file.h"
 
34
#include        "tiffio.h"
 
35
 
 
36
START_NAMESPACE( Aqsis )
 
37
 
 
38
/// Required function that implements Class Factory design pattern for DDManager libraries
 
39
IqDDManager* CreateDisplayDriverManager()
 
40
{
 
41
    return new CqDDManagerSimple;
 
42
}
 
43
 
 
44
//---------------------------------------------------------------------
 
45
/** Initialise the device manager.
 
46
 */
 
47
 
 
48
TqInt CqDDManagerSimple::Initialise()
 
49
{
 
50
    return ( 0 );
 
51
}
 
52
 
 
53
TqInt CqDDManagerSimple::Shutdown()
 
54
{
 
55
    return ( 0 );
 
56
}
 
57
 
 
58
 
 
59
TqInt CqDDManagerSimple::AddDisplay( const TqChar* name, const TqChar* type, const TqChar* mode, TqInt modeID, TqInt dataOffset, TqInt dataSize, std::map<std::string, void*> mapOfArguments )
 
60
{
 
61
    m_aDisplayRequests.push_back( SqDDevice( name, type, mode ) );
 
62
    return ( 0 );
 
63
}
 
64
 
 
65
TqInt CqDDManagerSimple::ClearDisplays()
 
66
{
 
67
    m_aDisplayRequests.clear();
 
68
    return ( 0 );
 
69
}
 
70
 
 
71
TqInt CqDDManagerSimple::OpenDisplays()
 
72
{
 
73
    std::vector<SqDDevice>::iterator i;
 
74
    for ( i = m_aDisplayRequests.begin(); i != m_aDisplayRequests.end(); i++ )
 
75
    {
 
76
        i->m_XRes = QGetRenderContext() ->pImage() ->iXRes();
 
77
        i->m_YRes = QGetRenderContext() ->pImage() ->iYRes();
 
78
        RtInt mode = 0;
 
79
        if ( strstr( i->m_strMode.c_str(), RI_RGB ) != NULL )
 
80
            mode |= ModeRGB;
 
81
        if ( strstr( i->m_strMode.c_str(), RI_A ) != NULL )
 
82
            mode |= ModeA;
 
83
        if ( strstr( i->m_strMode.c_str(), RI_Z ) != NULL )
 
84
            mode |= ModeZ;
 
85
        TqInt SamplesPerElement = mode & ModeRGB ? 3 : 0;
 
86
        SamplesPerElement += mode & ModeA ? 1 : 0;
 
87
        SamplesPerElement = mode & ModeZ ? 1 : SamplesPerElement;
 
88
        i->m_SamplesPerElement = SamplesPerElement;
 
89
 
 
90
        // Create a buffer big enough to hold a row of buckets.
 
91
        i->m_pData = new unsigned char[ i->m_XRes * i->m_YRes * i->m_SamplesPerElement ];
 
92
    }
 
93
    return ( 0 );
 
94
}
 
95
 
 
96
TqInt CqDDManagerSimple::CloseDisplays()
 
97
{
 
98
    std::vector<SqDDevice>::iterator i;
 
99
 
 
100
    i = m_aDisplayRequests.begin();
 
101
 
 
102
    for ( ; i != m_aDisplayRequests.end(); i++ )
 
103
    {
 
104
        uint16 photometric = PHOTOMETRIC_RGB;
 
105
        uint16 config = PLANARCONFIG_CONTIG;
 
106
 
 
107
        TIFF* pOut = TIFFOpen( i->m_strName.c_str(), "w" );
 
108
 
 
109
        if ( pOut )
 
110
        {
 
111
            // Write the image to a tiff file.
 
112
 
 
113
            int ExtraSamplesTypes[ 1 ] = {EXTRASAMPLE_ASSOCALPHA};
 
114
 
 
115
            TIFFSetField( pOut, TIFFTAG_IMAGEWIDTH, ( uint32 ) i->m_XRes );
 
116
            TIFFSetField( pOut, TIFFTAG_IMAGELENGTH, ( uint32 ) i->m_YRes );
 
117
            TIFFSetField( pOut, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT );
 
118
            TIFFSetField( pOut, TIFFTAG_SAMPLESPERPIXEL, i->m_SamplesPerElement );
 
119
            TIFFSetField( pOut, TIFFTAG_BITSPERSAMPLE, 8 );
 
120
            TIFFSetField( pOut, TIFFTAG_PLANARCONFIG, config );
 
121
            //TIFFSetField( pOut, TIFFTAG_COMPRESSION, compression );
 
122
            //if ( compression == COMPRESSION_JPEG )
 
123
            //TIFFSetField( pOut, TIFFTAG_JPEGQUALITY, quality );
 
124
            TIFFSetField( pOut, TIFFTAG_PHOTOMETRIC, photometric );
 
125
            TIFFSetField( pOut, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize( pOut, 0 ) );
 
126
 
 
127
            if ( i->m_SamplesPerElement == 4 )
 
128
                TIFFSetField( pOut, TIFFTAG_EXTRASAMPLES, 1, ExtraSamplesTypes );
 
129
 
 
130
            // Set the position tages in case we aer dealing with a cropped image.
 
131
            //TIFFSetField(pOut, TIFFTAG_XPOSITION, (float)CWXMin);
 
132
            //TIFFSetField(pOut, TIFFTAG_YPOSITION, (float)CWYMin);
 
133
 
 
134
            TqInt       linelen = i->m_XRes * i->m_SamplesPerElement;
 
135
            TqInt row;
 
136
            for ( row = 0; row < i->m_YRes; row++ )
 
137
            {
 
138
                if ( TIFFWriteScanline( pOut, i->m_pData + ( row * linelen ), row, 0 ) < 0 )
 
139
                    break;
 
140
            }
 
141
            TIFFClose( pOut );
 
142
        }
 
143
    }
 
144
    return ( 0 );
 
145
}
 
146
 
 
147
 
 
148
 
 
149
TqInt CqDDManagerSimple::DisplayBucket( IqBucket* pBucket )
 
150
{
 
151
    std::vector<SqDDevice>::iterator i;
 
152
    for ( i = m_aDisplayRequests.begin(); i != m_aDisplayRequests.end(); i++ )
 
153
    {
 
154
        TqInt   xmin = pBucket->XOrigin();
 
155
        TqInt   ymin = pBucket->YOrigin();
 
156
        TqInt   xsize = pBucket->Width();
 
157
        TqInt   ysize = pBucket->Height();
 
158
 
 
159
        for ( std::vector<SqDDevice>::iterator i = m_aDisplayRequests.begin(); i != m_aDisplayRequests.end(); i++ )
 
160
        {
 
161
            TqInt       samples = i->m_SamplesPerElement;
 
162
            TqInt       linelen = i->m_XRes * samples;
 
163
 
 
164
            RtInt mode = 0;
 
165
            if ( strstr( i->m_strMode.c_str(), RI_RGB ) != NULL )
 
166
                mode |= ModeRGB;
 
167
            if ( strstr( i->m_strMode.c_str(), RI_A ) != NULL )
 
168
                mode |= ModeA;
 
169
            if ( strstr( i->m_strMode.c_str(), RI_Z ) != NULL )
 
170
                mode |= ModeZ;
 
171
 
 
172
            SqImageSample val( QGetRenderContext()->GetOutputDataTotalSize() );
 
173
            TqInt y;
 
174
            for ( y = 0; y < ysize; y++ )
 
175
            {
 
176
                TqInt sy = y + ymin;
 
177
                TqInt x;
 
178
                for ( x = 0; x < xsize; x++ )
 
179
                {
 
180
                    TqInt sx = x + xmin;
 
181
                    TqInt so = ( sy * linelen ) + ( sx * samples );
 
182
                    // If outputting a zfile, use the midpoint method.
 
183
                    /// \todo Should really be generalising this section to use specif Filter/Expose/Quantize functions.
 
184
                    if ( mode & ModeZ )
 
185
                    {
 
186
                        i->m_pData[ so ] = static_cast<unsigned char>( pBucket->Depth( sx, sy ) );
 
187
                    }
 
188
                    else
 
189
                    {
 
190
                        if ( samples >= 3 )
 
191
                        {
 
192
                            CqColor col = pBucket->Color( sx, sy );
 
193
                            i->m_pData[ so + 0 ] = static_cast<unsigned char>( col.fRed() );
 
194
                            i->m_pData[ so + 1 ] = static_cast<unsigned char>( col.fGreen() );
 
195
                            i->m_pData[ so + 2 ] = static_cast<unsigned char>( col.fBlue() );
 
196
                            if ( samples == 4 )
 
197
                            {
 
198
                                CqColor o = pBucket->Opacity( sx, sy );
 
199
                                TqFloat a = ( o.fRed() + o.fGreen() + o.fBlue() ) / 3.0f;
 
200
                                i->m_pData[ so + 3 ] = static_cast<unsigned char>( a * pBucket->Coverage( sx, sy ) );
 
201
                            }
 
202
                        }
 
203
                        else if ( samples == 1 )
 
204
                        {
 
205
                            CqColor o = pBucket->Opacity( sx, sy );
 
206
                            TqFloat a = ( o.fRed() + o.fGreen() + o.fBlue() ) / 3.0f;
 
207
                            i->m_pData[ so + 0 ] = static_cast<unsigned char>( a * pBucket->Coverage( sx, sy ) );
 
208
                        }
 
209
                    }
 
210
                }
 
211
            }
 
212
        }
 
213
    }
 
214
    return ( 0 );
 
215
}
 
216
 
 
217
TqBool  CqDDManagerSimple::fDisplayNeeds( const TqChar* var )
 
218
{
 
219
    if ( strcmp( var, "rgba" ) == 0 )
 
220
        return ( TqTrue );
 
221
    else if ( strcmp( var, "rgb" ) == 0 )
 
222
        return ( TqTrue );
 
223
    else if ( strcmp( var, "a" ) == 0 )
 
224
        return ( TqTrue );
 
225
    else
 
226
        return ( TqFalse );
 
227
}
 
228
 
 
229
 
 
230
END_NAMESPACE( Aqsis )