BubIOResult bub_read_from_partition(BubOps* ops,
                                    const char* partition_name,
                                    void* buf,
                                    int64_t offset_from_partition,
                                    size_t num_bytes,
                                    size_t* out_num_read) {
  bub_assert(partition_name != NULL);
  bub_assert(buf != NULL);
  bub_assert(out_num_read != NULL);

  EFI_STATUS err;
  GPTEntry *partition_entry;
  UINT64 partition_size;
  MyBubOps* bub = (MyBubOps*)ops;

  err = bub_partition_entry_by_name(bub->block_io,
                                    partition_name,
                                    &partition_entry);
  if (EFI_ERROR(err))
    return BUB_IO_RESULT_ERROR_NO_SUCH_PARTITION;

  partition_size = IMG_SIZE(partition_entry, bub->block_io);

  if (offset_from_partition < 0) {
    if ((-offset_from_partition) > partition_size) {
      bub_warning("Offset outside range.\n");
      bub_free(partition_entry);
      return BUB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION;
    }
    offset_from_partition = partition_size - (-offset_from_partition);
  }

  // Check if num_bytes goes beyond partition end. If so, don't read beyond
  // this boundary -- do a partial I/O instead.
  if (num_bytes > partition_size - offset_from_partition)
    *out_num_read = partition_size - offset_from_partition;
  else
    *out_num_read = num_bytes;

  err = uefi_call_wrapper(bub->disk_io->ReadDisk, 5,
                          bub->disk_io,
                          bub->block_io->Media->MediaId,
                          (partition_entry->first_lba *
                            bub->block_io->Media->BlockSize) +
                            offset_from_partition,
                          *out_num_read,
                          buf);
  if (EFI_ERROR(err)) {
    bub_warning("Could not read from Disk.\n");
    *out_num_read = 0;
    bub_free(partition_entry);
    return BUB_IO_RESULT_ERROR_IO;
  }

  bub_free(partition_entry);
  return BUB_IO_RESULT_OK;
}
BubIOResult bub_write_to_partition(BubOps* ops,
                                   const char* partition_name,
                                   const void* buf,
                                   int64_t offset_from_partition,
                                   size_t num_bytes) {
  bub_assert(partition_name != NULL);
  bub_assert(buf != NULL);

  EFI_STATUS err;
  GPTEntry *partition_entry;
  UINT64 partition_size;
  MyBubOps* bub = (MyBubOps*)ops;

  err = bub_partition_entry_by_name(bub->block_io,
                                    partition_name,
                                    &partition_entry);
  if (EFI_ERROR(err))
    return BUB_IO_RESULT_ERROR_NO_SUCH_PARTITION;

  partition_size = IMG_SIZE(partition_entry, bub->block_io);

  if (offset_from_partition < 0) {
    if ((-offset_from_partition) > partition_size) {
      bub_warning("Offset outside range.\n");
      bub_free(partition_entry);
      return BUB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION;
    }
    offset_from_partition = partition_size - (-offset_from_partition);
  }

  // Check if num_bytes goes beyond partition end. If so, error out -- no
  // partial I/O.
  if (num_bytes > partition_size - offset_from_partition) {
    bub_warning("Cannot write beyond partition boundary.\n");
    bub_free(partition_entry);
    return BUB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION;
  }

  err = uefi_call_wrapper(bub->disk_io->WriteDisk, 5,
                          bub->disk_io,
                          bub->block_io->Media->MediaId,
                          (partition_entry->first_lba *
                            bub->block_io->Media->BlockSize) +
                            offset_from_partition,
                          num_bytes,
                          buf);

  if (EFI_ERROR(err)) {
    bub_warning("Could not write to Disk.\n");
    bub_free(partition_entry);
    return BUB_IO_RESULT_ERROR_IO;
  }

  bub_free(partition_entry);
  return BUB_IO_RESULT_OK;
}
示例#3
0
文件: nv_vlad.hpp 项目: kyama/otama
	void
	extract_dense(nv_matrix_t *vlad, int j,
				  const nv_matrix_t *image,
				  nv_keypoint_dense_t *dense,
				  int ndense
		)
	{
		NV_ASSERT(vlad->n == DIM);
		int desc_m;
		nv_matrix_t *key_vec;
		nv_matrix_t *desc_vec;
		nv_matrix_t *resize, *gray, *smooth;
		
		int i;
		int km = 0;

		if (m_fit_area == 0) {
			float scale = IMG_SIZE() / (float)NV_MAX(image->rows, image->cols);	
			resize = nv_matrix3d_alloc(3, (int)(image->rows * scale),
									   (int)(image->cols * scale));
		} else {
			float axis_ratio = (float)image->rows / image->cols;
			int new_cols = (int)sqrtf(m_fit_area / axis_ratio);
			int new_rows = (int)((float)m_fit_area / new_cols);
			resize = nv_matrix3d_alloc(3, new_rows, new_cols);
		}
		gray = nv_matrix3d_alloc(1, resize->rows, resize->cols);
		smooth = nv_matrix3d_alloc(1, resize->rows, resize->cols);
		
		for (i = 0; i < ndense; ++i) {
			km += dense[i].rows * dense[i].cols;
		}
		km *= 2;
		key_vec = nv_matrix_alloc(NV_KEYPOINT_KEYPOINT_N, km);
		desc_vec = nv_matrix_alloc(NV_KEYPOINT_DESC_N, km);
		nv_resize(resize, image);
		nv_gray(gray, resize);
		nv_gaussian5x5(smooth, 0, gray, 0);
		
		nv_matrix_zero(desc_vec);
		nv_matrix_zero(key_vec);
		
		desc_m = nv_keypoint_dense_ex(m_ctx, key_vec, desc_vec, smooth, 0,
									  dense, ndense);
		feature_vector(vlad, j, key_vec, desc_vec, desc_m);
		
		nv_matrix_free(&gray);
		nv_matrix_free(&resize);
		nv_matrix_free(&smooth);
		nv_matrix_free(&key_vec);
		nv_matrix_free(&desc_vec);
	}
