I/O: plain filenames, streaming with callbacks
Posted: 2012-04-06T13:40:18-07:00
I'm building an image library for Python on top of MagickCore, and for the life of me I can't get around these two issues with reading/writing files.
1. The `convert` utility has its own microsyntax around filenames, for specifying the format, extracting a specific frame, cropping and resizing, etc. This is mighty convenient when dealing with the terse environment of a command line. Unfortunately it seems that some of this behavior (the type prefixes and frame extraction, at least) exists deep in the core code, even within ReadImage.
Is there any way to circumvent or disable the special syntax? A high-level language API doesn't need the same kind of shorthand, and I don't want end users having to worry about filenames not actually being filenames in some cases, especially when it's not well documented what parts of the filename syntax ReadImage supports.
I tried adding backslashes before colons and brackets, but GetPathComponent doesn't seem to respect backslashes. My current solution is to fopen the file myself and only pass ReadImage a FILE*, but then ImageMagick can't fall back to guessing the filetype based on the extension.
2. Python has an API that allows an object pretend to be a filehandle. Such an object can then stream from a database, generate data as it's read, etc. I'd like to be able to read images from and write images to such objects, and it seems like this should be possible; most decoders already use the ReadBlob abstraction to read chunks at a time from an opaque source.
I can't bridge this gap, though. Virtually all of stream.c is only defined in stream-private.h, making it off-limits. There's a FifoStream blob type that almost seems appropriate, but ReadBlob doesn't know how to read from such blobs. Right now the best I can do is create an in-memory buffer of the entire encoded image, then pass that from Python to BlobToImage or vice versa.
Is there a way to read/write based on arbitrary callbacks?
1. The `convert` utility has its own microsyntax around filenames, for specifying the format, extracting a specific frame, cropping and resizing, etc. This is mighty convenient when dealing with the terse environment of a command line. Unfortunately it seems that some of this behavior (the type prefixes and frame extraction, at least) exists deep in the core code, even within ReadImage.
Is there any way to circumvent or disable the special syntax? A high-level language API doesn't need the same kind of shorthand, and I don't want end users having to worry about filenames not actually being filenames in some cases, especially when it's not well documented what parts of the filename syntax ReadImage supports.
I tried adding backslashes before colons and brackets, but GetPathComponent doesn't seem to respect backslashes. My current solution is to fopen the file myself and only pass ReadImage a FILE*, but then ImageMagick can't fall back to guessing the filetype based on the extension.
2. Python has an API that allows an object pretend to be a filehandle. Such an object can then stream from a database, generate data as it's read, etc. I'd like to be able to read images from and write images to such objects, and it seems like this should be possible; most decoders already use the ReadBlob abstraction to read chunks at a time from an opaque source.
I can't bridge this gap, though. Virtually all of stream.c is only defined in stream-private.h, making it off-limits. There's a FifoStream blob type that almost seems appropriate, but ReadBlob doesn't know how to read from such blobs. Right now the best I can do is create an in-memory buffer of the entire encoded image, then pass that from Python to BlobToImage or vice versa.
Is there a way to read/write based on arbitrary callbacks?