Example #1
0
int vglSave4dTiff(char* filename, VglImage* image, int lStart, int lEnd)
{
  vglCheckContext(image, VGL_RAM_CONTEXT);
  if ( (image->nChannels != 1) && (image->nChannels != 3) )
  {
    fprintf(stderr, "%s: %s: Error: image has %d channels but only 1 or 3 channels supported. Use vglImage4to3Channels function before saving\n", __FILE__, __FUNCTION__, image->nChannels);
    return 1;
  }
  char* temp_filename = (char*)malloc(strlen(filename)+256);
  int c = 0;
  for(int i = lStart; i <= lEnd; i++)
  {
    VglImage* temp_image = vglCreate3dImage(cvSize(image->getWidth(), image->getHeight()), image->depth, image->nChannels, image->getLength());
    temp_image->ndarray = (char*)malloc(temp_image->getTotalSizeInBytes());
    memcpy((char*)temp_image->ndarray,((char*)image->ndarray)+c,temp_image->getTotalSizeInBytes());
    sprintf(temp_filename, filename, i);
    vglSaveTiff(temp_filename, temp_image);
    c += temp_image->getTotalSizeInBytes();
    vglReleaseImage(&temp_image);
  }

  return 0;
}      
Example #2
0
/** Function for saving 2D and 3D TIFF images
  */
int vglSaveTiff(char* outFilename, VglImage* image)
{
  vglCheckContext(image, VGL_RAM_CONTEXT);

  TIFF *out = TIFFOpen(outFilename, "w");
  char* buff = image->getImageData();

  //int b = image->getBitsPerSample();
  int c = image->getNChannels();
  int widthStep = image->getWidthStep();

  for(int z = 0; z < image->getLength(); z++)
  {
    TIFFSetField(out, TIFFTAG_IMAGEWIDTH, image->getWidth());
    TIFFSetField(out, TIFFTAG_IMAGELENGTH, image->getHeight());
    TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, image->getBitsPerSample());
    if (image->nChannels == 1)
      TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    else
      TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
    TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, image->nChannels);
    TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
    TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
   
    TIFFSetField(out, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
    TIFFSetField(out, TIFFTAG_PAGENUMBER, z, image->getLength());

    for(int y = 0; y < image->getHeight(); y++)
    {
      TIFFWriteScanline(out, &buff[ (  (z*widthStep*image->getHeight()) + (y*widthStep)  )], y, 0);
    }
    
    TIFFWriteDirectory(out);
  }
 
  TIFFClose(out);
}
void vglCl3dMergeZByMean(VglImage* img_input, VglImage* img_output, int number_of_merges){

  vglCheckContext(img_input, VGL_CL_CONTEXT);
  vglCheckContext(img_output, VGL_CL_CONTEXT);

  cl_int err;

  static cl_program program = NULL;
  if (program == NULL)
  {
    char* file_path = (char*) "CL/vglCl3dMergeZByMean.cl";
    printf("Compiling %s\n", file_path);
    std::ifstream file(file_path);
    if(file.fail())
    {
      std::string str("File not found: ");
      str.append(file_path);
      vglClCheckError(-1, (char*)str.c_str());
    }
    std::string prog( std::istreambuf_iterator<char>( file ), ( std::istreambuf_iterator<char>() ) );
    const char *source_str = prog.c_str();
#ifdef __DEBUG__
    printf("Kernel to be compiled:\n%s\n", source_str);
#endif
    program = clCreateProgramWithSource(cl.context, 1, (const char **) &source_str, 0, &err );
    vglClCheckError(err, (char*) "clCreateProgramWithSource" );
    err = clBuildProgram(program, 1, cl.deviceId, NULL, NULL, NULL );
    vglClBuildDebug(err, program);
  }

  static cl_kernel kernel = NULL;
  if (kernel == NULL)
  {
    kernel = clCreateKernel( program, "vglCl3dMergeZByMean", &err ); 
    vglClCheckError(err, (char*) "clCreateKernel" );
  }


  err = clSetKernelArg( kernel, 0, sizeof( cl_mem ), (void*) &img_input->oclPtr );
  vglClCheckError( err, (char*) "clSetKernelArg 0" );

  err = clSetKernelArg( kernel, 1, sizeof( cl_mem ), (void*) &img_output->oclPtr );
  vglClCheckError( err, (char*) "clSetKernelArg 1" );

  err = clSetKernelArg( kernel, 2, sizeof( int ), (void*) &number_of_merges );
  vglClCheckError( err, (char*) "clSetKernelArg 1" );

  if (img_input->ndim <= 2){
    printf("2D images not supported by this operation\n");
  }
  else if (img_input->ndim == 3){
    size_t worksize[] = { img_input->getWidth(), img_input->getHeight(), img_input->getLength() };
    clEnqueueNDRangeKernel( cl.commandQueue, kernel, 3, NULL, worksize, 0, 0, 0, 0 );
  }
  else{
    printf("More than 3 dimensions not yet supported\n");
  }

  vglClCheckError( err, (char*) "clEnqueueNDRangeKernel" );

  vglSetContext(img_output, VGL_CL_CONTEXT);
}
Example #4
0
/** Calculate sum a = a + b and save carry

  */
