Пример #1
0
   void load_pbm(const char* name, uchar* &im, int &height, int &width)
   {
      char buf[PNM_BUFFER_SIZE];

      /* read header */
      std::ifstream file(name, std::ios::in | std::ios::binary);
      pnm_read(file, buf);
      if (strncmp(buf, "P4", 2))
      {
         printf("type mismatch\n");
         exit(1);
      }

      pnm_read(file, buf);
      width = atoi(buf);

      pnm_read(file, buf);
      height = atoi(buf);

      /* read data */
      if( im != NULL) delete[]im;
      im = new uchar[width*height];
      for (int i = 0; i < height; i++)
         read_packed(im+(width*i), width, file);
   }
Пример #2
0
ico_image_t *
ico_image_read(ico_reader_t *file, int index, int *error) {
  io_glue *ig = file->ig;
  ico_reader_image_entry *im;
  long bi_size, width, height, planes, bit_count;
  ico_image_t *result;

  if (index < 0 || index >= file->count) {
    *error = ICOERR_Bad_Image_Index;
    return NULL;
  }

  im = file->images + index;
  if (i_io_seek(ig, im->offset, SEEK_SET) != im->offset) {
    *error = ICOERR_File_Error;
    return NULL;
  }

  if (!read_packed(ig, "dddww xxxx xxxx xxxx xxxx xxxx xxxx", &bi_size, 
		   &width, &height, &planes, &bit_count)) {
    *error = ICOERR_Short_File;
    return NULL;
  }

  /* the bitmapinfoheader height includes the height of 
     the and and xor masks */
  if (bi_size != 40 || width != im->width || height != im->height * 2
      || planes != 1) { /* don't know how to handle planes != 1 */
    *error = ICOERR_Invalid_File;
    return NULL;
  }

  if (bit_count != 1 && bit_count != 4 && bit_count != 8 && bit_count != 32) {
    *error = ICOERR_Unknown_Bits;
    return 0;
  }

  result = malloc(sizeof(ico_image_t));
  if (!result) {
    *error = ICOERR_Out_Of_Memory;
    return NULL;
  }
  result->width = width;
  result->height = im->height;
  result->direct = bit_count > 8;
  result->bit_count = bit_count;
  result->palette = NULL;
  result->image_data = NULL;
  result->mask_data = NULL;
  result->hotspot_x = im->hotspot_x;
  result->hotspot_y = im->hotspot_y;
    
  if (bit_count == 32) {
    result->palette_size = 0;

    result->image_data = malloc(result->width * result->height * sizeof(ico_color_t));
    if (!result->image_data) {
      free(result);
      *error = ICOERR_Out_Of_Memory;
      return NULL;
    }
    if (!read_32bit_data(file, result, error)) {
      free(result->image_data);
      free(result);
      return NULL;
    }
  }
  else {
    int read_result;

    result->palette_size = 1 << bit_count;
    result->palette = malloc(sizeof(ico_color_t) * result->palette_size);
    if (!result->palette) {
      free(result);
      *error = ICOERR_Out_Of_Memory;
      return NULL;
    }

    result->image_data = malloc(result->width * result->height);
    if (!result->image_data) {
      *error = ICOERR_Out_Of_Memory;
      free(result->palette);
      free(result);
      return 0;
    }      
    
    if (!read_palette(file, result, error)) {
      free(result->palette);
      free(result->image_data);
      free(result);
      return 0;
    }

    switch (bit_count) {
    case 1:
      read_result = read_1bit_data(file, result, error);
      break;

    case 4:
      read_result = read_4bit_data(file, result, error);
      break;
      
    case 8:
      read_result = read_8bit_data(file, result, error);
      break;

    default:
      assert(0); /* this can't happen in theory */
      read_result = 0;
      break;
    }

    if (!read_result) {
      free(result->palette);
      free(result->image_data);
      free(result);
      return 0;
    }
  }

  result->mask_data = malloc(result->width * result->height);
  if (!result->mask_data) {
    *error = ICOERR_Out_Of_Memory;
    free(result->palette);
    free(result->image_data);
    free(result);
    return 0;
  }

  if (!read_mask(file, result, error)) {
    free(result->mask_data);
    free(result->palette);
    free(result->image_data);
    free(result);
    return 0;
  }

  return result;
}
Пример #3
0
ico_reader_t *
ico_reader_open(i_io_glue_t *ig, int *error) {
  long res1, type, count;
  ico_reader_t *file = NULL;
  int i;

  if (!read_packed(ig, "www", &res1, &type, &count)) {
    *error = ICOERR_Short_File;
    return NULL;
  }
  if (res1 != 0 || (type != 1 && type != 2) || count == 0) {
    *error = ICOERR_Invalid_File;
    return NULL;
  }

  file = malloc(sizeof(ico_reader_t));
  if (!file) {
    *error = ICOERR_Out_Of_Memory;
    return NULL;
  }
  file->count = count;
  file->type = type;
  file->ig = ig;
  file->images = malloc(sizeof(ico_reader_image_entry) * count);
  if (file->images == NULL) {
    *error = ICOERR_Out_Of_Memory;
    free(file);
    return NULL;
  }

  for (i = 0; i < count; ++i) {
    long width, height, bytes_in_res, image_offset;

    ico_reader_image_entry *image = file->images + i;
    if (type == ICON_ICON) {
      if (!read_packed(ig, "bb xxxxxx dd", &width, &height, &bytes_in_res, 
		       &image_offset)) {
	free(file->images);
	free(file);
	*error = ICOERR_Short_File;
	return NULL;
      }
      image->hotspot_x = image->hotspot_y = 0;
    }
    else {
      long hotspot_x, hotspot_y;

      if (!read_packed(ig, "bb xx ww dd", &width, &height, 
		       &hotspot_x, &hotspot_y, &bytes_in_res, 
		       &image_offset)) {
	free(file->images);
	free(file);
	*error = ICOERR_Short_File;
	return NULL;
      }
      image->hotspot_x = hotspot_x;
      image->hotspot_y = hotspot_y;
    }

    image->width = width;
    image->height = height;
    image->offset = image_offset;
    image->size = bytes_in_res;
  }

  return file;
}