Hello
I have been trying to teach myself to program for the G15 by making small applications for the display. The g15r_drawIcon has me a bit baffled though as the icons I try to draw come up shortened in both x and y directions.
I have inserted the code for the application below:
<br />
/*<br />
* g15icon.c: simple icon printing application<br />
* Compile with:<br />
* gcc -o g15icon g15icon.c -lg15render -lg15daemon_client<br />
*/</p>
<p>#include<br />
#include<br />
#include </p>
<p>int main(int argc, char **argv)<br />
{<br />
int screen = 0;<br />
struct g15canvas canvas;<br />
char icon3x3[] = {0xE0, 0xA0, 0xE0};<br />
char icon4x4[] = {0xF0, 0x90, 0x90, 0xF0};<br />
char icon8x8[] = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55};<br />
char icon16x4[] = {0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55};</p>
<p> /* Initialize screen */<br />
screen = new_g15_screen(G15_G15RBUF);</p>
<p> /* Render screen */<br />
g15r_clearScreen(&canvas, G15_COLOR_WHITE);</p>
<p> g15r_drawIcon(&canvas, icon3x3, 32, 8, 3, 3);</p>
<p> g15r_drawIcon(&canvas, icon4x4, 24, 8, 4, 4);</p>
<p> g15r_drawIcon(&canvas, icon8x8, 8, 8, 8, 8);</p>
<p> g15r_drawIcon(&canvas, icon16x4, 48, 8, 16, 4);</p>
<p> g15_send(screen, (char*)canvas.buffer, G15_BUFFER_LEN);</p>
<p> getchar();</p>
<p> g15_close_screen(screen);</p>
<p> return 0;<br />
}<br />
I use:
libg15 v. 1.2.6
libg15render v. 1.2
g15daemon v. 1.9.5.3
I looked up the definition of WBMP on Wikipedia. Am I wrong to use this?
The two largest icons are the most interesting, and best show what the problem is. The 8x8 icon is supposed to show a full 8x8 checkerboard but only shows a 7x7 checkerboard, and it is impossible to expand the boundaries of the g15r_drawIcon call without it resulting in complete distortion of the image.
What am I doing wrong?
Re: Programming C for libg15render
It uses XBM instead of WBMP as I've found it easier to work with, as you can just use The Gimp to convert an image
to XBM and save it, then open the file and just copy the text to your source code.
<br /> static void g15rx_drawXBM(g15canvas *canvas, unsigned char* data, int width, int height ,int pos_x, int pos_y)<br /> {<br /> int y = 0;<br /> int z = 0;<br /> unsigned char byte;<br /> int bytes_per_row = ceil((double) width / 8);</p> <p> int bits_left = width;<br /> int current_bit = 0;</p> <p> for(y = 0; y < height; y ++)<br /> {<br /> bits_left = width;<br /> for(z=0;z < bytes_per_row; z++)<br /> {<br /> byte = data[(y * bytes_per_row) + z];<br /> current_bit = 0;<br /> while(current_bit < 8)<br /> {<br /> if(bits_left > 0)<br /> {<br /> if((byte >> current_bit) & 1) g15r_setPixel(canvas, (current_bit + (z*8) + pos_x),y + pos_y,G15_COLOR_BLACK);<br /> bits_left--;<br /> }<br /> current_bit++;<br /> }<br /> }<br /> }<br /> }<br />Ever thankful for this! Works like a charm.
Re: Programming C for libg15render
Thanks. I'll look into it.
Re: Programming C for libg15render
Should you be interested, I wrote the following function a while ago for the audacious plugin I was working on.
It uses XBM instead of WBMP as I've found it easier to work with, as you can just use The Gimp to convert an image
to XBM and save it, then open the file and just copy the text to your source code.
<br /> static void g15rx_drawXBM(g15canvas *canvas, unsigned char* data, int width, int height ,int pos_x, int pos_y)<br /> {<br /> int y = 0;<br /> int z = 0;<br /> unsigned char byte;<br /> int bytes_per_row = ceil((double) width / 8);</p> <p> int bits_left = width;<br /> int current_bit = 0;</p> <p> for(y = 0; y < height; y ++)<br /> {<br /> bits_left = width;<br /> for(z=0;z < bytes_per_row; z++)<br /> {<br /> byte = data[(y * bytes_per_row) + z];<br /> current_bit = 0;<br /> while(current_bit < 8)<br /> {<br /> if(bits_left > 0)<br /> {<br /> if((byte >> current_bit) & 1) g15r_setPixel(canvas, (current_bit + (z*8) + pos_x),y + pos_y,G15_COLOR_BLACK);<br /> bits_left--;<br /> }<br /> current_bit++;<br /> }<br /> }<br /> }<br /> }<br />Re: Programming C for libg15render
Changing the loop limits does indeed rectify the problems with icon sizes. However, the 4x4 and 3x3 icons are still not shown correctly (Is there a way to take a screenshot?).
The 4x4 and 3x3 icons are supposed to be boxes. They instead come out rather like an ill-defined blob.
Also, if I'm correct in assuming that 1 (on) is supposed to mean non-background color (as I understand there are 2 versions of the G15, I have the orange one), then I believe g15r_drawIcon is reversing the bits' color.
The new code:
<br /> void drawIcon(struct g15canvas *canvas, unsigned char *buf, int my_x, int my_y, int width, int height)<br /> {<br /> int y,x,val;<br /> unsigned int pixel_offset = 0;<br /> unsigned int byte_offset, bit_offset;</p> <p> for (y=0; y < height; y++)<br /> for (x=0; x < width; x++)<br /> {<br /> pixel_offset = y * width + x;<br /> byte_offset = pixel_offset / BYTE_SIZE;<br /> bit_offset = 7 - (pixel_offset % BYTE_SIZE);</p> <p> val = (buf[byte_offset] & (1 << bit_offset)) >> bit_offset;<br /> g15r_setPixel (canvas, x + my_x, y + my_y, val);<br /> }<br /> }<br />I had also missed a part of the WBMP specification. Here is a new and improved version of my code:
<br /> void drawIcon(struct g15canvas *c, unsigned char *icon, int x, int y, int w, int h)<br /> {<br /> int i, j, count = 0;</p> <p> for(i = 0; i < h; i++)<br /> {<br /> for(j = 0; j < w; j++)<br /> {<br /> if(count > 7)<br /> {<br /> count = 0;<br /> icon++;<br /> }<br /> g15r_setPixel(c, j+x, i+y, ((*icon >> (7 - count)) & 1) ^ 1);<br /> count++;<br /> }<br /> icon++;<br /> count = 0;<br /> }<br /> }<br />Re: Programming C for libg15render
The code for g15r_drawIcon() is quite similar to what you have, though there may be an off-by-one error. See http://www.g15tools.com/docs-g15r/pixel_8c.html#f9c1fbc33fdcb45c32ec6579.... Can you try changing lines 417 and 418 of pixel.c to just width and height rather that width -1, height -1.
Re: Programming C for libg15render
Well, I'm quite sure it only meant the data.
I wrote my own drawIcon function and it works.
<br /> void drawIcon(struct g15canvas *c, unsigned char *icon, int x, int y, int w, int h)<br /> {<br /> int i, j, count = 0;</p> <p> for(i = 0; i < h; i++)<br /> {<br /> for(j = 0; j < w; j++)<br /> {<br /> if(count > 7)<br /> {<br /> count = 0;<br /> icon++;<br /> }<br /> g15r_setPixel(c, j+x, i+y, ((*icon >> count) & 1) ^ 1);<br /> count++;<br /> }<br /> icon++;<br /> count = 0;<br /> }<br /> }<br />So I have a solution, though it would be more elegant to use a built-in function.
Thank You for your help, anyway.
Re: Programming C for libg15render
Well as it said a "wbmp buffer" perhaps it means header too :>? That was my only thing, but you are probably correct.
Re: Programming C for libg15render
I am using the reference in the man-page for libg15render, so if that is correct I should be using the right data-types. Where do you mean I should be using the (0, 0, 8, 8, icon)? The call and the parameters work (aside from the problem mentioned). I get no complaints from C about types or anything.
As for the other question: If I write the height as 9 I get the missing line that should be there, however if I extend the width to 9 the entire image is corrupted, which makes me think that the numbers I have input are probably correct. Or at least follow the original intentions of the authors of libg15render.
I have seriously considered looking at the source code. That will be my next option, for sure.
Re: Programming C for libg15render
Well if that format is correct, then shouldn't you send something along the lines of
(unsigned int) 0
(char) 0
(unsigned int) 8
(unsigned int) 8
(And now your char icon8x8)
?
If all else fails, open up the sourcecode for libg15render and check why it won't work :>
What does sending the height & width as 9 do?
What does the largest bitmap do?