Add option to use picture from memory

parent ecdd28cd
......@@ -209,7 +209,7 @@ OPE_EXPORT int ope_comments_add(OggOpusComments *comments, const char *tag, cons
*/
OPE_EXPORT int ope_comments_add_string(OggOpusComments *comments, const char *tag_and_val);
/** Add a picture.
/** Add a picture from a file.
\param[in,out] comments Where to add the comments
\param filename File name for the picture
\param picture_type Type of picture (-1 for default)
......@@ -218,6 +218,16 @@ OPE_EXPORT int ope_comments_add_string(OggOpusComments *comments, const char *ta
*/
OPE_EXPORT int ope_comments_add_picture(OggOpusComments *comments, const char *filename, int picture_type, const char *description);
/** Add a picture already in memory.
\param[in,out] comments Where to add the comments
\param ptr Pointer to picture in memory
\param size Size of picture pointed to by ptr
\param picture_type Type of picture (-1 for default)
\param description Description (NULL means no comment)
\return Error code
*/
OPE_EXPORT int ope_comments_add_picture_from_memory(OggOpusComments *comments, const char *ptr, size_t size, int picture_type, const char *description);
/*@}*/
/*@}*/
......
......@@ -147,6 +147,18 @@ int ope_comments_add_picture(OggOpusComments *comments, const char *filename, in
return OPE_OK;
}
int ope_comments_add_picture_from_memory(OggOpusComments *comments, const char *ptr, size_t size, int picture_type, const char *description) {
char *picture_data;
int err;
picture_data = _ope_parse_picture_specification_from_memory(ptr, size, picture_type, description, &err, &comments->seen_file_icons);
if (picture_data == NULL || err != OPE_OK){
return err;
}
_ope_comment_add(&comments->comment, &comments->comment_length, "METADATA_BLOCK_PICTURE", picture_data);
free(picture_data);
return OPE_OK;
}
typedef struct EncStream EncStream;
......
......@@ -342,7 +342,6 @@ static char *_ope_parse_picture_specification_impl(unsigned char *buf, size_t nb
&width,&height,&depth,&colors,&has_palette);
}
else{
free(buf);
*error = OPE_INVALID_PICTURE;
return NULL;
}
......@@ -354,7 +353,6 @@ static char *_ope_parse_picture_specification_impl(unsigned char *buf, size_t nb
if(picture_type==1&&(width!=32||height!=32
||strlen(mime_type)!=9
||oi_strncasecmp("image/png",mime_type,9)!=0)){
free(buf);
*error = OPE_INVALID_ICON;
return NULL;
}
......@@ -390,7 +388,6 @@ static char *_ope_parse_picture_specification_impl(unsigned char *buf, size_t nb
} else {
*error = OPE_ALLOC_FAIL;
}
free(buf);
return out;
}
......@@ -399,11 +396,39 @@ char *_ope_parse_picture_specification(const char *filename, int picture_type, c
size_t nbuf;
size_t data_offset;
unsigned char *buf;
char *ret;
if (picture_type < 0) picture_type=3;
if (!validate_picture_type(picture_type, *seen_file_icons)) {
*error = OPE_INVALID_PICTURE;
return NULL;
}
buf = _ope_read_picture_file(filename, description, error, &nbuf, &data_offset);
return _ope_parse_picture_specification_impl(buf, nbuf, data_offset, picture_type, description, error, seen_file_icons);
if (buf == NULL) return NULL;
ret = _ope_parse_picture_specification_impl(buf, nbuf, data_offset, picture_type, description, error, seen_file_icons);
free(buf);
return ret;
}
char *_ope_parse_picture_specification_from_memory(const char *mem, size_t size, int picture_type, const char *description,
int *error, int *seen_file_icons){
size_t nbuf;
size_t data_offset;
unsigned char *buf;
char *ret;
if (picture_type < 0) picture_type=3;
if (!validate_picture_type(picture_type, *seen_file_icons)) {
*error = OPE_INVALID_PICTURE;
return NULL;
}
data_offset=32+strlen(description)+10;
nbuf = data_offset + size;
buf = (unsigned char *)malloc(nbuf);
if (buf == NULL) {
*error = OPE_ALLOC_FAIL;
return NULL;
}
memcpy(buf+data_offset, mem, size);
ret = _ope_parse_picture_specification_impl(buf, nbuf, data_offset, picture_type, description, error, seen_file_icons);
free(buf);
return ret;
}
......@@ -42,6 +42,9 @@ typedef enum{
char *_ope_parse_picture_specification(const char *filename, int picture_type, const char *description,
int *error, int *seen_file_icons);
char *_ope_parse_picture_specification_from_memory(const char *mem, size_t size, int picture_type, const char *description,
int *error, int *seen_file_icons);
#define WRITE_U32_BE(buf, val) \
do{ \
(buf)[0]=(unsigned char)((val)>>24); \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment