void draw_image(TDisplayBuffer* pixel_matrix,u8* img,s16 height,s16 width,s16 x0,s16 y0){ u8 c; s16 i, j; s32 img_iter=0; s32 index,step; TDisplayBuffer* img_0 = (TDisplayBuffer *) img; step=SIZE_OF_DISPLAY_Y-height; index=SIZE_OF_DISPLAY_Y*x0+y0; //dont display pictures that doesent fit screen if( (y0 > SIZE_OF_DISPLAY_Y) || (y0 + height < 0) || (x0 > SIZE_OF_DISPLAY_X) || (x0 + width < 0) ) return; for (i=0;i<width;i++) { for (j=0;j<height;j++) { if ( ( (y0+j) < SIZE_OF_DISPLAY_Y ) && ( (y0+j)>=0 ) && ( (x0+i) < SIZE_OF_DISPLAY_X ) && ( (x0+i)>=0 ) ) //check if pixel should be putted in buffer { c=img_0[img_iter>>3] & (1<< (7 -(img_iter & 7))); // read img_iter bit from img[] if (c != 0) draw_dot_index(pixel_matrix,index,dsON); else draw_dot_index(pixel_matrix,index,dsOFF); } img_iter++; index++; } index+=step; }}
void draw_image(TDisplayBuffer* pixel_matrix,u8* img,s16 height,s16 width,s16 x0,s16 y0){ u8 c; s16 i, j; s32 img_iter=0; s32 index,step; TDisplayBuffer* img_0 = (TDisplayBuffer *) img; step=SIZE_OF_DISPLAY_Y-height; index=SIZE_OF_DISPLAY_Y*x0+y0; if( (y0 > SIZE_OF_DISPLAY_Y) || (y0 + height < 0) ) return; if( (x0 > SIZE_OF_DISPLAY_X) || (x0 + width < 0) ) return; for (i=0;i<width;i++) { if(((x0+i) < SIZE_OF_DISPLAY_X) && ((x0+i)>=0 )) { for (j=0;j<height;j++) { if ( ((y0+j) < SIZE_OF_DISPLAY_Y) && ((y0+j)>=0 ) ) //check if pixel should be putted in buffer { c=img_0[img_iter>>3] & (1<< (7 -(img_iter & 7))); // read img_iter bit from img[] if (c != 0) draw_dot_index(pixel_matrix,index,dsON); else draw_dot_index(pixel_matrix,index,dsOFF); } img_iter++; index++; } } else { img_iter+=height; index+=height; } index+=step; }}
//3rd function whitout if-s in for loopsvoid draw_image(TDisplayBuffer* pixel_matrix,u8* img,s16 height,s16 width,s16 x0,s16 y0){ u8 c; s16 i, j; s32 img_iter=0; s32 index,step; s16 width_s,height_s; TDisplayBuffer* img_0 = (TDisplayBuffer *) img; s32 iter_step = 0; //unsigned int dbg; step=SIZE_OF_DISPLAY_Y-height; index=SIZE_OF_DISPLAY_Y*x0+y0; if( (y0 > SIZE_OF_DISPLAY_Y) || (y0 + height < 0) ) return; if( (x0 > SIZE_OF_DISPLAY_X) || (x0 + width < 0) ) return; if( y0 + height > SIZE_OF_DISPLAY_Y ) { height_s = SIZE_OF_DISPLAY_Y - y0; step=SIZE_OF_DISPLAY_Y-height_s; iter_step+=y0 + height - SIZE_OF_DISPLAY_Y; } else if(y0 < 0 ) { height_s = height + y0 ; step=SIZE_OF_DISPLAY_Y-height_s; img_iter=img_iter + y0 * (-1); iter_step+=y0*(-1); y0 = 0; } else { height_s = height; } if( x0 + width > SIZE_OF_DISPLAY_X ) { width_s = SIZE_OF_DISPLAY_X - x0; } else if(x0 < 0 ) { width_s = width + x0; img_iter=height*x0*(-1); x0 = 0; } else { width_s = width; } index=SIZE_OF_DISPLAY_Y*x0+y0; for (i=0;i<width_s;i++) { for (j=0;j<height_s;j++) { c=img_0[img_iter>>3] & (1<< (7 -(img_iter & 7))); // read img_iter bit from img[] if (c != 0) draw_dot_index(pixel_matrix,index,dsON); else draw_dot_index(pixel_matrix,index,dsOFF); img_iter++; index++; } img_iter+=iter_step; index+=step; }}
void draw_dot_index(TDisplayBuffer * pixel_matrix,s32 index,TDispStatus status){ u8 * matrix = (u8 *) pixel_matrix; if(status == dsON) matrix[index>>3] |= (0x01 << (index & 7)); else if(status == dsOFF) matrix[index>>3] &= ~(0x01 << (index & 7)); else if(status == dsTOGGLE) matrix[index>>3] ^= (0x01 << (index & 7));}
typedef unsigned char byte;typedef unsigned char u8;typedef signed char s8;typedef unsigned short u16;typedef signed short s16;typedef unsigned int u32;typedef signed int s32;typedef u8 TDisplayBuffer;#define SIZE_OF_DISPLAY_X 128#define SIZE_OF_DISPLAY_Y 128void test_routine(void){ TDisplayBuffer db=[SIZE_OF_DISPLAY_X*SIZE_OF_DISPLAY_Y/8]; //display buffer u8 img[4*4/8]; //image of size 4*4 int i,e; for(i=0;i<sizeof(db);i++) db[i]=0; //make buffer empty for(i=0;i<sizeof(img);i++) img[i]=0xff; //make picture black draw_image(db,img,4,4,2,-2); //put picture on screen e=0; for(i=0;i<sizeof(db);i++); { if(db) e++; } if(e==0) printf("there was no picture -> error"); else printf("this functon works ok (hopefuly)");}