Redirect Standard C++ Functions

Apr 8, 2012 at 1:01am
Hello

I have a project with image processing with my own controller.The images have to be read from an sd card nad then write them back in.The sd cards will be formated with fat32 and im using a library to access the card.

Im also using libtiff for the image processing.But the libtiff uses the C standard file functions ( fopen , fclose ... ) and i have to use the functions from the fat library.

Is there any way to redirect calls to standard file functions to my fat functions?(since i cant change the code from libtiff).

If you could just explain of provide some links.

Thanks

Apr 8, 2012 at 6:16pm
Anyone? Someone who has worked with libtiff or a general solution.

I saw that libtiff provides a function for redirecting read/write operations
and some handlers for
1
2
3
4
5
6
7
8
9
10
11
TIFF* TIFFClientOpen(const char* filename, const char* mode, thandle_t clientdata,
    TIFFReadWriteProc readproc, TIFFReadWriteProc writeproc, TIFFSeekProc seekproc,
    TIFFCloseProc closeproc, TIFFSizeProc sizeproc, TIFFMapFileProc mapproc,
    TIFFUnmapFileProc unmapproc)

typedef tsize_t (*TIFFReadWriteProc)(thandle_t, tdata_t, tsize_t);
typedef toff_t (*TIFFSeekProc)(thandle_t, toff_t, int);
typedef int (*TIFFCloseProc)(thandle_t);
typedef toff_t (*TIFFSizeProc)(thandle_t);
typedef int (*TIFFMapFileProc)(thandle_t, tdata_t*, toff_t*);
typedef void (*TIFFUnmapFileProc)(thandle_t, tdata_t, toff_t);


The fat library gives the following functions for the operation libtiff needs
1
2
3
4
FRESULT f_read (FIL*, void*, UINT, UINT*);	/* Read data from a file */
FRESULT f_write (FIL*, const void*, UINT, UINT*);	/* Write data to a file */
FRESULT f_lseek (FIL*, DWORD);	/* Move file pointer of a file object */
FRESULT f_close (FIL*);			/* Close an open file object */


Any ideas how i could set those handlers with my functions?Im having trouble with the number of arguments for the handlers and the functions .

Apr 9, 2012 at 2:01am
Damn stupid forum just lost me a large response I typed in for you.... ***FRUSTRATED****

You need to create functions called "trampolines" to stand between the mbed functions and the libTIFF functions. Let me try to type them in again!!!!!!!!!!!!!!!!!!!!!!!!! ***ANGRY****

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
tsize_t my_tiff_read_proc( thandle_t handle, tdata_t buffer, tsize_t size )
{
  UINT result;
  if (f_read( (FIL*)handle, (void*)buffer, (UINT)size, &result ) == FR_OK) return result;
  else return 0;
}

tsize_t my_tiff_write_proc( thandle_t handle, tdata_t buffer, tsize_t size )
{
  UINT result;
  if (f_write( (FIL*)handle, (const void*)buffer, (UINT)size, &result ) == FR_OK) return result;
  else return 0;
}

toff_t my_tiff_seek_proc( thandle_t handle, toff_t offset, int origin )
{
  if (origin != SEEK_SET)
  {
    errno = EINVAL;
    return -1;
  }
  switch (f_seek( (FIL*)handle, (DWORD)offset ))
  {
    case FR_OK: return 0;
    case FR_INVALID_PARAMETER: errno = EINVAL; break;
    default: errno = EBADF;
  }
  return -1;
}

int my_tiff_close_proc( thandle_t handle )
{
  f_close( (FIL*)handle );
  return 0;
}

Next, you need to open the mbed FAT file yourself.

1
2
3
4
5
FIL fat_file;
if (!f_open( &fat_file, "myfile.tiff", FA_READ ))
{
  fooey();
}

Now you need to tell libTIFF to treat it as an open file:

1
2
3
4
5
6
7
8
9
10
11
12
TIFF* tiff =
TIFFClientOpen( "myfile.tiff", "r",
  &my_tiff_read_proc,
  &my_tiff_write_proc,
  &my_tiff_seek_proc,
  &my_tiff_close_proc,
  NULL, NULL, NULL );
if (!tiff)
{
  f_close( &fat_file );
  fooey();
}

Notice that you must provide the appropriate read and/or write handlers, and you must provide a seek proc (as the file must have random access), and a close proc.

Hope this helps.
Apr 9, 2012 at 11:38am
In the general case, you can provide your own functions to replace system calls using an LD_PRELOAD shared object. Google that for more information.
Topic archived. No new replies allowed.