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