ref: 6ffb25b255e9533860e0e437d00839a256f12e02
parent: ecdd28cd75c37a3550245e44c329ec9d5362bdcc
author: Jean-Marc Valin <jmvalin@jmvalin.ca>
date: Mon Feb 5 09:04:25 EST 2018
Add option to use picture from memory
--- a/include/opusenc.h
+++ b/include/opusenc.h
@@ -209,7 +209,7 @@
*/
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)
@@ -217,6 +217,16 @@
\return Error code
*/
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);
/*@}*/
/*@}*/
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -147,6 +147,18 @@
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;
--- a/src/picture.c
+++ b/src/picture.c
@@ -342,7 +342,6 @@
&width,&height,&depth,&colors,&has_palette);
}
else{
- free(buf);
*error = OPE_INVALID_PICTURE;
return NULL;
}
@@ -354,7 +353,6 @@
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 @@
} else {
*error = OPE_ALLOC_FAIL;
}
- free(buf);
return out;
}
@@ -399,6 +396,7 @@
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;
@@ -405,5 +403,32 @@
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;
}
--- a/src/picture.h
+++ b/src/picture.h
@@ -42,6 +42,9 @@
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); \