示例#4
0
void convolve_atomic_patches( float_layers* source, float_layers* target, 
                              const dm_params_t* params, res_scale* first_level ) 
{
    const int extend = 1; // slightly spatially extend response maps
    const float norm = 1; // renorm patches
    
    const int f = first_level->f; // scale factor w.r.t. original image
    const int psize = first_level->patch_size; // current patch size
    
    // first, sample patches
    float_image patches = {0};
    assert(!first_level->norms.pixels);
    first_level->norms = image_like(float, &first_level->grid);
    float_array norms_arr = {first_level->norms.pixels, (int)IMG_SIZE(&first_level->norms)};
    sample_patches( source, &first_level->grid, psize, f, norm, params->n_thread, &patches, &norms_arr );
    //hash_image(&patches)
    
    // rectify the norm to a boolean (0 or 1) (useless ?)
    first_level->assign = empty_array(int,norms_arr.tx);
    int n=0, tx = patches.tx;
    for(int i=0; i<norms_arr.tx; i++) {
      norms_arr.pixels[i] = norms_arr.pixels[i]>0;
      
      // eliminate zero-norm patches
      if( norms_arr.pixels[i] ) {
        if( n < i ) // copy
          memcpy( patches.pixels + n*tx, patches.pixels + i*tx, tx*sizeof(float));
        first_level->assign.pixels[i] = n++;
      } else 
        first_level->assign.pixels[i] = -1;
      
      // convolution is not fully invariant to the image border: 
      // blank cells outside the image are a bit disadvantageous
      if( norms_arr.pixels[i] == 0 )
        norms_arr.pixels[i] = 1-trans_inv;
    }
    patches.ty = n; // update new number of valid patches
    
    //hash_image(&first_level->norms)
    //hash_image(&patches)
    
    // compute the first level convolutions
    fastconv( &patches, target, psize/f, params->ngh_rad/f, extend, norm, params->n_thread, first_level );
    
    free(patches.pixels);
}