S
Saumya Garg
I am trying to write a Module written in C for ruby to help me do some
image processing.
I get the following error when I try to run my code.
processImg.rb:7:in `OpenFile': cannot convert Module into String (TypeError)
from /home/usr/johnDoe/processImg.rb:7
Below is a simplified example of my code that generates the same error.
C code:
#include <stdio.h>
#include <stdlib.h>
#include "ruby.h"
static VALUE getFile(VALUE nameFile){
char *fileName = STR2CSTR(nameFile);
FILE *pFile;
long lSize;
float *buffer;
pFile = fopen (fileName, "rb" );
// obtain file size.
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
buffer = (float*) malloc (lSize);
// copy the file into the buffer.
fread (buffer,1,lSize,pFile);
fclose (pFile);
VALUE data = Data_Make_Struct(data,float,0,free, buffer);
return data;
}
static VALUE writeFile(VALUE data, VALUE nameFile){
float *img;
Data_Get_Struct(data, float, img);
char *fileName = STR2CSTR(nameFile);
FILE * pFile;
int sizeFloat = sizeof(float);
int numberFloats = sizeof(*img) / sizeFloat;
pFile = fopen (fileName , "w");
fwrite (img, sizeFloat,numberFloats, pFile);
fclose (pFile);
free (img);
return Qnil;
}
static VALUE cImageFile;
void Init_Image_File(){
cImageFile = rb_define_module("Image_File");
rb_define_module_function(cImageFile,"OpenFile",getFile,1);
rb_define_module_function(cImageFile,"WriteFile",writeFile,2);
rb_define_module_function(cImageFile,"FreeMem",freeMem, 1);
}
Ruby Code:
require 'Image_File'
imageName = ARGV[0]
newImageName = ARGV[1]
puts (imageName)
data = Image_File.OpenFile(imageName)
Image_File.WriteFile(data, newImageName)
Thanks in advance for your help.
image processing.
I get the following error when I try to run my code.
processImg.rb:7:in `OpenFile': cannot convert Module into String (TypeError)
from /home/usr/johnDoe/processImg.rb:7
Below is a simplified example of my code that generates the same error.
C code:
#include <stdio.h>
#include <stdlib.h>
#include "ruby.h"
static VALUE getFile(VALUE nameFile){
char *fileName = STR2CSTR(nameFile);
FILE *pFile;
long lSize;
float *buffer;
pFile = fopen (fileName, "rb" );
// obtain file size.
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
buffer = (float*) malloc (lSize);
// copy the file into the buffer.
fread (buffer,1,lSize,pFile);
fclose (pFile);
VALUE data = Data_Make_Struct(data,float,0,free, buffer);
return data;
}
static VALUE writeFile(VALUE data, VALUE nameFile){
float *img;
Data_Get_Struct(data, float, img);
char *fileName = STR2CSTR(nameFile);
FILE * pFile;
int sizeFloat = sizeof(float);
int numberFloats = sizeof(*img) / sizeFloat;
pFile = fopen (fileName , "w");
fwrite (img, sizeFloat,numberFloats, pFile);
fclose (pFile);
free (img);
return Qnil;
}
static VALUE cImageFile;
void Init_Image_File(){
cImageFile = rb_define_module("Image_File");
rb_define_module_function(cImageFile,"OpenFile",getFile,1);
rb_define_module_function(cImageFile,"WriteFile",writeFile,2);
rb_define_module_function(cImageFile,"FreeMem",freeMem, 1);
}
Ruby Code:
require 'Image_File'
imageName = ARGV[0]
newImageName = ARGV[1]
puts (imageName)
data = Image_File.OpenFile(imageName)
Image_File.WriteFile(data, newImageName)
Thanks in advance for your help.