2007-10-30 12:49:04 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright 2007 Edgard Lima <edgard.lima@indt.org.br>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the
|
|
|
|
* GNU Lesser General Public License Version 2.1 (the "LGPL"), in
|
|
|
|
* which case the following provisions apply instead of the ones
|
|
|
|
* mentioned above:
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2007-11-18 21:06:51 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2007-10-30 12:49:04 +00:00
|
|
|
#include "metadataparse.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
*static declarations
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
metadataparse_parse_none (ParseData * parse_data, const guint8 * buf,
|
|
|
|
guint32 * bufsize, guint8 ** next_start, guint32 * next_size);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* extern implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
metadataparse_init (ParseData * parse_data)
|
|
|
|
{
|
|
|
|
parse_data->state = STATE_NULL;
|
|
|
|
parse_data->img_type = IMG_NONE;
|
|
|
|
parse_data->option = PARSE_OPT_ALL;
|
2007-11-18 21:06:51 +00:00
|
|
|
parse_data->offset = 0;
|
|
|
|
memset (&parse_data->exif, 0x00, sizeof (MetadataChunk));
|
|
|
|
memset (&parse_data->iptc, 0x00, sizeof (MetadataChunk));
|
|
|
|
memset (&parse_data->xmp, 0x00, sizeof (MetadataChunk));
|
2007-10-30 12:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* offset: number of bytes to jump (just a hint to jump a chunk)
|
|
|
|
* size: number of bytes to read on next call (just a hint to get all chunk)
|
|
|
|
* return:
|
|
|
|
* -1 -> error
|
|
|
|
* 0 -> done
|
|
|
|
* 1 -> need more data
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
metadataparse_parse (ParseData * parse_data, const guint8 * buf,
|
|
|
|
guint32 bufsize, guint32 * next_offset, guint32 * next_size)
|
|
|
|
{
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
guint8 *next_start = (guint8 *) buf;
|
|
|
|
|
|
|
|
if (parse_data->state == STATE_NULL) {
|
|
|
|
ret =
|
|
|
|
metadataparse_parse_none (parse_data, buf, &bufsize, &next_start,
|
|
|
|
next_size);
|
|
|
|
if (ret == 0)
|
|
|
|
parse_data->state = STATE_READING;
|
|
|
|
else
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (parse_data->img_type) {
|
|
|
|
case IMG_JPEG:
|
|
|
|
ret =
|
|
|
|
metadataparse_jpeg_parse (&parse_data->format_data.jpeg,
|
2007-11-18 21:06:51 +00:00
|
|
|
(guint8 *) buf, &bufsize, parse_data->offset, &next_start, next_size);
|
2007-10-30 12:49:04 +00:00
|
|
|
break;
|
|
|
|
case IMG_PNG:
|
2007-11-02 16:50:42 +00:00
|
|
|
ret =
|
|
|
|
metadataparse_png_parse (&parse_data->format_data.png,
|
2007-11-18 21:06:51 +00:00
|
|
|
(guint8 *) buf, &bufsize, parse_data->offset, &next_start, next_size);
|
2007-10-30 12:49:04 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* unexpected */
|
|
|
|
ret = -1;
|
|
|
|
goto done;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*next_offset = next_start - buf;
|
2007-11-18 21:06:51 +00:00
|
|
|
parse_data->offset += *next_offset;
|
2007-10-30 12:49:04 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
|
2007-10-30 18:21:22 +00:00
|
|
|
if (ret == 0) {
|
|
|
|
parse_data->state = STATE_DONE;
|
|
|
|
}
|
|
|
|
|
2007-10-30 12:49:04 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
metadataparse_dispose (ParseData * parse_data)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (parse_data->img_type) {
|
|
|
|
case IMG_JPEG:
|
|
|
|
metadataparse_jpeg_dispose (&parse_data->format_data.jpeg);
|
|
|
|
break;
|
2007-11-02 16:50:42 +00:00
|
|
|
case IMG_PNG:
|
|
|
|
metadataparse_png_dispose (&parse_data->format_data.png);
|
|
|
|
break;
|
2007-10-30 12:49:04 +00:00
|
|
|
}
|
|
|
|
|
2007-11-18 21:06:51 +00:00
|
|
|
if (parse_data->xmp.adapter) {
|
|
|
|
gst_object_unref (parse_data->xmp.adapter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parse_data->iptc.adapter) {
|
|
|
|
gst_object_unref (parse_data->iptc.adapter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parse_data->exif.adapter) {
|
|
|
|
gst_object_unref (parse_data->exif.adapter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parse_data->xmp.buffer) {
|
|
|
|
free (parse_data->xmp.buffer);
|
2007-10-30 12:49:04 +00:00
|
|
|
}
|
|
|
|
|
2007-11-18 21:06:51 +00:00
|
|
|
if (parse_data->iptc.buffer) {
|
|
|
|
free (parse_data->iptc.buffer);
|
2007-10-30 12:49:04 +00:00
|
|
|
}
|
|
|
|
|
2007-11-18 21:06:51 +00:00
|
|
|
if (parse_data->exif.buffer) {
|
|
|
|
free (parse_data->exif.buffer);
|
2007-10-30 12:49:04 +00:00
|
|
|
}
|
|
|
|
|
2007-11-18 21:06:51 +00:00
|
|
|
memset (&parse_data->xmp, 0x00, sizeof (MetadataChunk));
|
|
|
|
memset (&parse_data->iptc, 0x00, sizeof (MetadataChunk));
|
|
|
|
memset (&parse_data->exif, 0x00, sizeof (MetadataChunk));
|
|
|
|
|
2007-10-30 12:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* static implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* find image type */
|
|
|
|
static int
|
|
|
|
metadataparse_parse_none (ParseData * parse_data, const guint8 * buf,
|
|
|
|
guint32 * bufsize, guint8 ** next_start, guint32 * next_size)
|
|
|
|
{
|
|
|
|
|
|
|
|
int ret = -1;
|
2007-11-18 21:06:51 +00:00
|
|
|
MetadataChunk *exif = NULL;
|
|
|
|
MetadataChunk *iptc = NULL;
|
|
|
|
MetadataChunk *xmp = NULL;
|
2007-10-30 12:49:04 +00:00
|
|
|
|
2007-11-18 21:06:51 +00:00
|
|
|
*next_start = (guint8 *) buf;
|
2007-10-30 12:49:04 +00:00
|
|
|
|
|
|
|
parse_data->img_type = IMG_NONE;
|
|
|
|
|
2007-11-02 16:50:42 +00:00
|
|
|
if (*bufsize < 3) {
|
|
|
|
*next_size = 3;
|
2007-10-30 12:49:04 +00:00
|
|
|
ret = 1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parse_data->option & PARSE_OPT_EXIF)
|
2007-11-18 21:06:51 +00:00
|
|
|
exif = &parse_data->exif;
|
2007-10-30 12:49:04 +00:00
|
|
|
if (parse_data->option & PARSE_OPT_IPTC)
|
2007-11-18 21:06:51 +00:00
|
|
|
iptc = &parse_data->iptc;
|
2007-10-30 12:49:04 +00:00
|
|
|
if (parse_data->option & PARSE_OPT_XMP)
|
2007-11-18 21:06:51 +00:00
|
|
|
xmp = &parse_data->xmp;
|
2007-10-30 12:49:04 +00:00
|
|
|
|
|
|
|
if (buf[0] == 0xFF && buf[1] == 0xD8 && buf[2] == 0xFF) {
|
2007-11-18 21:06:51 +00:00
|
|
|
metadataparse_jpeg_init (&parse_data->format_data.jpeg, exif, iptc, xmp);
|
2007-11-02 16:50:42 +00:00
|
|
|
ret = 0;
|
2007-10-30 12:49:04 +00:00
|
|
|
parse_data->img_type = IMG_JPEG;
|
2007-11-02 16:50:42 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*bufsize < 8) {
|
|
|
|
*next_size = 8;
|
|
|
|
ret = 1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf[0] == 0x89 && buf[1] == 0x50 && buf[2] == 0x4E && buf[3] == 0x47 &&
|
|
|
|
buf[4] == 0x0D && buf[5] == 0x0A && buf[6] == 0x1A && buf[7] == 0x0A) {
|
2007-11-18 21:06:51 +00:00
|
|
|
metadataparse_png_init (&parse_data->format_data.png, exif, iptc, xmp);
|
2007-11-02 16:50:42 +00:00
|
|
|
ret = 0;
|
2007-10-30 12:49:04 +00:00
|
|
|
parse_data->img_type = IMG_PNG;
|
2007-11-02 16:50:42 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2007-10-30 12:49:04 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|