int vglClMpIsZero(VglImage* num_a){

  int isZero = 200;
  cl_mem isZero_oclPtr = NULL;

  vglCheckContext(num_a, VGL_CL_CONTEXT);

  cl_int err;

  isZero_oclPtr = clCreateBuffer(cl.context, CL_MEM_READ_WRITE, sizeof(isZero), NULL, &err);
  vglClCheckError( err, (char*) "clCreateNDImage" );

  err = clEnqueueWriteBuffer(cl.commandQueue, isZero_oclPtr, CL_TRUE, 0, sizeof(isZero), &isZero, 0, NULL, NULL);
  vglClCheckError( err, (char*) "clEnqueueWriteBuffer" );
  clFinish(cl.commandQueue);


  static cl_program program = NULL;
  if (program == NULL)
  {
    char* file_path = (char*) "CL_MP/vglClMpIsZero.cl";
    printf("Compiling %s\n", file_path);
    std::ifstream file(file_path);
    if(file.fail())
    {
      fprintf(stderr, "%s:%s: Error: File %s not found.\n", __FILE__, __FUNCTION__, file_path);
      exit(1);
    }
    std::string prog( std::istreambuf_iterator<char>( file ), ( std::istreambuf_iterator<char>() ) );
    const char *source_str = prog.c_str();
#ifdef __DEBUG__
    printf("Kernel to be compiled:\n%s\n", source_str);
#endif
    program = clCreateProgramWithSource(cl.context, 1, (const char **) &source_str, 0, &err );
    vglClCheckError(err, (char*) "clCreateProgramWithSource" );
    err = clBuildProgram(program, 1, cl.deviceId, NULL, NULL, NULL );
    vglClBuildDebug(err, program);
  }

  static cl_kernel kernel = NULL;
  if (kernel == NULL)
  {
    kernel = clCreateKernel( program, "vglClMpIsZero", &err ); 
    vglClCheckError(err, (char*) "clCreateKernel" );
  }


  err = clSetKernelArg( kernel, 0, sizeof( cl_mem ), (void*) &num_a->oclPtr );
  vglClCheckError( err, (char*) "clSetKernelArg 0" );

  err = clSetKernelArg( kernel, 1, sizeof( cl_mem ), (void*) &isZero_oclPtr );
  vglClCheckError( err, (char*) "clSetKernelArg 1" );

  if (num_a->ndim <= 2){
    size_t worksize[] = { num_a->shape[VGL_WIDTH], num_a->shape[VGL_HEIGHT], 1 };
    clEnqueueNDRangeKernel( cl.commandQueue, kernel, 2, NULL, worksize, 0, 0, 0, 0 );
  }
  else if (num_a->ndim == 3){
    size_t worksize[] = { num_a->shape[VGL_WIDTH], num_a->shape[VGL_HEIGHT], num_a->shape[VGL_LENGTH] };
    clEnqueueNDRangeKernel( cl.commandQueue, kernel, 3, NULL, worksize, 0, 0, 0, 0 );
  }
  else{
    printf("More than 3 dimensions not yet supported\n");
  }

  vglClCheckError( err, (char*) "clEnqueueNDRangeKernel" );

  err = clEnqueueReadBuffer(cl.commandQueue, isZero_oclPtr, CL_TRUE, 0, sizeof(isZero), &isZero, 0, NULL, NULL);
  vglClCheckError( err, (char*) "clEnqueueReadNDImage" );

  vglSetContext(num_a, VGL_CL_CONTEXT);

  return isZero;
}
Example #5
0
bool vglClEqual(VglImage* input1, VglImage* input2)
{
  if(input1->ndim > 2)
  {
    fprintf(stderr, "%s: %s: Error: image with more then 2 dimensions not supported. Use vglCl3dEqual instead.\n", __FILE__, __FUNCTION__);
    return false;
  }

  vglCheckContext(input1,VGL_CL_CONTEXT);
  vglCheckContext(input2,VGL_CL_CONTEXT);

  cl_int err;

  cl_mem mobj_equal = NULL;
  mobj_equal = clCreateBuffer(cl.context, CL_MEM_READ_WRITE, sizeof(char), NULL, &err);
  vglClCheckError( err, (char*) "clCreateBuffer histogram" );
  char e = 200;
  err = clEnqueueWriteBuffer(cl.commandQueue,mobj_equal,CL_TRUE,0,sizeof(char),&e,0,NULL,NULL);
  vglClCheckError(err, (char*) "clEnqueueWriteBuffer mobj_arr");

  static cl_program program = NULL;
  if (program == NULL)
  {
    char* file_path = (char*) "CL_UTIL/vglClEqual.cl";
    printf("Compiling %s\n", file_path);
    std::ifstream file(file_path);
    if(file.fail())
    {
      fprintf(stderr, "%s:%s: Error: File %s not found.\n", __FILE__, __FUNCTION__, file_path);
      exit(1);
    }
    std::string prog( std::istreambuf_iterator<char>( file ), ( std::istreambuf_iterator<char>() ) );
    const char *source_str = prog.c_str();
#ifdef __DEBUG__
    printf("Kernel to be compiled:\n%s\n", source_str);
#endif
    program = clCreateProgramWithSource(cl.context, 1, (const char **) &source_str, 0, &err );
    vglClCheckError(err, (char*) "clCreateProgramWithSource" );
    err = clBuildProgram(program, 1, cl.deviceId, NULL, NULL, NULL );
    vglClBuildDebug(err, program);
  }

  static cl_kernel kernel = NULL;
  if (kernel == NULL)
  {
    kernel = clCreateKernel( program, "vglCl3dEqual", &err ); 
    vglClCheckError(err, (char*) "clCreateKernel" );
  }

  err = clSetKernelArg( kernel, 0, sizeof( cl_mem ), (void*) &input1->oclPtr );
  vglClCheckError( err, (char*) "clSetKernelArg 0" );

  err = clSetKernelArg( kernel, 1, sizeof( cl_mem ), (void*) &input2->oclPtr );
  vglClCheckError( err, (char*) "clSetKernelArg 1" );

  err = clSetKernelArg( kernel, 2, sizeof( cl_mem ), (void*) &mobj_equal );
  vglClCheckError( err, (char*) "clSetKernelArg 2" );

  size_t worksize[] = { input1->shape[VGL_WIDTH], input1->shape[VGL_HEIGHT], 0 };
  clEnqueueNDRangeKernel( cl.commandQueue, kernel, 2, NULL, worksize, 0, 0, 0, 0 );
  vglClCheckError( err, (char*) "clEnqueueNDRangeKernel" );
  
  err = clEnqueueReadBuffer(cl.commandQueue,mobj_equal,CL_TRUE,0,sizeof(char),&e,0,NULL,NULL);
  vglClCheckError( err, (char*) "clEnqueueReadBuffer" );

  return e != 1;
}
Example #6
0
void vglCl3dGrayLevelTransform(VglImage* input, VglImage* output, int* transformation)
{
  
  if (input->nChannels > 1 || output->nChannels > 1)
  {
     fprintf(stderr, "%s: %s: Error: image with more then 1 channel not supported. Please convert to 1 channel.\n", __FILE__, __FUNCTION__);
     return;
  }

  vglCheckContext(input,VGL_CL_CONTEXT);
  vglCheckContext(output,VGL_CL_CONTEXT);

  cl_int err;

  cl_mem mobj_arr = NULL;
  mobj_arr = clCreateBuffer(cl.context, CL_MEM_READ_WRITE, 256*sizeof(int), NULL, &err);
  vglClCheckError(err, (char*) "clCreateBuffer mobj_arr" );
  err = clEnqueueWriteBuffer(cl.commandQueue,mobj_arr,CL_TRUE, 0, 256*sizeof(int), transformation, 0, NULL, NULL);
  vglClCheckError(err, (char*) "clEnqueueWriteBuffer mobj_arr");

  static cl_program program = NULL;
  if (program == NULL)
  {
    char* file_path = (char*) "CL_UTIL/vglClHistogramEq.cl";
    printf("Compiling %s\n", file_path);
    std::ifstream file(file_path);
    if(file.fail())
    {
      fprintf(stderr, "%s:%s: Error: File %s not found.\n", __FILE__, __FUNCTION__, file_path);
      exit(1);
    }
    std::string prog( std::istreambuf_iterator<char>( file ), ( std::istreambuf_iterator<char>() ) );
    const char *source_str = prog.c_str();
#ifdef __DEBUG__
    printf("Kernel to be compiled:\n%s\n", source_str);
#endif
    program = clCreateProgramWithSource(cl.context, 1, (const char **) &source_str, 0, &err );
    vglClCheckError(err, (char*) "clCreateProgramWithSource" );
    err = clBuildProgram(program, 1, cl.deviceId, NULL, NULL, NULL );
    vglClBuildDebug(err, program);
  }

  static cl_kernel kernel = NULL;
  if (kernel == NULL)
  {
    kernel = clCreateKernel( program, "vglCl3dGrayLevelTransform", &err ); 
    vglClCheckError(err, (char*) "clCreateKernel" );
  }

  err = clSetKernelArg( kernel, 0, sizeof( cl_mem ), (void*) &input->oclPtr);
  vglClCheckError( err, (char*) "clSetKernelArg 0" );

  err = clSetKernelArg( kernel, 1, sizeof( cl_mem ), (void*) &output->oclPtr);
  vglClCheckError( err, (char*) "clSetKernelArg 1" );

  err = clSetKernelArg( kernel, 2, sizeof( cl_mem ), (void*) &mobj_arr);
  vglClCheckError( err, (char*) "clSetKernelArg 2" );

  size_t worksize[] = { input->shape[VGL_WIDTH], input->shape[VGL_HEIGHT], input->shape[VGL_LENGTH] };

  err = clEnqueueNDRangeKernel( cl.commandQueue, kernel, 3, NULL, worksize, 0, 0, 0, 0 );
  vglClCheckError( err, (char*) "clEnqueueNDRangeKernel" );

  err = clReleaseMemObject( mobj_arr );
  vglClCheckError(err, (char*) "clReleaseMemObject mobj_arr");

  
}
Example #7
0
cl_mem vglCl3dPartialHistogram(VglImage* img_input)
{

  vglCheckContext(img_input, VGL_CL_CONTEXT);

  cl_int err;

  cl_mem mobj_histogram = NULL;
  mobj_histogram = clCreateBuffer(cl.context, CL_MEM_READ_WRITE, img_input->nChannels*256*img_input->shape[VGL_WIDTH]*sizeof(unsigned int), NULL, &err);
  vglClCheckError( err, (char*) "clCreateBuffer histogram" );

  static cl_program program = NULL;
  if (program == NULL)
  {
    char* file_path = (char*) "CL_UTIL/vglClHistogram.cl";
    printf("Compiling %s\n", file_path);
    std::ifstream file(file_path);
    if(file.fail())
    {
      fprintf(stderr, "%s:%s: Error: File %s not found.\n", __FILE__, __FUNCTION__, file_path);
      exit(1);
    }
    std::string prog( std::istreambuf_iterator<char>( file ), ( std::istreambuf_iterator<char>() ) );
    const char *source_str = prog.c_str();
#ifdef __DEBUG__
    printf("Kernel to be compiled:\n%s\n", source_str);
#endif
    program = clCreateProgramWithSource(cl.context, 1, (const char **) &source_str, 0, &err );
    vglClCheckError(err, (char*) "clCreateProgramWithSource" );
    err = clBuildProgram(program, 1, cl.deviceId, NULL, NULL, NULL );
    vglClBuildDebug(err, program);
  }

  static cl_kernel kernel = NULL;
  if (kernel == NULL)
  {
    kernel = clCreateKernel( program, "vglCl3dPartialHistogram", &err ); 
    vglClCheckError(err, (char*) "clCreateKernel" );
  }


  err = clSetKernelArg( kernel, 0, sizeof( cl_mem ), (void*) &img_input->oclPtr );
  vglClCheckError( err, (char*) "clSetKernelArg 0" );

  err = clSetKernelArg( kernel, 1, sizeof( cl_mem ), (void*) &mobj_histogram );
  vglClCheckError( err, (char*) "clSetKernelArg 1" );

  err = clSetKernelArg( kernel, 2, sizeof( int ), &img_input->nChannels );
  vglClCheckError( err, (char*) "clSetKernelArg 2" );

  if (img_input->ndim == 3){
    size_t worksize[] = { img_input->shape[VGL_WIDTH], 1, 1 };
    clEnqueueNDRangeKernel( cl.commandQueue, kernel, 1, NULL, worksize, 0, 0, 0, 0 );
    vglClCheckError( err, (char*) "clEnqueueNDRangeKernel" );
  }
  else{
    printf("dimension different of 3 not supported by this function, try to use another version of the function\n");
  }

  return mobj_histogram;
}