Module Extension problem

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.
 
F

Florian Gross

Saumya said:
processImg.rb:7:in `OpenFile': cannot convert Module into String
(TypeError)
from /home/usr/johnDoe/processImg.rb:7

This should fix it AFAIK:
static VALUE getFile(VALUE nameFile){
static VALUE getFile(VALUE self, VALUE nameFile){
static VALUE writeFile(VALUE data, VALUE nameFile){
static VALUE writeFile(VALUE self, VALUE data, VALUE nameFile){
Thanks in advance for your help.

Hope I could help. :)

Regards,
Florian Gross
 
S

Saumya Garg

Thanks for the response.
Unfortunately, that suggestion did not work.
Using those modifications I got the following error:

processImg.rb:7:in `OpenFile': wrong argument type Module (expected
Class) (TypeError)
from /home/usr/JohnDoe/processImg.rb:7

Line 7 refers to the line: data = Image_File.OpenFile(imageName)

Saumya
 
T

ts

S> Using those modifications I got the following error:

S> processImg.rb:7:in `OpenFile': wrong argument type Module (expected
S> Class) (TypeError)
S> from /home/usr/JohnDoe/processImg.rb:7

S> VALUE data = Data_Make_Struct(data,float,0,free, buffer);

This line is wrong. The first argument must be a class.


Guy Decoux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top