Browse Source

Avoid code duplication

pull/2213/head
Brian Popow 4 years ago
parent
commit
65c3c44ab5
  1. 45
      src/ImageSharp/Metadata/Profiles/IPTC/IptcProfile.cs

45
src/ImageSharp/Metadata/Profiles/IPTC/IptcProfile.cs

@ -256,27 +256,21 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Iptc
if (hasValuesInUtf8) if (hasValuesInUtf8)
{ {
length += 5 + CodedCharacterSetUtf8Value.Length; // Additional length for UTF-8 Tag. // Additional length for UTF-8 Tag.
length += 5 + CodedCharacterSetUtf8Value.Length;
} }
this.Data = new byte[length]; this.Data = new byte[length];
int i = 0; int offset = 0;
if (hasValuesInUtf8) if (hasValuesInUtf8)
{ {
// Envelope Record. // Write Envelope Record.
this.Data[i++] = IptcTagMarkerByte; offset = this.WriteRecord(offset, CodedCharacterSetUtf8Value, IptcRecordNumber.Envelope, IptcEnvelopeCodedCharacterSet);
this.Data[i++] = (byte)IptcRecordNumber.Envelope;
this.Data[i++] = IptcEnvelopeCodedCharacterSet;
this.Data[i++] = (byte)(CodedCharacterSetUtf8Value.Length >> 8);
this.Data[i++] = (byte)CodedCharacterSetUtf8Value.Length;
Buffer.BlockCopy(CodedCharacterSetUtf8Value, 0, this.Data, i, CodedCharacterSetUtf8Value.Length);
i += CodedCharacterSetUtf8Value.Length;
} }
foreach (IptcValue value in this.Values) foreach (IptcValue value in this.Values)
{ {
// Application Record. // Write Application Record.
// +-----------+----------------+---------------------------------------------------------------------------------+ // +-----------+----------------+---------------------------------------------------------------------------------+
// | Octet Pos | Name | Description | // | Octet Pos | Name | Description |
// +==========-+================+=================================================================================+ // +==========-+================+=================================================================================+
@ -293,17 +287,24 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Iptc
// | | Octet Count | the following data field(32767 or fewer octets). Note that the value of bit 7 of| // | | Octet Count | the following data field(32767 or fewer octets). Note that the value of bit 7 of|
// | | | octet 4(most significant bit) always will be 0. | // | | | octet 4(most significant bit) always will be 0. |
// +-----------+----------------+---------------------------------------------------------------------------------+ // +-----------+----------------+---------------------------------------------------------------------------------+
this.Data[i++] = IptcTagMarkerByte; offset = this.WriteRecord(offset, value.ToByteArray(), IptcRecordNumber.Application, (byte)value.Tag);
this.Data[i++] = (byte)IptcRecordNumber.Application; }
this.Data[i++] = (byte)value.Tag; }
this.Data[i++] = (byte)(value.Length >> 8);
this.Data[i++] = (byte)value.Length; private int WriteRecord(int offset, byte[] recordData, IptcRecordNumber recordNumber, byte recordBinaryRepresentation)
if (value.Length > 0) {
{ this.Data[offset++] = IptcTagMarkerByte;
Buffer.BlockCopy(value.ToByteArray(), 0, this.Data, i, value.Length); this.Data[offset++] = (byte)recordNumber;
i += value.Length; this.Data[offset++] = recordBinaryRepresentation;
} this.Data[offset++] = (byte)(recordData.Length >> 8);
this.Data[offset++] = (byte)recordData.Length;
if (recordData.Length > 0)
{
Buffer.BlockCopy(recordData, 0, this.Data, offset, recordData.Length);
offset += recordData.Length;
} }
return offset;
} }
private void Initialize() private void Initialize()

Loading…
Cancel
Save