Back to index

php5  5.3.10
Functions
wbmp.c File Reference
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "wbmp.h"
#include "gd.h"
#include "gdhelpers.h"

Go to the source code of this file.

Functions

int getmbi (int(*getin)(void *in), void *in)
void putmbi (int i, void(*putout)(int c, void *out), void *out)
int skipheader (int(*getin)(void *in), void *in)
Wbmpcreatewbmp (int width, int height, int color)
int readwbmp (int(*getin)(void *in), void *in, Wbmp **return_wbmp)
int writewbmp (Wbmp *wbmp, void(*putout)(int c, void *out), void *out)
void freewbmp (Wbmp *wbmp)
void printwbmp (Wbmp *wbmp)

Function Documentation

Wbmp* createwbmp ( int  width,
int  height,
int  color 
)

Definition at line 111 of file wbmp.c.

{
  int i;

  Wbmp *wbmp;
  if ((wbmp = (Wbmp *) gdMalloc (sizeof (Wbmp))) == NULL)
    return (NULL);

  if (overflow2(sizeof (int), width)) {
    gdFree(wbmp);
    return NULL;
  }
  if (overflow2(sizeof (int) * width, height)) {
    gdFree(wbmp);
    return NULL;
  }

  if ((wbmp->bitmap = (int *) safe_emalloc(sizeof(int), width * height, 0)) == NULL)
    {
      gdFree (wbmp);
      return (NULL);
    }

  wbmp->width = width;
  wbmp->height = height;

  for (i = 0; i < width * height; wbmp->bitmap[i++] = color);

  return (wbmp);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void freewbmp ( Wbmp wbmp)

Definition at line 294 of file wbmp.c.

{
  gdFree (wbmp->bitmap);
  gdFree (wbmp);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int getmbi ( int(*)(void *in)  getin,
void *  in 
)

Definition at line 38 of file wbmp.c.

{
  int i, mbi = 0;

  do
    {
      i = getin (in);
      if (i < 0)
       return (-1);
      mbi = (mbi << 7) | (i & 0x7f);
    }
  while (i & 0x80);

  return (mbi);
}

Here is the caller graph for this function:

void printwbmp ( Wbmp wbmp)

Definition at line 307 of file wbmp.c.

{
  int row, col;
  for (row = 0; row < wbmp->height; row++)
    {
      for (col = 0; col < wbmp->width; col++)
       {
         if (wbmp->bitmap[wbmp->width * row + col] == WBMP_BLACK)
           {
             putchar ('#');
           }
         else
           {
             putchar (' ');
           }
       }
      putchar ('\n');
    }
}
void putmbi ( int  i,
void(*)(int c, void *out putout,
void *  out 
)

Definition at line 64 of file wbmp.c.

{
  int cnt, l, accu;

  /* Get number of septets */
  cnt = 0;
  accu = 0;
  while (accu != i)
    accu += i & 0x7f << 7 * cnt++;

  /* Produce the multibyte output */
  for (l = cnt - 1; l > 0; l--)
    putout (0x80 | (i & 0x7f << 7 * l) >> 7 * l, out);

  putout (i & 0x7f, out);

}

Here is the caller graph for this function:

int readwbmp ( int(*)(void *in)  getin,
void *  in,
Wbmp **  return_wbmp 
)

Definition at line 151 of file wbmp.c.

{
  int row, col, byte, pel, pos;
  Wbmp *wbmp;

  if ((wbmp = (Wbmp *) gdMalloc (sizeof (Wbmp))) == NULL)
    return (-1);

  wbmp->type = getin (in);
  if (wbmp->type != 0)
    {
      gdFree (wbmp);
      return (-1);
    }

  if (skipheader (getin, in))
    return (-1);


  wbmp->width = getmbi (getin, in);
  if (wbmp->width == -1)
    {
      gdFree (wbmp);
      return (-1);
    }

  wbmp->height = getmbi (getin, in);
  if (wbmp->height == -1)
    {
      gdFree (wbmp);
      return (-1);
    }

#ifdef __DEBUG
  printf ("W: %d, H: %d\n", wbmp->width, wbmp->height);
#endif

  if (overflow2(sizeof (int), wbmp->width) ||
    overflow2(sizeof (int) * wbmp->width, wbmp->height))
    {
      gdFree(wbmp);
      return (-1);
    }

  if ((wbmp->bitmap = (int *) safe_emalloc((size_t)wbmp->width * wbmp->height, sizeof(int), 0)) == NULL)
    {
      gdFree (wbmp);
      return (-1);
    }

#ifdef __DEBUG
  printf ("DATA CONSTRUCTED\n");
#endif

  pos = 0;
  for (row = 0; row < wbmp->height; row++)
    {
      for (col = 0; col < wbmp->width;)
       {
         byte = getin (in);

         for (pel = 7; pel >= 0; pel--)
           {
             if (col++ < wbmp->width)
              {
                if (byte & 1 << pel)
                  {
                    wbmp->bitmap[pos] = WBMP_WHITE;
                  }
                else
                  {
                    wbmp->bitmap[pos] = WBMP_BLACK;
                  }
                pos++;
              }
           }
       }
    }

  *return_wbmp = wbmp;

  return (0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int skipheader ( int(*)(void *in)  getin,
void *  in 
)

Definition at line 90 of file wbmp.c.

{
  int i;

  do
    {
      i = getin (in);
      if (i < 0)
       return (-1);
    }
  while (i & 0x80);

  return (0);
}

Here is the caller graph for this function:

int writewbmp ( Wbmp wbmp,
void(*)(int c, void *out putout,
void *  out 
)

Definition at line 248 of file wbmp.c.

{
  int row, col;
  int bitpos, octet;

  /* Generate the header */
  putout (0, out);          /* WBMP Type 0: B/W, Uncompressed bitmap */
  putout (0, out);          /* FixHeaderField */



  /* Size of the image */
  putmbi (wbmp->width, putout, out);      /* width */
  putmbi (wbmp->height, putout, out);     /* height */


  /* Image data */
  for (row = 0; row < wbmp->height; row++)
    {
      bitpos = 8;
      octet = 0;
      for (col = 0; col < wbmp->width; col++)
       {
         octet |= ((wbmp->bitmap[row * wbmp->width + col] == 1) ? WBMP_WHITE : WBMP_BLACK) << --bitpos;
         if (bitpos == 0)
           {
             bitpos = 8;
             putout (octet, out);
             octet = 0;
           }
       }
      if (bitpos != 8)
       putout (octet, out);

    }
  return (0);

}

Here is the call graph for this function:

Here is the caller graph for this function: