1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001-2009 Josh Coalson
3 * Copyright (C) 2011-2013 Xiph.Org Foundation
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "FLAC/assert.h"
29 #include "share/grabbag.h" /* for grabbag__picture_parse_specification() etc */
31 #include "operations_shorthand.h"
33 static FLAC__bool import_pic_from(const char *filename, FLAC__StreamMetadata **picture, const char *specification, FLAC__bool *needs_write);
34 static FLAC__bool export_pic_to(const char *filename, const FLAC__StreamMetadata *picture, const char *pic_filename);
36 FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write)
38 FLAC__bool ok = true, has_type1 = false, has_type2 = false;
39 FLAC__StreamMetadata *picture = 0;
40 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
43 die("out of memory allocating iterator");
45 FLAC__metadata_iterator_init(iterator, chain);
47 switch(operation->type) {
48 case OP__IMPORT_PICTURE_FROM:
49 ok = import_pic_from(filename, &picture, operation->argument.specification.value, needs_write);
51 /* append PICTURE block */
52 while(FLAC__metadata_iterator_next(iterator))
54 if(!FLAC__metadata_iterator_insert_block_after(iterator, picture)) {
55 print_error_with_chain_status(chain, "%s: ERROR: adding new PICTURE block to metadata", filename);
56 FLAC__metadata_object_delete(picture);
61 /* check global PICTURE constraints (max 1 block each of type=1 and type=2) */
62 while(FLAC__metadata_iterator_prev(iterator))
65 FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
66 if(block->type == FLAC__METADATA_TYPE_PICTURE) {
67 if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
69 print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one 32x32 standard icon (type=1) PICTURE block", filename);
74 else if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
76 print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one icon (type=2) PICTURE block", filename);
82 } while(FLAC__metadata_iterator_next(iterator));
85 case OP__EXPORT_PICTURE_TO:
87 const Argument_BlockNumber *a = operation->argument.export_picture_to.block_number_link;
88 int block_number = (a && a->num_entries > 0)? (int)a->entries[0] : -1;
91 FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
92 if(block->type == FLAC__METADATA_TYPE_PICTURE && (block_number < 0 || i == (unsigned)block_number))
95 } while(FLAC__metadata_iterator_next(iterator) && 0 == picture);
98 flac_fprintf(stderr, "%s: ERROR: FLAC file has no PICTURE block\n", filename);
100 flac_fprintf(stderr, "%s: ERROR: FLAC file has no PICTURE block at block #%d\n", filename, block_number);
104 ok = export_pic_to(filename, picture, operation->argument.filename.value);
113 FLAC__metadata_iterator_delete(iterator);
121 FLAC__bool import_pic_from(const char *filename, FLAC__StreamMetadata **picture, const char *specification, FLAC__bool *needs_write)
123 const char *error_message;
125 if(0 == specification || strlen(specification) == 0) {
126 flac_fprintf(stderr, "%s: ERROR: empty picture specification\n", filename);
130 *picture = grabbag__picture_parse_specification(specification, &error_message);
133 flac_fprintf(stderr, "%s: ERROR: while parsing picture specification \"%s\": %s\n", filename, specification, error_message);
137 if(!FLAC__format_picture_is_legal(&(*picture)->data.picture, &error_message)) {
138 flac_fprintf(stderr, "%s: ERROR: new PICTURE block for \"%s\" is illegal: %s\n", filename, specification, error_message);
146 FLAC__bool export_pic_to(const char *filename, const FLAC__StreamMetadata *picture, const char *pic_filename)
149 const FLAC__uint32 len = picture->data.picture.data_length;
151 if(0 == pic_filename || strlen(pic_filename) == 0) {
152 flac_fprintf(stderr, "%s: ERROR: empty export file name\n", filename);
155 if(0 == strcmp(pic_filename, "-"))
156 f = grabbag__file_get_binary_stdout();
158 f = flac_fopen(pic_filename, "wb");
161 flac_fprintf(stderr, "%s: ERROR: can't open export file %s: %s\n", filename, pic_filename, strerror(errno));
165 if(fwrite(picture->data.picture.data, 1, len, f) != len) {
166 flac_fprintf(stderr, "%s: ERROR: writing PICTURE data to file\n", filename);