Advertisement
Guest User

Untitled

a guest
Mar 17th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1. #define DIV8(n) (((n) + 7) / 8)
  2. #define ORDENA(a, b) {if (a > b) {int temp = a; a = b; b = temp;}}
  3. #define LIGA_SEGURO(img, y, x) if (x < me->colunas && x >= 0 && y < me->linhas && y >= 0) me->bitmap[y][(x)/8] |= 128 >> ((x)%8);
  4.  
  5.  
  6. pos_BwImage *pos_BwImage_new(int lin, int col) {
  7.    pos_BwImage *res;
  8.    unsigned char **bitmap;
  9.    unsigned char *data;
  10.    res = (pos_BwImage *)pos_malloc(sizeof(pos_BwImage));
  11.    if (res == NULL)
  12.       return NULL;
  13.  
  14.    bitmap = (unsigned char **)pos_malloc(sizeof(unsigned char*)*lin);
  15.    if (bitmap == NULL) {
  16.       pos_free(res);
  17.       return NULL;
  18.    }
  19.    data = (unsigned char *)pos_calloc(lin*DIV8(col), sizeof(unsigned char));
  20.    if (data == NULL) {
  21.       pos_free(bitmap);
  22.       pos_free(res);
  23.       return NULL;
  24.    }
  25.    pos_BwImage_init(res, lin, col, bitmap, data);
  26.    res->modoAlocacao = 2; // tudo mallocado
  27.    return res;
  28. }
  29.  
  30. pos_BwImage *pos_BwImage_init(pos_BwImage *image, int lin, int col, unsigned char **lines, unsigned char *data) {
  31.    int i;
  32.    image->linhas = lin;
  33.    image->colunas = col;
  34.    image->modoAlocacao = 0;
  35.    image->bitmap = lines;
  36.    for (i = 0; i < lin; i++) {
  37.       image->bitmap[i] = data + (i*DIV8(col));
  38.    }
  39.    return image;
  40. }
  41.  
  42. void pos_BwImage_drawRect(pos_BwImage *me, int x1, int y1, int x2, int y2) {
  43.    int i;
  44.  
  45.    ORDENA(x1,x2);
  46.    ORDENA(y1,y2);
  47.  
  48.    for (i=y1; i<y2;i++)
  49.    {
  50.       LIGA_SEGURO(img,i,x1);
  51.       LIGA_SEGURO(img,i,x2);
  52.    }
  53.    for (i=x1; i<=x2;i++)
  54.    {
  55.       LIGA_SEGURO(img,y1,i);
  56.       LIGA_SEGURO(img,y2,i);
  57.    }
  58. }
  59.  
  60. int main(void) {
  61.    pos_BwImage *im1;
  62.     pos_BwImage *im2;
  63.     pos_BwImage *im3;
  64.    pos_prn_Code status;
  65.    
  66.    im1 = pos_BwImage_new(1, 1);
  67.    im2 = pos_BwImage_new(123, 345); //y=123 e x=345
  68.    
  69.    status = pos_BwImage_print(im1);
  70.    status = pos_BwImage_print(im2);
  71.    
  72.    pos_BwImage_drawRect(im1, 0, 0, 10, 10);
  73.    status = pos_BwImage_print(im1);
  74.    pos_BwImage_drawRect(im2, 0, 0, 344, 124);
  75.    status = pos_BwImage_print(im2);
  76.    
  77.    return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement