Blame view

main.c 6.93 KB
4daa2ca3   Jean Wasilewski   Displaying a pixe...
1
  #include <X11/Xlib.h>
4daa2ca3   Jean Wasilewski   Displaying a pixe...
2
  #include <stdio.h>
262d40e6   Jean Wasilewski   Working version b...
3
4
  #include <stdlib.h>		/* getenv(), etc. */
  #include <unistd.h>		/* sleep(), etc.  */
4daa2ca3   Jean Wasilewski   Displaying a pixe...
5
  
262d40e6   Jean Wasilewski   Working version b...
6
7
8
9
10
11
12
  Window create_simple_window(Display* display, int width, int height, int x, int y)
  {
    int screen_num = DefaultScreen(display);
    int win_border_width = 2;
    Window win;
  
    win = XCreateSimpleWindow(display, RootWindow(display, screen_num), x, y, width, height, win_border_width, BlackPixel(display, screen_num), WhitePixel(display, screen_num));
a0ae785b   Jean Wasilewski   Non working version
13
  
262d40e6   Jean Wasilewski   Working version b...
14
15
    XMapWindow(display, win);
    XFlush(display);
4daa2ca3   Jean Wasilewski   Displaying a pixe...
16
  
262d40e6   Jean Wasilewski   Working version b...
17
18
    return win;
  }
4daa2ca3   Jean Wasilewski   Displaying a pixe...
19
  
262d40e6   Jean Wasilewski   Working version b...
20
  GC create_gc(Display* display, Window win, int reverse_video)
a0ae785b   Jean Wasilewski   Non working version
21
  {
262d40e6   Jean Wasilewski   Working version b...
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
    GC gc;
    unsigned long valuemask = 0;		/* which values in 'values' to  */
  					/* check when creating the GC.  */
    XGCValues values;			/* initial values for the GC.   */
    unsigned int line_width = 2;		/* line width for the GC.       */
    int line_style = LineSolid;		/* style for lines drawing and  */
    int cap_style = CapButt;		/* style of the line's edje and */
    int join_style = JoinBevel;		/*  joined lines.		*/
    int screen_num = DefaultScreen(display);
  
    gc = XCreateGC(display, win, valuemask, &values);
    if (gc < 0) {
  	fprintf(stderr, "XCreateGC: \n");
    }
  
    if (reverse_video)
    {
      XSetForeground(display, gc, WhitePixel(display, screen_num));
      XSetBackground(display, gc, BlackPixel(display, screen_num));
    }
    else
    {
      XSetForeground(display, gc, BlackPixel(display, screen_num));
      XSetBackground(display, gc, WhitePixel(display, screen_num));
    }
  
    XSetLineAttributes(display, gc, line_width, line_style, cap_style, join_style);
    XSetFillStyle(display, gc, FillSolid);
  
    return gc;
  }
4daa2ca3   Jean Wasilewski   Displaying a pixe...
53
  
