UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
dxt_common.c
Go to the documentation of this file.
1
27#include "dxt_common.h"
28#include "dxt_util.h"
29
31int
33{
34 int argc = 0;
35 glutInit(&argc, NULL);
36 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
37 glutCreateWindow("DXT");
38 glutHideWindow();
39 glewInit();
40
41 int result = 0;
42
43#define CHECK_EXTENSION(extension) \
44 if ( !glewIsSupported(extension) ) { \
45 fprintf(stderr, "OpenGL extension '%s' is missing!\n", extension); \
46 result = -1; \
47 } \
48
49 CHECK_EXTENSION("GL_VERSION_2_0");
50 CHECK_EXTENSION("GL_ARB_vertex_program");
51 CHECK_EXTENSION("GL_ARB_fragment_program");
52 CHECK_EXTENSION("GL_EXT_gpu_shader4");
53 CHECK_EXTENSION("GL_EXT_framebuffer_object");
54 CHECK_EXTENSION("GL_ARB_texture_compression");
55 CHECK_EXTENSION("GL_EXT_texture_compression_s3tc");
56 CHECK_EXTENSION("GL_EXT_texture_integer");
57
58 printf("Graphic Card: %s\n", glGetString(GL_RENDERER));
59 printf("OpenGL version: %s\n", glGetString(GL_VERSION));
60 printf("GLSL version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
61
62 return result;
63}
64
66int
67dxt_image_load_from_file(const char* filename, int width, int height, DXT_IMAGE_TYPE** image)
68{
69 FILE* file;
70 file = fopen(filename, "rb");
71 if ( !file ) {
72 fprintf(stderr, "Failed open %s for reading!\n", filename);
73 return -1;
74 }
75
76 int data_size = width * height * 3;
77 unsigned char* data = (unsigned char*)malloc(data_size * sizeof(unsigned char));
78 if ( (size_t) data_size != fread(data, sizeof(unsigned char), data_size, file) ) {
79 fprintf(stderr, "Failed to load image data [%d bytes] from file %s!\n", data_size, filename);
80 return -1;
81 }
82 fclose(file);
83
84 *image = (DXT_IMAGE_TYPE*)malloc(width * height * 4 * sizeof(DXT_IMAGE_TYPE));
85 for ( int y = 0; y < height; y++ ) {
86 for ( int x = 0; x < width; x++ ) {
87 int index = 4 * ((y * width) + x);
88 int index_data = 3 * ((y * width) + x);
89 (*image)[index + 0] = (DXT_IMAGE_TYPE)data[index_data + 0];
90 (*image)[index + 1] = (DXT_IMAGE_TYPE)data[index_data + 1];
91 (*image)[index + 2] = (DXT_IMAGE_TYPE)data[index_data + 2];
92 (*image)[index + 3] = 1.0;
93 }
94 }
95
96 free(data);
97
98#ifdef DEBUG
99 printf("Load Image [file: %s, size: %d bytes, resolution: %dx%d]\n", filename, data_size, width, height);
100#endif
101
102 return 0;
103}
104
106int
107dxt_image_save_to_file(const char* filename, DXT_IMAGE_TYPE* image, int width, int height)
108{
109 FILE* file;
110 file = fopen(filename, "wb");
111 if ( !file ) {
112 fprintf(stderr, "Failed open %s for writing!\n", filename);
113 return -1;
114 }
115
116 int data_size = width * height * 3;
117 unsigned char* data = (unsigned char*)malloc(data_size * sizeof(unsigned char));
118 for ( int y = 0; y < height; y++ ) {
119 for ( int x = 0; x < width; x++ ) {
120 int index = 4 * ((y * width) + x);
121 int index_data = 3 * ((y * width) + x);
122 data[index_data + 0] = (unsigned char)(image[index + 0]);
123 data[index_data + 1] = (unsigned char)(image[index + 1]);
124 data[index_data + 2] = (unsigned char)(image[index + 2]);
125 }
126 }
127
128 if ( (size_t) data_size != fwrite(data, sizeof(unsigned char), data_size, file) ) {
129 fprintf(stderr, "Failed to write image data [%d bytes] to file %s!\n", width * height * 3, filename);
130 free(data);
131 return -1;
132 }
133 free(data);
134 fclose(file);
135
136#ifdef DEBUG
137 printf("Save Image [file: %s, size: %d bytes, resolution: %dx%d]\n", filename, data_size, width, height);
138#endif
139
140 return 0;
141}
142
144int
145dxt_image_compressed_load_from_file(const char* filename, unsigned char** data, int* data_size)
146{
147 FILE* file;
148 file = fopen(filename, "rb");
149 if ( !file ) {
150 fprintf(stderr, "Failed open %s for reading!\n", filename);
151 return -1;
152 }
153
154 fseek(file, 0, SEEK_END);
155 *data_size = ftell(file);
156 rewind(file);
157 *data = (unsigned char*)malloc(*data_size * sizeof(unsigned char));
158 if ( (size_t) *data_size != fread(*data, sizeof(unsigned char), *data_size, file) ) {
159 fprintf(stderr, "Failed to load image data [%d bytes] from file %s!\n", *data_size, filename);
160 return -1;
161 }
162 fclose(file);
163
164#ifdef DEBUG
165 printf("Load Compressed Image [file: %s, size: %d bytes]\n", filename, *data_size);
166#endif
167
168 return 0;
169}
170
172int
173dxt_image_compressed_save_to_file(const char* filename, unsigned char* image, int image_size)
174{
175 FILE* file;
176 file = fopen(filename, "wb");
177 if ( !file ) {
178 fprintf(stderr, "Failed open %s for writing!\n", filename);
179 return -1;
180 }
181
182 if ( (size_t) image_size != fwrite(image, sizeof(unsigned char), image_size, file) ) {
183 fprintf(stderr, "Failed to write image data [%d bytes] to file %s!\n", image_size, filename);
184 return -1;
185 }
186 fclose(file);
187
188#ifdef DEBUG
189 printf("Save Compressed Image [file: %s, size: %d bytes]\n", filename, image_size);
190#endif
191
192 return 0;
193}
194
196int
198{
199 free(image);
200
201 return 0;
202}
203
205int
206dxt_image_compressed_destroy(unsigned char* image_compressed)
207{
208 free(image_compressed);
209
210 return 0;
211}
int dxt_image_compressed_load_from_file(const char *filename, unsigned char **data, int *data_size)
Definition dxt_common.c:145
int dxt_image_destroy(DXT_IMAGE_TYPE *image)
Definition dxt_common.c:197
int dxt_image_load_from_file(const char *filename, int width, int height, DXT_IMAGE_TYPE **image)
Definition dxt_common.c:67
int dxt_image_save_to_file(const char *filename, DXT_IMAGE_TYPE *image, int width, int height)
Definition dxt_common.c:107
int dxt_image_compressed_destroy(unsigned char *image_compressed)
Definition dxt_common.c:206
int dxt_image_compressed_save_to_file(const char *filename, unsigned char *image, int image_size)
Definition dxt_common.c:173
#define CHECK_EXTENSION(extension)
int dxt_init()
Definition dxt_common.c:32
#define DXT_IMAGE_TYPE
Definition dxt_common.h:71