Example #1
0
size_t ppm_to_patch(void **lumpdata, const char *filename,
                    int insert_x, int insert_y)
{
  void *data;
  size_t size = read_or_die(&data, filename);

  int i, width, height, *column_colours;
  unsigned char *pixels, **columns, *patch;
  size_t *columnsizes, totalcolumnsize, offset;

  pixels = parseppm(data, size, filename, &width, &height);
  columns = xmalloc(width * sizeof(*columns));
  columnsizes = xmalloc(width * sizeof(*columnsizes));
  column_colours = xmalloc(height * sizeof(*column_colours));

  for (totalcolumnsize = i = 0; i < width; i++)
  {
    pixels_to_colours(column_colours, pixels, width, height, i);
    columnsizes[i] = createcolumn(&columns[i], column_colours, height);
    totalcolumnsize += columnsizes[i];
  }

  patch = xmalloc(8+4*width+totalcolumnsize);

  ((short *)patch)[0] = SHORT(width);
  ((short *)patch)[1] = SHORT(height);
  ((short *)patch)[2] = SHORT(insert_x);
  ((short *)patch)[3] = SHORT(insert_y);

  for (offset = 8+4*width, i = 0; i < width; i++)
  {
    ((int *)(patch+8))[i] = LONG(offset);
    offset += columnsizes[i];
  }

  for (offset = 8+4*width, i = 0; i < width; i++)
  {
    memmove(patch + offset, columns[i], columnsizes[i]);
    offset += columnsizes[i];
    free(columns[i]);
  }

  free(column_colours);
  free(columnsizes);
  free(columns);
  free(data);

  *lumpdata = patch;
  return 8+4*width+totalcolumnsize;
}
Example #2
0
size_t ppm_to_bitmap(void **lumpdata, const char *filename)
{
  void *data;
  size_t size = read_or_die(&data, filename);

  int i, j, width, height;
  unsigned char *pixels, *bitmap;

  pixels = parseppm(data, size, filename, &width, &height);
  bitmap = xmalloc(width * height);

  for (j = 0; j < height; j++)
    for (i = 0; i < width; i++)
      bitmap[width*j+i] = palette_getindex(&pixels[3*(width*j+i)]);

  free(data);

  *lumpdata = bitmap;
  return width * height;
}