-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinux_display.c
More file actions
165 lines (135 loc) · 3.34 KB
/
linux_display.c
File metadata and controls
165 lines (135 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
ECE 468 - Lab 1
Written By: Taylor Steinberg
Instructor: Adam Hoover
Date: Jan 22, 2014
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int main(int argc, char **argv) {
Display *Monitor;
Window ImageWindow;
GC ImageGC;
XWindowAttributes Atts;
XImage *Picture;
int ROWS;
int COLS;
unsigned char *displaydata;
char colorCode[3];
char maxColorVal[4];
// Check input paramters
if(argc != 2) {
printf("Invalid input paramters\n");
return 0;
}
// Open file pointer
FILE *fp = NULL;
fp = fopen(argv[1], "rb");
if(fp == NULL) {
printf("Unable to open file\n");
return 0;
}
// Read in file header
int err = 0;
int iteration = 0;
while(err != 4) {
err = fscanf(fp, "%s %d %d %s", colorCode, &COLS, &ROWS, maxColorVal);
iteration++;
if(iteration == 10) {
printf("ERROR: Unable to read file after maximum iterations\nExiting...\n");
exit(1);
}
}
printf("Type = %s, ROWS = %d, COLS = %d, Max Val = %s\n", colorCode, ROWS, COLS, maxColorVal);
// Determine B/W or RGB
int bytesPerColor;
if(strcmp(colorCode, "P5") == 0) {
bytesPerColor = 1;
} else if(strcmp(colorCode, "P6") == 0) {
bytesPerColor = 3;
} else {
printf("ERROR: Invalid File Type\nExiting...\n");
exit(1);
}
int totalBytes = ROWS * COLS * bytesPerColor;
fseek(fp, sizeof(*displaydata), 1); // Jump Junk Data
// Read in binary data
unsigned char *inputData = NULL;
inputData = (unsigned char *) calloc(sizeof(*inputData), totalBytes);
err = 0;
iteration = 0;
while(err != totalBytes) {
err = fread(inputData, sizeof(*inputData), totalBytes, fp);
iteration++;
if(iteration == 3) {
printf("Unable to read file after maximum iterations\nExiting...\n");
exit(1);
}
}
// Convert to 16-bit Color
int totalBytes16Bit = ROWS*COLS*2;
displaydata = calloc(sizeof(*displaydata), totalBytes16Bit);
int i;
for(i = 0; i < totalBytes16Bit; i+=2) {
unsigned char redBits = 0x00;
unsigned char greenBits = 0x00;
unsigned char blueBits = 0x00;
if(bytesPerColor == 1) {
redBits = inputData[i/2] & 0xF8;
greenBits = inputData[i/2] & 0xFC;
blueBits = redBits;
} else {
redBits = inputData[(3*i)/2] & 0xF8;
greenBits = inputData[(3*i)/2+1] & 0xFC;
blueBits = inputData[(3*i)/2+2] & 0xF8;
}
displaydata[i+1] |= redBits | (greenBits >> 5);
displaydata[i] |= (greenBits << 3) | (blueBits >> 3);
}
Monitor=XOpenDisplay(NULL);
if (Monitor == NULL) {
printf("Unable to open graphics display\n");
exit(0);
}
ImageWindow=XCreateSimpleWindow(
Monitor,
RootWindow(Monitor,0),
50,
10, /* x,y on screen */
COLS,
ROWS, /* width, height */
2, /* border width */
BlackPixel(Monitor,0),
WhitePixel(Monitor,0)
);
ImageGC=XCreateGC(Monitor,ImageWindow,0,NULL);
XMapWindow(Monitor,ImageWindow);
XFlush(Monitor);
while(1) {
XGetWindowAttributes(Monitor,ImageWindow,&Atts);
if (Atts.map_state == IsViewable) break;
}
Picture=XCreateImage(Monitor, DefaultVisual(Monitor,0), DefaultDepth(Monitor,0), ZPixmap, 0, (char *)displaydata, COLS, ROWS, 16, 0);
XPutImage(
Monitor,
ImageWindow,
ImageGC,
Picture,
0,
0,
0,
0, /* src x,y and dest x,y offsets */
COLS,
ROWS
); /* size of the image data */
XFlush(Monitor);
printf("Press any key to quit...\n");
getchar();
XCloseDisplay(Monitor);
fclose(fp);
return 0;
}