Ejemplo n.º 1
0
/**
 * Create the BMP object with specified width and height and depth.
 */
bmpfile_t *
bmp_create(uint32_t width, uint32_t height, uint32_t depth)
{
  bmpfile_t *result;
  double bytes_per_pixel;
  uint32_t bytes_per_line;
  uint32_t palette_size;

  if (depth != 1 && depth != 4 && depth != 8 && depth != 16 && depth != 24 &&
      depth != 32)
    return NULL;

  result = malloc(sizeof(bmpfile_t));

  memset(result, 0, sizeof(bmpfile_t));

  result->header.magic[0] = 'B';
  result->header.magic[1] = 'M';

  result->dib.header_sz = 40;
  result->dib.width = width;
  result->dib.height = height;
  result->dib.nplanes = 1;
  result->dib.depth = depth;
  result->dib.hres = DEFAULT_DPI_X;
  result->dib.vres = DEFAULT_DPI_Y;

  if (depth == 16)
    result->dib.compress_type = BI_BITFIELDS;
  else
    result->dib.compress_type = BI_RGB;

  bmp_malloc_pixels(result);
  bmp_malloc_colors(result);

  /* Calculate the field value of header and DIB */
  bytes_per_pixel = (result->dib.depth * 1.0) / 8.0;
  bytes_per_line = (int)ceil(bytes_per_pixel * result->dib.width);
  if (bytes_per_line % 4 != 0)
    bytes_per_line += 4 - bytes_per_line % 4;

  result->dib.bmp_bytesz = bytes_per_line * result->dib.height;

  palette_size = 0;
  if (depth == 1 || depth == 4 || depth == 8)
    palette_size = uint32_pow(2, result->dib.depth) * 4;
  else if (result->dib.depth == 16)
    palette_size = 3 * 4;

  result->header.offset = 14 + result->dib.header_sz + palette_size;
  result->header.filesz = result->header.offset + result->dib.bmp_bytesz;

  return result;
}
Ejemplo n.º 2
0
bmp_structp
bmp_create_bitmap(uint32_t width, uint32_t height, uint16_t depth)
{
	bmp_structp result;
	double bpp;
	uint32_t bpl;
	uint32_t palette_size;

	if (!bmp_validate_bit_depth(depth))
		return NULL;

	result = (bmp_structp)malloc(sizeof(bmp_struct));
	memset(result, 0, sizeof(bmp_struct));

	result->header.magic[0] = 'B';
	result->header.magic[1] = 'M';

	result->dib.header_sz = 40; // ? sizeof(bmp_header_t) ?
	result->dib.width = width;
	result->dib.height = height;
	result->dib.nplanes = 1;
	result->dib.depth = depth;
	result->dib.hres = DEFAULT_DPI_X;
	result->dib.vres = DEFAULT_DPI_Y;

	// COMPRESS TYPE

	bmp_malloc_pixels(result);
	bmp_malloc_colors(result);

	// calculate the number bytes in the bitmap.
	bpp = result->dib.depth / 8.0;
	bpl = (int)ceil(bpp * result->dib.width);
	bpl += bpl % 4; // add padding if necessary.

	result->dib.bmp_bytesz = bpl * result->dib.height;

	// calculate the palette size given specified depth.
	palette_size = 0;
	if (depth <= 8) palette_size = (1 << result->dib.depth) * 4;
	else if (depth == 16) palette_size = 12;

	result->header.offset = 14 + result->dib.header_sz + palette_size;
	result->header.filesz = result->header.offset + result->dib.bmp_bytesz;

	rgb_pixel_t* test = (rgb_pixel_t*)(&result->header + result->header.offset);
	return result;
}
Ejemplo n.º 3
0
bmp_t
bmp_create (uint32_t width, uint32_t height, uint32_t depth)
{
	bmp_t *result = new bmp_t;

	result->magic[0] = 'B';
	result->magic[1] = 'M';

	result->dib.header_size = 40;
	result->dib.width = width;
	result->dib.height = height;
	result->dib.nplanes = 1;
	result->dib.depth = depth;
	result->dib.compress_type = 0;
	result->dib.bmp_byte_size =
	result->dib.hres = default_dpi_x;
	result->dib.vres = default_dpi_y;

	bmp_malloc_pixels(result);
	bmp_malloc_colors(result);

	return result;
}