0

I get a buffer from a function in a library. It should be an image buffer. I use OpenCV's function cv::imdecode(...) to transform it in a cv::Mat. Sometimes the image is removed before I get the buffer, so in that case I expected that the buffer is empty, but I just found that it contains an .xml, containing some kind of message that the image is not found. So: Is there a way to verify that the buffer contains an .xml and not a .jpg? Or how to verify this?


EDIT:

Based on the comment I want to search if the buffer begins with "<?xml".

imgOut = cv::imdecode(imgBuf, CV_LOAD_IMAGE_COLOR);
if (imgOut.empty())
{
    std::string xmlStart = "<?xml";
    std::string bufStart(imgBuf.begin(), imgBuf.begin() + 5);
    if (bufStart == xmlStart)
    {
        throw ImageNotAvailableException()
    }
    throw ImageDecodeException(bucketNameIn, objectNameIn);
}

cv::imdecode is returning an empty cv::Mat if the decode fails. Is it OK to do it like this (imgBuf is a std::vector< uchar >)?

sop
  • 3,445
  • 8
  • 41
  • 84
  • 1
    if it is a well formed xml then it should conform to the spec: http://www.w3.org/TR/xml/#sec-well-formed also related: http://stackoverflow.com/questions/891223/better-way-to-detect-xml so you could try to parse it as an xml and if it fails then try to look for a jpg header: http://en.wikipedia.org/wiki/JPEG#Syntax_and_structure – EdChum Aug 07 '14 at 07:55
  • @EdChum I have edited the question, can you please help? – sop Aug 07 '14 at 09:15
  • unless you're still looking for a image contained *inside* the xml, why bother ? if imdecode can't parse it, it does not matter if it was xml, pdf or arbitrary junk. – berak Aug 07 '14 at 10:14
  • It looks like you want to distinguish between a missing image and a corrupted image; is that the goal? – David K Aug 07 '14 at 11:13
  • @DavidK Not really. I want to do something more if the image was deleted. Because the `.xml` case (`deleted image` case) is not often, I have put the `throw ImageNotAvailableException();` in the `imgOut.empty()` case. – sop Aug 07 '14 at 13:45
  • That is what I meant by "distinguish": something in the software says whether the read failed due to one reason or the other. In this case that "something" is the type of exception thrown. And presumably, the handlers of those exceptions produce some further difference in results, otherwise you haven't _really_ done anything different in the two cases. – David K Aug 07 '14 at 14:21

1 Answers1

0

Probably you can check the jpeg header.

Acourding to this article the jpeg heder begin with (FFD8) http://www.fastgraph.com/help/jpeg_header_format.html

Attila
  • 16
  • 2