Adds a new public method group, similar to FreeImage_Load called FreeImage_LoadHeader.
Updates the majority of plugins to support this mode.
The patch is in GIT format due to the inclusion of binary files.
Anonymous
View and moderate all "patches Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Patches"
GIT format patch file exported from Mercurial
Hi there,
since not only true header information but also metadata has been requested to be loaded without the actual pixel data, I recommend choosing a different approach.
What about introducing a common load flag DO_NOT_LOAD_PIXEL_DATA, which each plugin may respect. Then, a flag in FreeImage's internal FIBITMAP structure could determine, whether a FIBITMAP actually has pixel data associated or not. In any case, image manipulating functions need to check this flag by invoking the new public function BOOL FreeImage_HasPixelData(FIBITMAP dib), before actually accessing pixel data.
Maybe such an approach would introduce less changes on the public API?
Additionally, I suggest using a void pointer in the internal FIBITMAP structure, which may point to an image's actual pixel data, rather than a BOOL flag to determine whether an image has pixel data or not. A NULL pointer signals, that there is no pixel data associated with this image.
Currently, an image's pixel data is stored at the memory which follows the internal FIBITMAP structure. This could be left unchanged for images, that have been loaded and decoded by FreeImage. In this case, the pointer would just point to a memory location some bytes ahead of the pointer itself... However, this would offer the possibility to wrap arbitrary memory as a FIBITMAP (like a cast) in the future.
I've had an implementation of the above... However, currently I'm no longer sure whether I still have it.
@Hervé: would this be an interesting new feature for any of the next releases?
Carsten
View and moderate all "patches Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Patches"
Hi.
If the suggestion by Carsten is more likely to be accepted into the main line then I will implement that, if the existing patch no longer exists.
I'd still require a FreeImage_SupportsLoadHeaderOnly or equivalent public API method, which could query the plugin for whether the global load flag is supported or not.
The current implementation isn't something I'm particularly proud of - it's just the quickest way I could find to implement the feature that I required, and Carstens suggested method seems more in keeping with the rest of the API.
However, the problem solved by adding the MaybeHeader functions, which are the least pleasant parts of the patch, would still be needed - the only real change under this change would be that the LoadHeader function and related Load->InternalLoad plugin changes would go away.
Let me know.
Cheers.
Tom.
Hi Tom,
I'm with you. A public method to determine whether a plugin supports this DO_NOT_LOAD_PIXEL_DATA flag is a must, of course. This method will be just another plugin function like the FreeImage_FIFSupportsReading or FreeImage_FIFSupportsWriting functions.
Carsten
Ok. I've done the no_pixel_data flag change. Much better, that was a good idea.
There's no checking within the pixel access functions to see if the DIB is the no pixel variety yet).
Any recommendations as to how that would be done? In my use case I don't really care about this error checking, as the DIB is very short lived, but I'll add it for the sake of completeness.
Attaching patch now..
View and moderate all "patches Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Patches"
GIT format patch file with new FI_DO_NOT_LOAD_PIXELS load flag.
Hi, reading the comments its seams the solution is basically the same as the one I suggested half an year ago :
https://sourceforge.net/projects/freeimage/forums/forum/36111/topic/3323305
I'm waiting on comments on the feasibility of implementing such a (requested) feature that way, and I'm glad the issue surfaced again here.
View and moderate all "patches Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Patches"
Hi.
Yeah, the attached patch implements everything in your post, apart from the HasPixels flag in the header. But there seems no interest from the maintainers, so I guess I'll be maintaining the patch separately from the FreeImage source. Until it's folded into the main project, you can get a copy of the source which has this feature at http://bitbucket.org/xg/freeimage-loadheader/
Hi, Im impressed, you have already modified all plug-ins!
However, scanning through the .patch, it seams you just bail out from loading just after allocation, but this way no metadata will be written! This is a major drawback.
Also, I really do not see the need for FI_SupportsNoPixelsProc and FreeImage_FIFSupportsNoPixels family of functions!
FIFSupports* function test for some feature of the file format, but isn't loading metadata-only possible with all image formats?
Event if some format does not support this feature, it is possible to mimic it the same way you do it in the patch - just allocate and return!
So, in the end ALL plug ins can and will have the power to load without pixels, and if for some reason some plug does not do that, it will just load all as usual, which is completely harmless for the user.
My point is LOAD_NO_PIXELS can be just a hint, no need for extending the plug-in API with more functions.
Also, as side note, the Allocate* function(s) should have a (int) flags option (instead of new name and/or boolean value) this will solve may extensions requests without braking API/ABI...
...too bad the 3.13 is already out with the a new Ex version, but again without any options flag :(
View and moderate all "patches Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Patches"
If I had added support to all possible plugins (which is of course imposible), then yes I wouldn't have added the supports-no-pixels function. As it is, I have not because in my use case it is vital to know whether the header information is able to be loaded separately.
I haven't updated all plugins in the distribution, either - just the ones I could see a trivial way of updating to work. Other plugins (mng, I think is a good example, though I can't quite remember which ones I haven't updated) use other methods to load which made it very difficult for me to early-out once getting the information I required.
The driving factor behind my patch was to increase loading times. I have attempted to retrieve as much meta data as possible before earlying out in the plugin load functions, but total metadata reconstruction wasn't high on my list of priorities.
As for taking int flags into the allocate functions, that makes a lot of sense.
Hope that helps.
cklein05: "Additionally, I suggest using a void pointer in the internal FIBITMAP
structure, which may point to an image's actual pixel data, rather than a
BOOL flag to determine whether an image has pixel data or not. A NULL
pointer signals, that there is no pixel data associated with this image.
Currently, an image's pixel data is stored at the memory which follows the
internal FIBITMAP structure. This could be left unchanged for images, that
have been loaded and decoded by FreeImage. In this case, the pointer would
just point to a memory location some bytes ahead of the pointer itself...
However, this would offer the possibility to wrap arbitrary memory as a
FIBITMAP (like a cast) in the future.
I've had an implementation of the above... However, currently I'm no
longer sure whether I still have it."
Can you elaborate more on the implementation?
Looking for a way GetBits to return NULL, I checked GetPalette witch does much the same thing for the palette. It returns NULL by manually checking bPP. So the pointer itself is not null, and cannot be, because its just offset from the main dib.
What is your solution to return NULL from getBits without flag in the header?
Hi Mihail,
ok, first, I've found the not yet complete implementation of my suggestion. The easies approach would be to add a BOOL flag to the internal FREEIMAGEHEADER, that indicates, whether a FIBITMAP actually has pixel data or not. A public function FreeImage_HasPixelData (it is called FreeImage_HasImageData in my implementation, but likely HasPixelData would be more correct) could be used by both the API-user and the internal FreeImage functions (like the Toolkit functions).
First, I have no problems with a BOOL flag. I just see some future benefits, if we generally would use a pointer in FREEIMAGEHEADER, pointing to the actual pixel data. Then, we could easily "wrap" a FreeImage FIBITMAP over a pixel data, that is already present in memory. As an example, have a look a functions FreeImage_Acquire and FreeImage_AcquireT in my implementation.
Ok, currently, GetBits only returns an offset to FREEIMAGEHEADER, since the pixel data is stored directly behind the FREEIMAGEHEADER structure. So, replace the BOOL flag by a pointer to the pixel data:
struct FREEIMAGEHEADER
{
...
...
BYTE *bits_ptr;
//BYTE filler[1]; // fill to 32-bit alignment
}
Now, GetBits basically just returns
return (dib) ? ((FREEIMAGEHEADER *)dib->data)->bits_ptr : NULL;
So, the pointer bits_ptr could be NULL or could point to the pixel data, which could still be located directly after the FREEIMAGEHEADER structure or may easily be located anywhere else.
However, in my implementation there is a private function FreeImage_GetBitsPointer, which does this and GetBits just forwards to that function. This is due to the fact, that FREEIMAGEHEADER is private to BitmapAccess.cpp and GetBits is actually implemented in PixelAccess.cpp. Currently, in GetBits the pointer to the pixels is calculated only with sizes of public structures. However, accessing the new BYTE *bits_ptr member would require knowledge of FREEIMAGEHEADER in PixelAccess.cpp.
One approach would be to put FREEIMAGEHEADER into Utilities.h. My approach was to declare "inline BYTE * FreeImage_GetBitsPointer" in Utilities.h as a private wrapper function.
However, I've found that the header files, that accompany my implementation, seem to be original FreeImage files and so miss all needed declarations. So, to make my sources compile, you will at least have to add a forward declaration of FreeImage_GetBitsPointer, which is implemented in BitmapAccess.cpp, to Utilities.h.
Furthermore, there is no declaration entry for the mentioned functions FreeImage_Acquire and FreeImage_AcquireT in FreeImage.h. If you want to test these, add the declaration.
The already mentioned function FreeImage_HasPixelData (FreeImage_HasImageData in my sources) then boils down to:
return FreeImage_GetBitsPointer(dib) != NULL;
This function has also been added to many FreeImage functions, that access pixel data (as the Toolkit or Pixel Access functions). Have a look at function FreeImage_GetScanLine:
return FreeImage_HasPixelData(dib) ? CalculateScanLine(...) : NULL;
Here, no check for NULL dibs is needed, since FreeImage_HasPixelData returns false, if dib is NULL. This is because FreeImage_GetBitsPointer returns NULL if dib is NULL.
I've just found, that there are still many functions, that still need this check. We will have all of the FreeImage functions.
So, when loading an image, including its pixel data, the are no changes required for the plugin. Typically, it first calls FreeImage_Allocate, which allocates the needed memory (FREEIMAGEHEADER + pixel data + palette etc.) as before and also initializes the new bits_ptr member so, that it points to the address, that is returned by FreeImage_GetBits in the current FreeImage version ("the offset").
If you load an image WITHOUT pixel data, things get a bit more tricky. To prevent FreeImage_Allocate from allocating the memory for the pixel data, you need to pass 0 (zero) for both the image's width and height. Then, the allocated memory excludes the space for the pixel data. After that, the bits_ptr also points to the address directly following the header. However, likely this is unallocated and unowned memory. So, after allocating with size 0, there are two steps remaining: setting the bits_ptr to NULL and reset the image's with and height to the values obtained from the decoded image file.
However, this is not yet complete. There is a function FreeImage_SetBitsPointer, however, you will need to add its declaration to Utilities.h. Then, use this to set the pointer to NULL. We still need private accessor function to set width and height accordingly. There is not yet any plugin implementation, that makes use of this features, so you will need to add this to the plugins.
I've attached my sources. Have a special look at the FreeImage_Acquire functions, which basically do the same as I suggest for the plugins. However, these functions can access the FREEIMAGEHEADER structure directly, since these are implemented in BitmapAccess.cpp. Maybe putting FREEIMAGEHEADER into Utilities.h would make some things easier. However, its up to you to decide.
I just hope, that this posting together with my code will give you enough guidance to go on in this (my) direction.
Carsten
Ok, the attachment is much too large. I will mail the sources mentioned in my posting to Mihail. Anyone else, that is interested in these sources, could freely request them by mail.
Carsten
OK, I read both your post and mail, also the code you send me.
The two main points of interest are:
(3) Work around the current structure of FreeImage sources.
Lets review the last one first.
(3) The missing of private header(s) in FI is unfortunate. The use of utilities.h as a vehicle to gain access to declarations from one .cpp to
another (trough glue functions) is ugly and unnecessary. Considering all code (declarations and functions) is internal, we should refactor
instead of workaround. (I rise this as an issue, because the Metadata portion also screams for some moving of code around... Every year I
learn it, I forgot it the next).
So, in general we should have at least one private header in the future.
only", "peek " etc. requests. Its natural, and can be made transparent (no new functions).
Now, as far as the implementation is concerned here is my suggestion:
AllocateEx that has a int flag. Checking it will create the dib as usual but with no real data allocated.
All plug-ins will be changed to use AllocateEx(..., flag) instead of Allocate(...)
Your idea in the mail is to have a new func (without a flag), that loads without pixels, than, in the loaders,to use function pointer to
handle loading, swapping which allocate (old or new) is used based on flag.
I personally prefer to have a flag because it will enable other scenarios too (for instance NO_METADATA, or whatever)
Of course all will be transparent to the user. The old Allocate/T calls AllocateEx without the flag, the flag itself is private.
Except for the flag and the new AllocateEx, one last modification must be made.
We need to know if we have pixels or not.
There are two parts for that.
A. Storing this information
B. Retrieving it
Lets again go backwards. Originally I also though about having some FreeImage_HasPixels function.
Now, the more I think about it (and applying the rule to have as few new functions as possible (be it private or not)), the more I feel using
GetBits to test this is the right way.
First, all implementations WILL make GetBits return null if no pixels, simply because its a right thing to do.
Second this matches to the GetPalette usage, where it is a way to both test if there is a palette AND get a pointer to it.This will be exactly
the case for GetBits - if null - no pixels, if not null - return pointer to the data.
No new functions, just a line in the docs and thats it. (To go one step further GetScanLine is no necessary to follow the same rule, for it
the perf. is important and any tests are not welcome)
Now, about soring the information. here it also seams obvious that the easiest and probably the right way is to extеnd, the already private
header FREEIMAGEHEADER.The simples way is a bool flag, the more powerful is to store the pointer itself and test for null.
The intersting part of course is that storing the pointer is also needed for 2. - to create dib fom externally allocated pixels.
I like this idea, but still think its too advanced for the public API. In its current implementation especially, the user has to do a lot of
book keeping (like keeping in sync with the metadata and/or palette, and of course remembering to manually deallocate); also to have knowledge
and tools to allocate aligned memory! In any way IF needed one can add a byte pointer param to AllocateEx to enable this scenario. (And
because AllocateEx is part of the private API one can use a default param for it and no code modifications will be necessary)
However, as I said, my main interest is the "Create dib with no allocated pixels" part, and I wish to concentrate on that for now."Create dib
from externally allocated pixels" is extension upon that as it seams and can be implemented later.
So, Lets summarize what is needed for point 1.:
A. have a global load flag
B. Allocate (trough a new function) a bitmap without pixels
C. Store the information if dib has pixels
D. Retrieve this information
To implement these in the simplest possible way we have to:
A. Just add new flag LOAD_NO_PIXELS 0x8 is free to use.
B. AllocateEx boils down to:
AllocateEx(..., flag)
{
...
//line 234
unsigned dib_size = FreeImage_GetImageSize( flag & LOAD_NO_PIXELS ? 0 : width, flag & LOAD_NO_PIXELS ? 0 : height, bpp);
bitmap->data = (BYTE *)FreeImage_Aligned_Malloc(dib_size * sizeof(BYTE), FIBITMAP_ALIGNMENT);
...
//line 243
FREEIMAGEHEADER *fih = (FREEIMAGEHEADER *)bitmap->data;
fih->has_pixels = !(flag & LOAD_NO_PIXELS);
// OR
// fih->pixels = (flag & LOAD_NO_PIXELS) ? NULL : FreeImage_CalculateBitsPointer(bitmap); //< from your implemetation
...
}
C. To store we have to add either has_pixels or pixels to FREEIMAGEHEADER and move it to private header
D. To retrieve we simply
GetBits(FIBITMAP* dib)
{
if((dib == NULL) || ((FREEIMAGEHEADER *)dib->data)->has_pixels == false)
return NULL;
// the current getBits code //
//or
// return (dib != NULL) ? ((FREEIMAGEHEADER *)dib->data)->pixels : NULL; // from your implementation
}
(Of course to support fully the pointer way will need some more functions)
To use this in the real world we have to:
the loading needs to be modified. The second part is a bit more involved, because one must dissect the loading code, find that part and skip
it)
dib) return; its trivial)
Now, I write all this with the knowledge, you suggest pretty much the same. And thats the point.
I simply wish we sync our views, and I think all this matches your intend in the most straightforward way possible.
I also wish, as I said, to concentrate mainly on point 1 here.
By now one thing is certain.
Its completely save to say - we have to add guard for every function using pixels. No matter the implementation.
Its also save to say GetBits CAN do this job. No matter the implementation.
And I think this is a good start!
In the flowing days I will open a bug to implement all this in the reverse order.
Starting from adding GetBits test where needed. I think its a safe bet.
Later we can decide on the implementation of the whole thing.
MihailNaydenov
View and moderate all "patches Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Patches"
mnaydenov:
It seems you've ignored the actual patch that I opened this ticket with and are talking about the privately mailed stuff. The steps you've outlined are available in the series of patches at http://bitbucket.org/xg/freeimage-loadheader
I've pretty much given up on getting this into the project, I'm maintaining that fork until I can find another library that supports this functionality.
View and moderate all "patches Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Patches"
Seems a very similar method was taken in the end, thanks. Did you actually use the patch? Just curious - some of the code is near-identical...