Hi all, recently I tried to add cover art to flac files and have dfficulties in doing so.<div><br></div><div>FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();</div><div><br></div><div>if(0 == chain)</div><div> return;</div>
<div><br></div><div>if(!FLAC__metadata_chain_read(chain, filename)) </div><div><span class="Apple-tab-span" style="white-space:pre">        </span>return;</div><div><br clear="all"><div>FLAC__StreamMetadata *picture = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PICTURE);</div>
<div>FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();</div><div><br></div><div>if(0 == iterator)</div><div> return;</div><div><br></div><div>FLAC__metadata_iterator_init(iterator, chain);</div><div>
while(FLAC__metadata_iterator_next(iterator))</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>;</div><div><br></div><div><div>const char *violation;</div><div>if( !FLAC__metadata_object_picture_is_legal(picture, &violation)) </div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>printf( "Illegal Picture: %s", violation );</div><div><br></div><div>if(!FLAC__metadata_iterator_insert_block_after(iterator, picture)) </div><div>
{</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>printf(L" ERROR: adding new PICTURE block to metadata");</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>FLAC__metadata_object_delete(picture);</div>
<div>}</div><div><br></div><div>FLAC__metadata_iterator_delete(iterator);</div></div><div><br></div><div><div>FLAC__metadata_chain_sort_padding(chain);</div><div>if(!FLAC__metadata_chain_write(chain, true, false ))</div><div>
{</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>const FLAC__Metadata_ChainStatus status = FLAC__metadata_chain_status(chain);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>printf( "Fail to write: %d", status );</div>
<div>}</div><div><br></div><div>FLAC__metadata_chain_delete(chain);</div><div><br></div><div>The above code will add a NULL cover art into the flac file, it works.</div><div><br></div><div>However, after I add the following code between the call of FLAC__metadata_iterator_next and FLAC__metadata_object_picture_is_legal,</div>
<div>I got a "fail to write: status 0" message.</div><div><br></div><div>picture->data.picture.type = (FLAC__StreamMetadata_Picture_Type)3;</div><div>FLAC__metadata_object_picture_set_mime_type( picture, "image/jpeg", true );</div>
<div>FLAC__metadata_object_picture_set_description( picture, (FLAC__byte*)"", true );</div><div><br></div><div><div>CFile picfile;</div><div>picfile.Open( L"I:\\1.jpg", CFile::modeRead | CFile::shareDenyWrite);</div>
<div>ULONGLONG dwLength = picfile.GetLength();</div><div>LPSTR pData = new char[static_cast<unsigned int>(dwLength+1)];</div><div>picfile.Read(pData, static_cast<UINT>(dwLength));</div><div><br></div><div>FLAC__metadata_object_picture_set_data( picture, (FLAC__byte*)pData, </div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>dwLength, true );</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>delete [] pData;</div><div><br></div><div>I did some tests and it seemed that the error was caused by the FLAC__metadata_object_picture_set_data function,</div>
<div>the members of picture object like width, height, depth, colors were not initialized, are they mandatory or optional?</div><div><br></div><div>What's wrong with the code? </div><div>Please help!</div><div><br></div>
</div></div>-- <br>Best Regards<br>
</div>