source: trunk/packages/vizservers/nanovis/ScreenSnapper.cpp @ 1018

Last change on this file since 1018 was 953, checked in by gah, 16 years ago

remove warnings from compile

File size: 3.8 KB
Line 
1
2/*
3 * ----------------------------------------------------------------------
4 * ScreenSnapper.h: ScreenSnapper class. It captures the render result
5 *                      and stores it in an array of chars or floats
6 *                      depending on chosen format.
7 *
8 * ======================================================================
9 *  AUTHOR:  Wei Qiao <qiaow@purdue.edu>
10 *           Purdue Rendering and Perceptualization Lab (PURPL)
11 *
12 *  Copyright (c) 2004-2006  Purdue Research Foundation
13 *
14 *  See the file "license.terms" for information on usage and
15 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 * ======================================================================
17 */
18
19#include <memory.h>
20#include <assert.h>
21#include "ScreenSnapper.h"
22
23ScreenSnapper::ScreenSnapper(int w, int h, NVISdatatype type,
24                             int channel_per_pixel)
25{
26    width = w;
27    height = h;
28
29    //only allow two types
30    assert(type == NVIS_FLOAT || type == NVIS_UNSIGNED_BYTE);
31    data_type = type;
32 
33    //only allow RGB or RGBA
34    assert(channel_per_pixel == 3 || channel_per_pixel == 4);
35    n_channels_per_pixel = channel_per_pixel;
36
37    if(type == NVIS_FLOAT)
38        data = new float[w*h*channel_per_pixel];
39    else if(type == NVIS_UNSIGNED_BYTE)
40        data = new unsigned char[w*h*channel_per_pixel];
41 
42    assert(data!=0);
43    reset(0);   //reset data
44}
45
46ScreenSnapper::~ScreenSnapper()
47{
48    if(data_type == NVIS_FLOAT)
49        delete[] (float*)data;
50    else if(data_type == NVIS_UNSIGNED_BYTE)
51        delete[] (unsigned char*)data;
52}
53
54void
55ScreenSnapper::reset(char c)
56{
57    unsigned int elemSize;
58    switch (data_type) {
59    case NVIS_FLOAT:
60        elemSize = sizeof(float);
61        break;
62
63    case NVIS_UNSIGNED_BYTE:
64        elemSize = sizeof(unsigned char);
65        break;
66
67    default:
68        assert(0);
69        break;
70    }
71    unsigned int size;
72    size = elemSize * width * height * n_channels_per_pixel;
73    memset(data, size, c);
74}
75
76void
77ScreenSnapper::capture()
78{
79    if(data_type == NVIS_FLOAT){
80        if(n_channels_per_pixel==3)
81            glReadPixels(0, 0, width, height, GL_RGB, GL_FLOAT, data);
82        else if(n_channels_per_pixel==4)
83            glReadPixels(0, 0, width, height, GL_RGBA, GL_FLOAT, data);
84    }
85    else if(data_type == NVIS_UNSIGNED_BYTE){
86        if(n_channels_per_pixel==3)
87            glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
88        else if(n_channels_per_pixel==4)
89            glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
90    }
91    assert(glGetError()==0);
92}
93
94void
95ScreenSnapper::print()
96{
97    for(int i=0; i<width*height; i++){
98        if(data_type == NVIS_FLOAT){
99            if(n_channels_per_pixel==3)
100                fprintf(stderr, "(%f %f %f) ",
101                        ((float*)data)[3*i],
102                        ((float*)data)[3*i+1],
103                        ((float*)data)[3*i+2]);
104            else if(n_channels_per_pixel==4)
105                fprintf(stderr, "(%f %f %f %f) ",
106                        ((float*)data)[4*i],
107                        ((float*)data)[4*i+1],
108                        ((float*)data)[4*i+2],
109                        ((float*)data)[4*i+3]);
110        }
111        else if(data_type == NVIS_UNSIGNED_BYTE){
112            if(n_channels_per_pixel==3)
113                fprintf(stderr, "(%d %d %d) ",
114                        ((unsigned char*)data)[3*i],
115                        ((unsigned char*)data)[3*i+1],
116                        ((unsigned char*)data)[3*i+2]);
117            else if(n_channels_per_pixel==4)
118                fprintf(stderr, "(%d %d %d %d) ",
119                        ((unsigned char*)data)[4*i],
120                        ((unsigned char*)data)[4*i+1],
121                        ((unsigned char*)data)[4*i+2],
122                        ((unsigned char*)data)[4*i+3]);
123        }
124    }
125}
126
Note: See TracBrowser for help on using the repository browser.