PDA

View Full Version : How to find out a MIMETYPE of an image?



ommharidaas
14th December 2011, 09:36
Hi everyone,
My application needs to open an image file to be set as a background of a qgraphicsview.
When I select the file from qfiledialog, I need to find out if that file has a particular MIMETYPE.
Is there any way to do that??
Need all the help I can get.
Thnx.

MarekR22
14th December 2011, 10:49
Why?
MIMETYPE is needed during copy paste.


QGraphicsView view;
view.setBackgroundBrush(QBrush(QImage("fileName.png")))

ommharidaas
15th December 2011, 08:04
Why?
MIMETYPE is needed during copy paste.


QGraphicsView view;
view.setBackgroundBrush(QBrush(QImage("fileName.png")))


Let me ask the question in another context.
Any idea about how can i find out if particular image is .svg (Pure) or an .SVG converted from .bmp??
This will answer my previous question automatically.
Thnx again.

ChrisW67
15th December 2011, 08:49
Let me ask the question in another context.
Any idea about how can i find out if particular image is .svg (Pure) or an .SVG converted from .bmp??

The mime type of the file would be very unlikely to tell these two apart anyway: both would be "image/svg+xml".

You could tell if an SVG file contains embedded raster images by parsing the file looking for the <image> element. If the xlink:href attribute points at something other than another SVG file, or contains a base64 data block then you have an embedded raster. There is a small possibility that a base64 data block is not a raster image and the only way to detect that would be to decode the data and check its type.

MarekR22
15th December 2011, 09:20
SVG is a xml file.
I don't know how exactly converter makes SVG from BMP, but in first step I would try to see content of this xml and figue out how is it done.
Then I would check if bool QSvgRenderer::elementExists ( const QString & id ) const can be used for detection if it is result of conversion.
In last step I would parse xml.

ommharidaas
15th December 2011, 11:20
The mime type of the file would be very unlikely to tell these two apart anyway: both would be "image/svg+xml".

You could tell if an SVG file contains embedded raster images by parsing the file looking for the <image> element. If the xlink:href attribute points at something other than another SVG file, or contains a base64 data block then you have an embedded raster. There is a small possibility that a base64 data block is not a raster image and the only way to detect that would be to decode the data and check its type.


Thnx ChrisW67,
Even i thought the same, both of them will have type "image/svg+xml".
Do u think it is wise to actually go ahead and try to decode that base64 data block???
If yes, how should i go about it??
If no, is there a way around it??



The mime type of the file would be very unlikely to tell these two apart anyway: both would be "image/svg+xml".

You could tell if an SVG file contains embedded raster images by parsing the file looking for the <image> element. If the xlink:href attribute points at something other than another SVG file, or contains a base64 data block then you have an embedded raster. There is a small possibility that a base64 data block is not a raster image and the only way to detect that would be to decode the data and check its type.


Thnx ChrisW67,
Even i thought the same, both of them will have type "image/svg+xml".
Do u think it is wise to actually go ahead and try to decode that base64 data block???
If yes, how should i go about it??
If no, is there a way around it??


Added after 6 minutes:


SVG is a xml file.
I don't know how exactly converter makes SVG from BMP, but in first step I would try to see content of this xml and figue out how is it done.
Then I would check if bool QSvgRenderer::elementExists ( const QString & id ) const can be used for detection if it is result of conversion.
In last step I would parse xml.



Thnx MarekR22,
Will check with bool QSvgRenderer::elementExists ( const QString & id ) const and see if it works.

ChrisW67
15th December 2011, 22:42
Do u think it is wise to actually go ahead and try to decode that base64 data block???
If yes, how should i go about it??
If no, is there a way around it??

Wise? Yes, if you want to be absolutely certain. However, the chance of someone embedding another SVG using <image> and base64 encoding, rather than either directly inserting the SVG elements or referring to a separate file, seems very low. Only you know your requirements and likely inputs well enough to know whether the probability and effect of a false detection is acceptable or not. If your requirement is simply to detect the use of embedded images then there's no need to look further than an <image> tag.

You can use QByteArray::fromBase64() once you have extracted the base 64 data from the attribute. If the result starts with "<?xml" (possibly preceded by a byte order mark) then you have an SVG, otherwise a PNG, JPG or other bitmap format.