View Full Version : int handle to File* fp - possible?
Phil Steinmeyer
11-14-2006, 07:36 PM
My internal file system is built with _open, _read, _close, all of which use file handles of type int.
I'm also using the Independent JPEG Group's jpeg functions, but unfortunately, they appear to be entirely set up for FILE* type handles.
I'd like to move all my jpegs into a single large pack, accessed with my int handle file system. But the problem is, when I've got that pack open, my handles are ints, jpeg wants FILE*, so I'm hosed (it would seem).
Does anyone know of a solution to this - either a way to get the jpeg library to accept ints or even just a jpeg that has been fully read into memory already, or a way to convert an int handle to a FILE* handle, or some other solution?
I suppose I could close and re-open the pack file the way jpeg wants it, but that would likely be slow.
I could convert all my code to use FILE* or all the jpeg code to use int, but that would be a pain.
Anybody got a relatively easy/clean solution?
Hi Phil
(this is presuming you are building jpeg from source)
Jpeg allows you to replace the input handler used to read bytes from a file. Take a look at the file jdatasrc.c in the jpeg source. This is the standard input handler for jpeg, you can see it's using FILE* and fread etc, and goes by the name of jpeg_stdio_src.
You can make your own input handler by supplying all these functions, and within them you can handle whatever kind of data you need to. These will be called by jpeg whenever it needs to get more bytes from the file etc. iirc I just copied the jdatasrc.c file and replaced what was necessary.
Then in the actual jpeg file reading part of your program, if you are using your pack file, you would replace
jpeg_stdio_src(&cinfo, input_file);
with say,
jpeg_philswackypack_src(&cinfo, pack_handle);
GameStudioD
11-14-2006, 08:47 PM
I was just searching for something like this the other day. I do not want to go into the details, but you do not want to pass a FILE* to the jpeg library. Badness!
Here is some code examples of how to read jpeg & png files from memory:
http://tfc.duke.free.fr/
Hi, You probably should write your memory-access version, but you can convert, using the "io_buf" array. The unix "file handles" returned from this _open call are indices into the array.
In the header file on VC8, they have:
#define stdin (&__iob_func()[0])
So you should be able to use:
FILE *file = &__iob_func()[file_handle];
I think I have used " _io_buf + file_handle" on older systems.
I not sure if there is a function to do this? probably.
Sol_HSA
11-14-2006, 09:57 PM
I've written a "ramfile" hack for my packfile, cfl - you may want to look at the ramfile source. I've used it successfully with jpeg lib:
http://iki.fi/sol/zip/ramfile.zip
Captain Nemo
11-15-2006, 01:03 AM
_fdopen: Associate a stream with a file that was previously opened for low-level I/O
and the reverse:
_fileno: Gets the file descriptor associated with a stream
PeterM
11-15-2006, 01:45 AM
I do not want to go into the details, but you do not want to pass a FILE* to the jpeg library. Badness!
I may as well clarify since I know what he's talking about. On Windows, you can't call any file functions on a FILE* which was created in a different module. This includes the JPEG library calling fread on your FILE*.
However if you can get it to call you back and for you to do the reading for it, all is well, which is what the other posters are hinting at.
Bad Sector
11-15-2006, 02:39 AM
I may as well clarify since I know what he's talking about. On Windows, you can't call any file functions on a FILE* which was created in a different module. This includes the JPEG library calling fread on your FILE*.
Er? I used libjpeg on Windows in some programs and never had such a problem.
PeterM
11-15-2006, 02:57 AM
Did you use libjpeg as a DLL? That's when the problem occurs.
Bad Sector
11-15-2006, 04:01 AM
Did you use libjpeg as a DLL? That's when the problem occurs.
Ah no, i statically linked the lib, i never use DLLs when i don't have to :-).
PeterM
11-15-2006, 04:06 AM
Cool. Sorry for the mix-up, I should have phrased my previous post better.
Phil Steinmeyer
11-15-2006, 08:31 AM
I'm compiling static, so _fdopen is exactly what I need - thanks for the tip.
Phil
Phil Steinmeyer
11-15-2006, 09:44 AM
Hmm - so using fdopen seemed to work well with the jpeg library, but with the ogg library (Tremor) - not so much.
It appears there are functions in the library I can override/rewrite, and I'll likely do that, but I'm surprised that using fdopen works fine for jpeg but not ogg.
Both are statically linked, FWIW.
Thoughts?
(Using fdopen to do the conversion leads to a crash somewhere within the ogg stuff - haven't debugged it in detail yet...)
Phil Steinmeyer
11-15-2006, 10:49 AM
Well - I worked around the ogg issues by using ov_open_callbacks, which lets me provide my own file handling routines...
vBulletin v3.6.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.