262d40e6   Jean Wasilewski   Working version b...
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  int main(int argc, char* argv[])
  {
    Display* display;		/* pointer to X Display structure.           */
    int screen_num;		/* number of screen to place the window on.  */
    Window win;			/* pointer to the newly created window.      */
    unsigned int display_width,
                 display_height;	/* height and width of the X display.        */
    unsigned int width, height;	/* height and width for the new window.      */
    char *display_name = getenv("DISPLAY");  /* address of the X display.      */
    GC gc;			/* GC (graphics context) used for drawing    */
  				/*  in our window.			     */
    Colormap screen_colormap;     /* color map to use for allocating colors.   */
    XColor red, brown, blue, yellow, green;
  				/* used for allocation of the given color    */
  				/* map entries.                              */
    Status rc;			/* return status of various Xlib functions.  */
  	int initOffsetBMP;
  	XColor col;
4daa2ca3   Jean Wasilewski   Displaying a pixe...
72
  
262d40e6   Jean Wasilewski   Working version b...
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
    display = XOpenDisplay(display_name);
    if (display == NULL) {
      fprintf(stderr, "%s: cannot connect to X server '%s'\n",
              argv[0], display_name);
      exit(1);
    }
  
    screen_num = DefaultScreen(display);
    display_width = DisplayWidth(display, screen_num);
    display_height = DisplayHeight(display, screen_num);
  
    width = display_width;
    height = display_height;
    printf("window width - '%d'; height - '%d'\n", width, height);
  
    win = create_simple_window(display, width, height, 0, 0);
  
    gc = create_gc(display, win, 0);
    XSync(display, False);
    screen_colormap = DefaultColormap(display, DefaultScreen(display));
  
    rc = XAllocNamedColor(display, screen_colormap, "red", &red, &red);
    if (rc == 0) {
      fprintf(stderr, "XAllocNamedColor - failed to allocated 'red' color.\n");
      exit(1);
    }
    rc = XAllocNamedColor(display, screen_colormap, "brown", &brown, &brown);
    if (rc == 0) {
      fprintf(stderr, "XAllocNamedColor - failed to allocated 'brown' color.\n");
      exit(1);
    }
    rc = XAllocNamedColor(display, screen_colormap, "blue", &blue, &blue);
    if (rc == 0) {
      fprintf(stderr, "XAllocNamedColor - failed to allocated 'blue' color.\n");
      exit(1);
    }
    rc = XAllocNamedColor(display, screen_colormap, "yellow", &yellow, &yellow);
    if (rc == 0) {
      fprintf(stderr, "XAllocNamedColor - failed to allocated 'yellow' color.\n");
      exit(1);
    }
    rc = XAllocNamedColor(display, screen_colormap, "green", &green, &green);
    if (rc == 0) {
      fprintf(stderr, "XAllocNamedColor - failed to allocated 'green' color.\n");
      exit(1);
    }
    
    /* draw one pixel near each corner of the window */
    /* draw the pixels in a red color. */
    XSetForeground(display, gc, red.pixel);
    XDrawPoint(display, win, gc, 5, 5);
    XDrawPoint(display, win, gc, 5, height-5);
    XDrawPoint(display, win, gc, width-5, 5);
    XDrawPoint(display, win, gc, width-5, height-5);
155f7ac1   Jean Wasilewski   Some BMP recognition
127
128
129
130
131
132
133
134
135
136
137
138
139
  
  	FILE* home = NULL;
  	char buffer[4];
  	home = fopen("home.bmp", "r");
  	if(home == NULL)
  		return 0;
  
  	fseek(home, 0x0A, SEEK_SET);
  	buffer[0] = getc(home);
  	buffer[1] = getc(home);
  	buffer[2] = getc(home);
  	buffer[3] = getc(home);
  	printf("Pixel array offset %02x %02x %02x %02x\n", buffer[0], buffer[1], buffer[2], buffer[3]);
a0ae785b   Jean Wasilewski   Non working version
140
  	initOffsetBMP = buffer[0] + (buffer[1] >> 8) + (buffer[2] >> 16) + (buffer[3] >> 24);
155f7ac1   Jean Wasilewski   Some BMP recognition
141
142
  
  	fseek(home, 0x12, SEEK_SET);
262d40e6   Jean Wasilewski   Working version b...
143
144
145
146
147
  	buffer[0] = getc(home);
  	buffer[1] = getc(home);
  	buffer[2] = getc(home);
  	buffer[3] = getc(home);
  	printf("Width %02x %02x %02x %02x\n", buffer[0], buffer[1], buffer[2], buffer[3]);
155f7ac1   Jean Wasilewski   Some BMP recognition
148
149
  
  	//fseek(home, 0x12, SEEK_SET);
262d40e6   Jean Wasilewski   Working version b...
150
151
152
153
154
  	buffer[0] = getc(home);
  	buffer[1] = getc(home);
  	buffer[2] = getc(home);
  	buffer[3] = getc(home);
  	printf("Height %02x %02x %02x %02x\n", buffer[0], buffer[1], buffer[2], buffer[3]);
155f7ac1   Jean Wasilewski   Some BMP recognition
155
156
  
  	fseek(home, 0x1C, SEEK_SET);
262d40e6   Jean Wasilewski   Working version b...
157
158
159
  	buffer[0] = getc(home);
  	buffer[1] = getc(home);
  	printf("Bits Per Pixel %02x %02x\n", buffer[0], buffer[1]);
155f7ac1   Jean Wasilewski   Some BMP recognition
160
161
162
163
164
165
166
167
  
  	if(buffer[1] != 0 || buffer[0] != 0x18)
  	{
  		printf("Could not read BMP\n");
  		return 1;
  	}
  
  
a0ae785b   Jean Wasilewski   Non working version
168
  	fseek(home, initOffsetBMP, SEEK_SET);
155f7ac1   Jean Wasilewski   Some BMP recognition
169
  
262d40e6   Jean Wasilewski   Working version b...
170
  	col.flags= DoRed | DoGreen | DoBlue;
4daa2ca3   Jean Wasilewski   Displaying a pixe...
171
  	
262d40e6   Jean Wasilewski   Working version b...
172
173
174
175
176
177
178
179
180
  	int i;
  	for(i=0; i<76800; i++)
  	{
  		col.red = buffer[2] * 257;
  		col.green = buffer[1] * 257;
  		col.blue = buffer[0] * 257;
  
  		if(XAllocColor(display, screen_colormap, &col) == 0)
  			printf("Failed to allocate color\n");
10b7eef7   henyxia   Separated color a...
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  		buffer[0] = getc(home);
  		buffer[1] = getc(home);
  		buffer[2] = getc(home);
  		printf("Alloc X %03d Y %03d R %02x G %02x B %02x\n", i%width, i/width, buffer[0], buffer[1], buffer[2]);
  	}
  
  
  	fseek(home, initOffsetBMP, SEEK_SET);
  	
  	for(i=0; i<76800; i++)
  	{
  		col.red = buffer[2] * 257;
  		col.green = buffer[1] * 257;
  		col.blue = buffer[0] * 257;
  
262d40e6   Jean Wasilewski   Working version b...
196
197
198
199
200
201
202
  		if(XSetForeground(display, gc, col.pixel) == 0)
  			printf("Failed to set foreground\n");
  		if(XDrawPoint(display,win,gc,i%width,height-1-(i/width)) == 0)
  			printf("Failed to draw point\n");
  		buffer[0] = getc(home);
  		buffer[1] = getc(home);
  		buffer[2] = getc(home);
10b7eef7   henyxia   Separated color a...
203
  		printf("Disp X %03d Y %03d R %02x G %02x B %02x\n", i%width, i/width, buffer[0], buffer[1], buffer[2]);
4daa2ca3   Jean Wasilewski   Displaying a pixe...
204
  	}
4daa2ca3   Jean Wasilewski   Displaying a pixe...
205
  
262d40e6   Jean Wasilewski   Working version b...
206
207
  
  
262d40e6   Jean Wasilewski   Working version b...
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
    /* draw two intersecting lines, one horizontal and one vertical, */
    /* which intersect at point "50,100".                            */
    /* draw the line in a brown color. */
    //XSetForeground(display, gc, brown.pixel);
    //XDrawLine(display, win, gc, 50, 0, 50, 200);
    //XDrawLine(display, win, gc, 0, 100, 200, 100);
  
    /* flush all pending requests to the X server. */
    XFlush(display);
  
    /* make a delay for a short period. */
    sleep(4);
  
    /* close the connection to the X server. */
    XCloseDisplay(display);
    return(0);
  }