add --export-picture-to option to metaflac
diff --git a/doc/html/changelog.html b/doc/html/changelog.html
index 3aedd12..9551127 100644
--- a/doc/html/changelog.html
+++ b/doc/html/changelog.html
@@ -70,7 +70,7 @@
 					<li>Better multichannel support</li>
 					<li>Large file (&gt;2GB) support everywhere</li>
 					<li><span class="commandname">flac</span> now supports FLAC as input to the encoder (i.e. can re-encode FLAC to FLAC) and preserve all the metadata like tags, etc.</li>
-					<li>New <a href="format.html#def_PICTURE"><span class="code">PICTURE</span></a> metadata block for storing things like cover art, new <span class="argument"><a href="documentation.html#flac_options_picture">--picture</a></span> option to <span class="commandname">flac</span> and <span class="argument"><a href="documentation.html#metaflac_shorthand_import_picture">--import-picture</a></span> option to <span class="commandname">metaflac</span> for importing pictures, and metadata API <a href="api/group__flac__metadata__level0.html#ga3">additions</a> for searching for suitable pictures based on type, size and color constraints.</li>
+					<li>New <a href="format.html#def_PICTURE"><span class="code">PICTURE</span></a> metadata block for storing things like cover art, new <span class="argument"><a href="documentation.html#flac_options_picture">--picture</a></span> option to <span class="commandname">flac</span> and <span class="argument"><a href="documentation.html#metaflac_shorthand_import_picture">--import-picture</a></span> option to <span class="commandname">metaflac</span> for importing pictures, new <a href="documentation.html#metaflac_shorthand_export_picture_to">--export-picture-to</a></span> option to <span class="commandname">metaflac</span> for exporting pictures, and metadata API <a href="api/group__flac__metadata__level0.html#ga3">additions</a> for searching for suitable pictures based on type, size and color constraints.</li>
 					<li>Support for new <tt>REPLAYGAIN_REFERENCE_LOUDNESS</tt> tag.</li>
 					<li>In the developer libraries, the interface has been simplfied by merging the three decoding layers into a single class; ditto for the encoders.  Also, libOggFLAC has been merged into libFLAC and libOggFLAC++ has been merged into libFLAC++ so there is a single API supporting both native FLAC and Ogg FLAC.</li>
 				</ul>
@@ -116,6 +116,7 @@
 				<ul>
 					<li>Added a new option <a href="documentation.html#metaflac_shorthand_set_tag_from_file"><span class="argument">--set-tag-from-file</span></a> for setting a tag from file (e.g. for importing a cuesheet as a tag).</li>
 					<li>Added a new option <span class="argument"><a href="documentation.html#metaflac_shorthand_import_picture">--import-picture</a></span> for importing pictures.</li>
+					<li>Added a new option <span class="argument"><a href="documentation.html#metaflac_shorthand_export_picture_to">--export-picture-to</a></span> for exporting pictures.</li>
 					<li>Added shorthand operation <a href="documentation.html#metaflac_shorthand_remove_replay_gain"><span class="argument">--remove-replay-gain</span></a> for removing ReplayGain tags.</li>
 					<li><a href="documentation.html#metaflac_shorthand_export_cuesheet_to"><span class="argument">--export-cuesheet-to</span></a> now properly specifies the FLAC file name (<a href="https://sourceforge.net/tracker/index.php?func=detail&amp;aid=1272825&amp;group_id=13478&amp;atid=363478">SF #1272825</a>).</li>
 					<li>Importing of non-CDDA-compliant cuesheets now issues a warning.</li>
diff --git a/doc/html/documentation.html b/doc/html/documentation.html
index 52dc5c2..1d6ef23 100644
--- a/doc/html/documentation.html
+++ b/doc/html/documentation.html
@@ -1201,7 +1201,7 @@
 					<span class="argument">--import-tags-from=FILE</span>
 				</td>
 				<td>
-					Import tags from a file.  Use '-' for stdin.  Each line should be of the form <span class="argument">NAME=VALUE</span>.  Multi-line comments are currently not supported.  Specify <span class="argument">--remove-all-tags</span> and/or <a href="#metaflac_options_no_utf8_convert"><span class="argument">--no-utf8-convert</span></a> before <span class="argument">--import-tags-from</span> if necessary.
+					Import tags from a file.  Use <span class="argument">-</span> for stdin.  Each line should be of the form <span class="argument">NAME=VALUE</span>.  Multi-line comments are currently not supported.  Specify <span class="argument">--remove-all-tags</span> and/or <a href="#metaflac_options_no_utf8_convert"><span class="argument">--no-utf8-convert</span></a> before <span class="argument">--import-tags-from</span> if necessary.
 				</td>
 			</tr>
 			<tr>
@@ -1210,7 +1210,7 @@
 					<span class="argument">--export-tags-to=FILE</span>
 				</td>
 				<td>
-					Export tags to a file.  Use '-' for stdin.  Each line will be of the form <span class="argument">NAME=VALUE</span>.  Specify <a href="#metaflac_options_no_utf8_convert"><span class="argument">--no-utf8-convert</span></a> if necessary.
+					Export tags to a file.  Use <span class="argument">-</span> for stdin.  Each line will be of the form <span class="argument">NAME=VALUE</span>.  Specify <a href="#metaflac_options_no_utf8_convert"><span class="argument">--no-utf8-convert</span></a> if necessary.
 				</td>
 			</tr>
 			<tr>
@@ -1219,7 +1219,7 @@
 					<span class="argument">--import-cuesheet-from=FILE</span>
 				</td>
 				<td>
-					Import a cuesheet from a file.  Use '-' for stdin.  Only one FLAC file may be specified.  A seekpoint will be added for each index point in the cuesheet to the <span class="code">SEEKTABLE</span> unless <span class="argument">--no-cued-seekpoints</span> is specified.
+					Import a cuesheet from a file.  Use <span class="argument">-</span> for stdin.  Only one FLAC file may be specified.  A seekpoint will be added for each index point in the cuesheet to the <span class="code">SEEKTABLE</span> unless <span class="argument">--no-cued-seekpoints</span> is specified.
 				</td>
 			</tr>
 			<tr>
@@ -1228,7 +1228,7 @@
 					<span class="argument">--export-cuesheet-to=FILE</span>
 				</td>
 				<td>
-					Export <span class="code">CUESHEET</span> block to a cuesheet file, suitable for use by CD authoring software.  Use '-' for stdout.  Only one FLAC file may be specified on the command line.
+					Export <span class="code">CUESHEET</span> block to a cuesheet file, suitable for use by CD authoring software.  Use <span class="argument">-</span> for stdout.  Only one FLAC file may be specified on the command line.
 				</td>
 			</tr>
 			<tr>
@@ -1242,6 +1242,15 @@
 			</tr>
 			<tr>
 				<td nowrap="nowrap" align="right" valign="top" bgcolor="#F4F4CC">
+					<a name="metaflac_shorthand_export_picture_to" />
+					<span class="argument">--export-picture-to=FILE</span>
+				</td>
+				<td>
+					Export <span class="code">PICTURE</span> block to a file.  Use <span class="argument">-</span> for stdout.  Only one FLAC file may be specified on the command line.  The first <span class="code">PICTURE</span> block will be exported unless <span class="argument">--export-picture-to</span> is preceded by a <span class="argument">--block-number=#</span> option to specify the exact metadata block to extract.  Note that the block number is the one shown by --list.
+				</td>
+			</tr>
+			<tr>
+				<td nowrap="nowrap" align="right" valign="top" bgcolor="#F4F4CC">
 					<a name="metaflac_shorthand_add_replay_gain" />
 					<span class="argument">--add-replay-gain</span>
 				</td>
diff --git a/man/metaflac.sgml b/man/metaflac.sgml
index 480ede7..2fbca4b 100644
--- a/man/metaflac.sgml
+++ b/man/metaflac.sgml
@@ -343,6 +343,14 @@
 	</listitem>
       </varlistentry>
       <varlistentry>
+	<term><option>--export-picture-to=file</option></term>
+        <listitem>
+	  <para>
+	    Export PICTURE block to a file.  Use '-' for stdout.  Only one FLAC file may be specified on the command line.  The first PICTURE block will be exported unless --export-picture-to is preceded by a --block-number=# option to specify the exact metadata block to extract.  Note that the block number is the one shown by --list.
+	  </para>
+        </listitem>
+      </varlistentry>
+      <varlistentry>
 	<term><option>--add-replay-gain</option></term>
         <listitem>
 	  <para>
diff --git a/src/metaflac/operations.c b/src/metaflac/operations.c
index de13383..764f47a 100644
--- a/src/metaflac/operations.c
+++ b/src/metaflac/operations.c
@@ -360,6 +360,7 @@
 			ok = do_shorthand_operation__cuesheet(filename, chain, operation, needs_write);
 			break;
 		case OP__IMPORT_PICTURE:
+		case OP__EXPORT_PICTURE_TO:
 			ok = do_shorthand_operation__picture(filename, chain, operation, needs_write);
 			break;
 		case OP__ADD_SEEKPOINT:
diff --git a/src/metaflac/operations_shorthand_picture.c b/src/metaflac/operations_shorthand_picture.c
index aefc187..8685cf5 100644
--- a/src/metaflac/operations_shorthand_picture.c
+++ b/src/metaflac/operations_shorthand_picture.c
@@ -28,6 +28,7 @@
 #include "share/grabbag.h" /* for grabbag__picture_parse_specification */
 
 static FLAC__bool import_pic_from(const char *filename, FLAC__StreamMetadata **picture, const char *specification, FLAC__bool *needs_write);
+static FLAC__bool export_pic_to(const char *filename, const FLAC__StreamMetadata *picture, const char *pic_filename);
 
 FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write)
 {
@@ -53,6 +54,52 @@
 					ok = false;
 				}
 			}
+			if(ok) {
+				/* check global PICTURE constraints (max 1 block each of type=1 and type=2) */
+				while(FLAC__metadata_iterator_prev(iterator))
+					;
+				do {
+					FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
+					if(block->type == FLAC__METADATA_TYPE_PICTURE) {
+						if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
+							if(has_type1) {
+								print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one 32x32 standard icon (type=1) PICTURE block", filename);
+								ok = false;
+							}
+							has_type1 = true;
+						}
+						else if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
+							if(has_type2) {
+								print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one icon (type=2) PICTURE block", filename);
+								ok = false;
+							}
+							has_type2 = true;
+						}
+					}
+				} while(FLAC__metadata_iterator_next(iterator));
+			}
+			break;
+		case OP__EXPORT_PICTURE_TO:
+			{
+				const Argument_BlockNumber *a = operation->argument.export_picture_to.block_number_link;
+				int block_number = (a && a->num_entries > 0)? (int)a->entries[0] : -1;
+				unsigned i = 0;
+				do {
+					FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
+					if(block->type == FLAC__METADATA_TYPE_PICTURE && (block_number < 0 || i == (unsigned)block_number))
+						picture = block;
+					i++;
+				} while(FLAC__metadata_iterator_next(iterator) && 0 == picture);
+				if(0 == picture) {
+					if(block_number < 0)
+						fprintf(stderr, "%s: ERROR: FLAC file has no PICTURE block\n", filename);
+					else
+						fprintf(stderr, "%s: ERROR: FLAC file has no PICTURE block at block #%d\n", filename, block_number);
+					ok = false;
+				}
+				else
+					ok = export_pic_to(filename, picture, operation->argument.filename.value);
+			}
 			break;
 		default:
 			ok = false;
@@ -60,30 +107,6 @@
 			break;
 	};
 
-	/* check global PICTURE constraints (max 1 block each of type=1 and type=2) */
-	while(FLAC__metadata_iterator_prev(iterator))
-		;
-	do {
-		FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
-		if(block->type == FLAC__METADATA_TYPE_PICTURE) {
-			if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
-				if(has_type1) {
-					print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one 32x32 standard icon (type=1) PICTURE block", filename);
-					ok = false;
-				}
-				has_type1 = true;
-			}
-			else if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
-				if(has_type2) {
-					print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one icon (type=2) PICTURE block", filename);
-					ok = false;
-				}
-				has_type2 = true;
-			}
-		}
-	} while(FLAC__metadata_iterator_next(iterator));
-
-
 	FLAC__metadata_iterator_delete(iterator);
 	return ok;
 }
@@ -116,3 +139,33 @@
 	*needs_write = true;
 	return true;
 }
+
+FLAC__bool export_pic_to(const char *filename, const FLAC__StreamMetadata *picture, const char *pic_filename)
+{
+	FILE *f;
+	const FLAC__uint32 len = picture->data.picture.data_length;
+
+	if(0 == pic_filename || strlen(pic_filename) == 0) {
+		fprintf(stderr, "%s: ERROR: empty export file name\n", filename);
+		return false;
+	}
+	if(0 == strcmp(pic_filename, "-"))
+		f = stdout;
+	else
+		f = fopen(pic_filename, "w");
+
+	if(0 == f) {
+		fprintf(stderr, "%s: ERROR: can't open export file %s: %s\n", filename, pic_filename, strerror(errno));
+		return false;
+	}
+
+	if(fwrite(picture->data.picture.data, 1, len, f) != len) {
+		fprintf(stderr, "%s: ERROR: writing PICTURE data to file\n", filename);
+		return false;
+	}
+
+	if(f != stdout)
+		fclose(f);
+
+	return true;
+}
diff --git a/src/metaflac/options.c b/src/metaflac/options.c
index 6d42599..86b217c 100644
--- a/src/metaflac/options.c
+++ b/src/metaflac/options.c
@@ -73,6 +73,7 @@
 	{ "import-cuesheet-from", 1, 0, 0 },
 	{ "export-cuesheet-to", 1, 0, 0 },
 	{ "import-picture", 1, 0, 0 },
+	{ "export-picture-to", 1, 0, 0 },
 	{ "add-seekpoint", 1, 0, 0 },
 	{ "add-replay-gain", 0, 0, 0 },
 	{ "remove-replay-gain", 0, 0, 0 },
@@ -101,6 +102,7 @@
 static void append_new_argument(CommandLineOptions *options, Argument argument);
 static Operation *append_major_operation(CommandLineOptions *options, OperationType type);
 static Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type);
+static Argument *find_argument(CommandLineOptions *options, ArgumentType type);
 static Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type);
 static Argument *append_argument(CommandLineOptions *options, ArgumentType type);
 static FLAC__bool parse_md5(const char *src, FLAC__byte dest[16]);
@@ -201,8 +203,14 @@
 	}
 
 	/* check for only one FLAC file used with --import-cuesheet-from/--export-cuesheet-to */
-	if((0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM) || 0 != find_shorthand_operation(options, OP__EXPORT_CUESHEET_TO)) && options->num_files > 1) {
-		fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-cuesheet-from' or '--export-cuesheet-to'\n");
+	if(
+		(
+			0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM) ||
+			0 != find_shorthand_operation(options, OP__EXPORT_CUESHEET_TO) ||
+			0 != find_shorthand_operation(options, OP__EXPORT_PICTURE_TO)
+		) && options->num_files > 1
+	) {
+		fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-cuesheet-from', '--export-cuesheet-to' or '--export-picture-to'\n");
 		had_error = true;
 	}
 
@@ -271,6 +279,10 @@
 				if(0 != op->argument.specification.value)
 					free(op->argument.specification.value);
 				break;
+			case OP__EXPORT_PICTURE_TO:
+				if(0 != op->argument.export_picture_to.filename)
+					free(op->argument.export_picture_to.filename);
+				break;
 			case OP__ADD_SEEKPOINT:
 				if(0 != op->argument.add_seekpoint.specification)
 					free(op->argument.add_seekpoint.specification);
@@ -555,6 +567,16 @@
 			ok = false;
 		}
 	}
+	else if(0 == strcmp(opt, "export-picture-to")) {
+		const Argument *arg = find_argument(options, ARG__BLOCK_NUMBER);
+		op = append_shorthand_operation(options, OP__EXPORT_PICTURE_TO);
+		FLAC__ASSERT(0 != option_argument);
+		if(!parse_string(option_argument, &(op->argument.export_picture_to.filename))) {
+			fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
+			ok = false;
+		}
+		op->argument.export_picture_to.block_number_link = arg? &(arg->value.block_number) : 0;
+	}
 	else if(0 == strcmp(opt, "add-seekpoint")) {
 		const char *violation;
 		char *spec;
@@ -733,6 +755,15 @@
 	return options->ops.operations + (options->ops.num_operations - 1);
 }
 
+Argument *find_argument(CommandLineOptions *options, ArgumentType type)
+{
+	unsigned i;
+	for(i = 0; i < options->args.num_arguments; i++)
+		if(options->args.arguments[i].type == type)
+			return &options->args.arguments[i];
+	return 0;
+}
+
 Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type)
 {
 	unsigned i;
diff --git a/src/metaflac/options.h b/src/metaflac/options.h
index a410e62..47bc0dd 100644
--- a/src/metaflac/options.h
+++ b/src/metaflac/options.h
@@ -61,6 +61,7 @@
 	OP__IMPORT_CUESHEET_FROM,
 	OP__EXPORT_CUESHEET_TO,
 	OP__IMPORT_PICTURE,
+	OP__EXPORT_PICTURE_TO,
 	OP__ADD_SEEKPOINT,
 	OP__ADD_REPLAY_GAIN,
 	OP__ADD_PADDING,
@@ -143,6 +144,11 @@
 } Argument_ImportCuesheetFrom;
 
 typedef struct {
+	char *filename;
+	const Argument_BlockNumber *block_number_link; /* may be NULL to mean 'first PICTURE block' */
+} Argument_ExportPictureTo;
+
+typedef struct {
 	unsigned length;
 } Argument_AddPadding;
 
@@ -157,6 +163,7 @@
 		Argument_String filename;
 		Argument_String specification;
 		Argument_ImportCuesheetFrom import_cuesheet_from;
+		Argument_ExportPictureTo export_picture_to;
 		Argument_AddSeekpoint add_seekpoint;
 		Argument_AddPadding add_padding;
 	} argument;
diff --git a/src/metaflac/usage.c b/src/metaflac/usage.c
index 3969422..ef83448 100644
--- a/src/metaflac/usage.c
+++ b/src/metaflac/usage.c
@@ -191,6 +191,12 @@
 	fprintf(out, "             you should also specify the number of colors used.\n");
 	fprintf(out, "           FILE is the path to the picture file to be imported, or the URL if\n");
 	fprintf(out, "             MIME type is -->\n");
+	fprintf(out, "--export-picture-to=FILE  Export PICTURE block to a file.  Use '-' for stdout.\n");
+	fprintf(out, "                      Only one FLAC file may be specified.  The first PICTURE\n");
+	fprintf(out, "                      block will be exported unless --export-picture-to is\n");
+	fprintf(out, "                      preceded by a --block-number=# option to specify the exact\n");
+	fprintf(out, "                      metadata block to extract.  Note that the block number is\n");
+	fprintf(out, "                      the one shown by --list.\n");
 	fprintf(out, "--add-replay-gain     Calculates the title and album gains/peaks of the given\n");
 	fprintf(out, "                      FLAC files as if all the files were part of one album,\n");
 	fprintf(out, "                      then stores them in the VORBIS_COMMENT block.  The tags\n");
diff --git a/test/test_metaflac.sh b/test/test_metaflac.sh
index 592846f..050d155 100755
--- a/test/test_metaflac.sh
+++ b/test/test_metaflac.sh
@@ -351,6 +351,21 @@
 	ncase=`expr $ncase + 1`
 done
 [ $ncase = 60 ] || die "expected case# to be 60"
+
+fn=export-picture-check
+echo -n "Testing --export-picture-to... "
+run_metaflac --export-picture-to=$fn $flacfile
+check_flac
+cmp $fn pictures/0.gif || die "ERROR, exported picture file and original differ"
+echo OK
+rm -f $fn
+echo -n "Testing --block-number --export-picture-to... "
+run_metaflac --block-number=9 --export-picture-to=$fn $flacfile
+check_flac
+cmp $fn pictures/0.png || die "ERROR, exported picture file and original differ"
+echo OK
+rm -f $fn
+
 run_metaflac --remove --block-type=PICTURE $flacfile
 check_flac
 metaflac_test case60 "--remove --block-type=PICTURE" "--list"