private EscherBSERecord CreateRecord() { EscherBSERecord r = new EscherBSERecord(); r.Options=(short)0x0001; r.BlipTypeWin32=EscherBSERecord.BT_JPEG; r.BlipTypeMacOS=EscherBSERecord.BT_JPEG; r.UID=HexRead.ReadFromString("01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 00"); r.Tag=(short)1; r.Ref=2; r.Offset=3; r.Usage=(byte)4; r.Name=(byte)5; r.Unused2=(byte)6; r.Unused3=(byte)7; r.RemainingData=new byte[0]; return r; }
public void TestReadPNG() { //provided in bug-44886 byte[] data = _samples.ReadFile("Container.dat"); EscherContainerRecord record = new EscherContainerRecord(); record.FillFields(data, 0, new DefaultEscherRecordFactory()); EscherContainerRecord bstore = (EscherContainerRecord)record.ChildRecords[1]; EscherBSERecord bse1 = (EscherBSERecord)bstore.ChildRecords[0]; Assert.AreEqual(EscherBSERecord.BT_PNG, bse1.BlipTypeWin32); Assert.AreEqual(EscherBSERecord.BT_PNG, bse1.BlipTypeMacOS); Assert.IsTrue(Arrays.Equals(new byte[]{ 0x65, 0x07, 0x4A, (byte)0x8D, 0x3E, 0x42, (byte)0x8B, (byte)0xAC, 0x1D, (byte)0x89, 0x35, 0x4F, 0x48, (byte)0xFA, 0x37, (byte)0xC2 }, bse1.UID)); Assert.AreEqual(255, bse1.Tag); Assert.AreEqual(32308, bse1.Size); EscherBitmapBlip blip1 = (EscherBitmapBlip)bse1.BlipRecord; Assert.AreEqual(0x6E00, blip1.Options); Assert.AreEqual(EscherBitmapBlip.RECORD_ID_PNG, blip1.RecordId); Assert.IsTrue(Arrays.Equals(new byte[]{ 0x65, 0x07, 0x4A, (byte)0x8D, 0x3E, 0x42, (byte)0x8B, (byte)0xAC, 0x1D, (byte)0x89, 0x35, 0x4F, 0x48, (byte)0xFA, 0x37, (byte)0xC2 }, blip1.UID)); //Serialize and Read again byte[] ser = bse1.Serialize(); EscherBSERecord bse2 = new EscherBSERecord(); bse2.FillFields(ser, 0, new DefaultEscherRecordFactory()); Assert.AreEqual(bse1.RecordId, bse2.RecordId); Assert.AreEqual(bse1.BlipTypeWin32, bse2.BlipTypeWin32); Assert.AreEqual(bse1.BlipTypeMacOS, bse2.BlipTypeMacOS); Assert.IsTrue(Arrays.Equals(bse1.UID, bse2.UID)); Assert.AreEqual(bse1.Tag, bse2.Tag); Assert.AreEqual(bse1.Size, bse2.Size); EscherBitmapBlip blip2 = (EscherBitmapBlip)bse1.BlipRecord; Assert.AreEqual(blip1.Options, blip2.Options); Assert.AreEqual(blip1.RecordId, blip2.RecordId); Assert.AreEqual(blip1.UID, blip2.UID); Assert.IsTrue(Arrays.Equals(blip1.PictureData, blip1.PictureData)); }
public void TestFillFields() { String data = "01 00 00 00 24 00 00 00 05 05 01 02 03 04 " + " 05 06 07 08 09 0A 0B 0C 0D 0E 0F 00 01 00 00 00 " + " 00 00 02 00 00 00 03 00 00 00 04 05 06 07"; EscherBSERecord r = new EscherBSERecord(); int bytesWritten = r.FillFields(HexRead.ReadFromString(data), 0, new DefaultEscherRecordFactory()); Assert.AreEqual(44, bytesWritten); Assert.AreEqual((short)0x0001, r.Options); Assert.AreEqual(EscherBSERecord.BT_JPEG, r.BlipTypeWin32); Assert.AreEqual(EscherBSERecord.BT_JPEG, r.BlipTypeMacOS); Assert.AreEqual("[01, 02, 03, 04, 05, 06, 07, 08, 09, 0A, 0B, 0C, 0D, 0E, 0F, 00, ]", HexDump.ToHex(r.UID)); Assert.AreEqual((short)1, r.Tag); Assert.AreEqual(2, r.Ref); Assert.AreEqual(3, r.Offset); Assert.AreEqual((byte)4, r.Usage); Assert.AreEqual((byte)5, r.Name); Assert.AreEqual((byte)6, r.Unused2); Assert.AreEqual((byte)7, r.Unused3); Assert.AreEqual(0, r.RemainingData.Length); }
public int AddBSERecord(EscherBSERecord e) { CreateDrawingGroup(); // maybe we don't need that as an instance variable anymore escherBSERecords.Add(e); int dgLoc = FindFirstRecordLocBySid(DrawingGroupRecord.sid); DrawingGroupRecord drawingGroup = (DrawingGroupRecord)Records[dgLoc]; EscherContainerRecord dggContainer = (EscherContainerRecord)drawingGroup.GetEscherRecord(0); EscherContainerRecord bstoreContainer; if (dggContainer.GetChild(1).RecordId == EscherContainerRecord.BSTORE_CONTAINER) { bstoreContainer = (EscherContainerRecord)dggContainer.GetChild(1); } else { bstoreContainer = new EscherContainerRecord(); bstoreContainer.RecordId=EscherContainerRecord.BSTORE_CONTAINER; //dggContainer.ChildRecords.Insert(1, bstoreContainer); List<EscherRecord> childRecords = dggContainer.ChildRecords; childRecords.Insert(1, bstoreContainer); dggContainer.ChildRecords = (childRecords); } bstoreContainer.Options=(short)((escherBSERecords.Count << 4) | 0xF); bstoreContainer.AddChildRecord(e); return escherBSERecords.Count; }
/// <summary> /// Adds a picture to the workbook. /// </summary> /// <param name="pictureData">The bytes of the picture</param> /// <param name="format">The format of the picture. One of /// PictureType.</param> /// <returns>the index to this picture (1 based).</returns> public int AddPicture(byte[] pictureData, NPOI.SS.UserModel.PictureType format) { InitDrawings(); byte[] uid; using (MD5 md5 = MD5.Create()) { uid = md5.ComputeHash(pictureData); } EscherBitmapBlip blipRecord = new EscherBitmapBlip(); blipRecord.RecordId = (short)(EscherBitmapBlip.RECORD_ID_START + format); switch (format) { case NPOI.SS.UserModel.PictureType.EMF: blipRecord.Options = HSSFPictureData.MSOBI_EMF; break; case NPOI.SS.UserModel.PictureType.WMF: blipRecord.Options = HSSFPictureData.MSOBI_WMF; break; case NPOI.SS.UserModel.PictureType.PICT: blipRecord.Options = HSSFPictureData.MSOBI_PICT; break; case NPOI.SS.UserModel.PictureType.PNG: blipRecord.Options = HSSFPictureData.MSOBI_PNG; break; case NPOI.SS.UserModel.PictureType.JPEG: blipRecord.Options = HSSFPictureData.MSOBI_JPEG; break; case NPOI.SS.UserModel.PictureType.DIB: blipRecord.Options = HSSFPictureData.MSOBI_DIB; break; } blipRecord.UID = uid; blipRecord.Marker = (byte)0xFF; blipRecord.PictureData = pictureData; EscherBSERecord r = new EscherBSERecord(); r.RecordId = EscherBSERecord.RECORD_ID; r.Options = (short)(0x0002 | ((int)format << 4)); r.BlipTypeMacOS = (byte)format; r.BlipTypeWin32 = (byte)format; r.UID = uid; r.Tag = (short)0xFF; r.Size = pictureData.Length + 25; r.Ref = 1; r.Offset = 0; r.BlipRecord = blipRecord; return workbook.AddBSERecord(r); }
/// <summary> /// Adds a picture to the workbook. /// </summary> /// <param name="pictureData">The bytes of the picture</param> /// <param name="format">The format of the picture. One of /// PictureType.</param> /// <returns>the index to this picture (1 based).</returns> public int AddPicture(byte[] pictureData, NPOI.SS.UserModel.PictureType format) { InitDrawings(); byte[] uid; using (MD5 hasher = MD5.Create()) { uid = hasher.ComputeHash(pictureData); } EscherBlipRecord blipRecord; int blipSize; short escherTag; switch (format) { case PictureType.WMF: // remove first 22 bytes if file starts with magic bytes D7-CD-C6-9A // see also http://de.wikipedia.org/wiki/Windows_Metafile#Hinweise_zur_WMF-Spezifikation if (LittleEndian.GetInt(pictureData) == unchecked((int)0x9AC6CDD7)) { byte[] picDataNoHeader = new byte[pictureData.Length-22]; Array.Copy(pictureData, 22, picDataNoHeader, 0, pictureData.Length-22); pictureData = picDataNoHeader; } EscherMetafileBlip blipRecordMeta = new EscherMetafileBlip(); blipRecord = blipRecordMeta; blipRecordMeta.UID=(/*setter*/uid); blipRecordMeta.SetPictureData(pictureData); // taken from libre office export, it won't open, if this is left to 0 blipRecordMeta.Filter=(/*setter*/unchecked((byte)-2)); blipSize = blipRecordMeta.CompressedSize + 58; escherTag = 0; break; case PictureType.EMF: blipRecordMeta = new EscherMetafileBlip(); blipRecord = blipRecordMeta; blipRecordMeta.UID=(/*setter*/uid); blipRecordMeta.SetPictureData(pictureData); // taken from libre office export, it won't open, if this is left to 0 blipRecordMeta.Filter=(/*setter*/unchecked((byte)-2)); blipSize = blipRecordMeta.CompressedSize + 58; escherTag = 0; break; default: EscherBitmapBlip blipRecordBitmap = new EscherBitmapBlip(); blipRecord = blipRecordBitmap; blipRecordBitmap.UID=(/*setter*/ uid ); blipRecordBitmap.Marker=(/*setter*/ (byte) 0xFF ); blipRecordBitmap.PictureData=(pictureData); blipSize = pictureData.Length + 25; escherTag = (short) 0xFF; break; } blipRecord.RecordId = (short)(EscherBitmapBlip.RECORD_ID_START + format); switch (format) { case NPOI.SS.UserModel.PictureType.EMF: blipRecord.Options = HSSFPictureData.MSOBI_EMF; break; case NPOI.SS.UserModel.PictureType.WMF: blipRecord.Options = HSSFPictureData.MSOBI_WMF; break; case NPOI.SS.UserModel.PictureType.PICT: blipRecord.Options = HSSFPictureData.MSOBI_PICT; break; case NPOI.SS.UserModel.PictureType.PNG: blipRecord.Options = HSSFPictureData.MSOBI_PNG; break; case NPOI.SS.UserModel.PictureType.JPEG: blipRecord.Options = HSSFPictureData.MSOBI_JPEG; break; case NPOI.SS.UserModel.PictureType.DIB: blipRecord.Options = HSSFPictureData.MSOBI_DIB; break; } EscherBSERecord r = new EscherBSERecord(); r.RecordId = EscherBSERecord.RECORD_ID; r.Options = (short)(0x0002 | ((int)format << 4)); r.BlipTypeMacOS = (byte)format; r.BlipTypeWin32 = (byte)format; r.UID = uid; r.Tag = (short)0xFF; r.Size = pictureData.Length + 25; r.Ref = 0; r.Offset = 0; r.BlipRecord = blipRecord; return workbook.AddBSERecord(r); }
/// <summary> /// Adds a picture to the workbook. /// </summary> /// <param name="pictureData">The bytes of the picture</param> /// <param name="format">The format of the picture. One of /// PICTURE_TYPE_* /// <returns>the index to this picture (1 based).</returns> public int AddPicture(byte[] pictureData, int format) { byte[] uid = NewUID; EscherBitmapBlip blipRecord = new EscherBitmapBlip(); blipRecord.RecordId = (short)(EscherBitmapBlip.RECORD_ID_START + format); switch (format) { case PICTURE_TYPE_EMF: blipRecord.Options = HSSFPictureData.MSOBI_EMF; break; case PICTURE_TYPE_WMF: blipRecord.Options = HSSFPictureData.MSOBI_WMF; break; case PICTURE_TYPE_PICT: blipRecord.Options = HSSFPictureData.MSOBI_PICT; break; case PICTURE_TYPE_PNG: blipRecord.Options = HSSFPictureData.MSOBI_PNG; break; case HSSFWorkbook.PICTURE_TYPE_JPEG: blipRecord.Options = HSSFPictureData.MSOBI_JPEG; break; case HSSFWorkbook.PICTURE_TYPE_DIB: blipRecord.Options = HSSFPictureData.MSOBI_DIB; break; } blipRecord.UID = uid; blipRecord.Marker = (byte)0xFF; blipRecord.PictureData = pictureData; EscherBSERecord r = new EscherBSERecord(); r.RecordId = EscherBSERecord.RECORD_ID; r.Options = (short)(0x0002 | (format << 4)); r.BlipTypeMacOS = (byte)format; r.BlipTypeWin32 = (byte)format; r.UID = uid; r.Tag = (short)0xFF; r.Size = pictureData.Length + 25; r.Ref = 1; r.Offset = 0; r.BlipRecord = blipRecord; return workbook.AddBSERecord(r); }
public void TestReadPICT() { //provided in bug-44886 byte[] data = _samples.ReadFile("Container.dat"); EscherContainerRecord record = new EscherContainerRecord(); record.FillFields(data, 0, new DefaultEscherRecordFactory()); EscherContainerRecord bstore = (EscherContainerRecord)record.ChildRecords[1]; EscherBSERecord bse1 = (EscherBSERecord)bstore.ChildRecords[1]; //System.out.println(bse1); Assert.AreEqual(EscherBSERecord.BT_WMF, bse1.BlipTypeWin32); Assert.AreEqual(EscherBSERecord.BT_PICT, bse1.BlipTypeMacOS); Assert.IsTrue(Arrays.Equals(new byte[]{ (byte)0xC7, 0x15, 0x69, 0x2D, (byte)0xE5, (byte)0x89, (byte)0xA3, 0x6F, 0x66, 0x03, (byte)0xD6, 0x24, (byte)0xF7, (byte)0xDB, 0x1D, 0x13 }, bse1.UID)); Assert.AreEqual(255, bse1.Tag); Assert.AreEqual(1133, bse1.Size); EscherMetafileBlip blip1 = (EscherMetafileBlip)bse1.BlipRecord; Assert.AreEqual(0x5430, blip1.Options); Assert.AreEqual(EscherMetafileBlip.RECORD_ID_PICT, blip1.RecordId); Assert.IsTrue(Arrays.Equals(new byte[]{ 0x57, 0x32, 0x7B, (byte)0x91, 0x23, 0x5D, (byte)0xDB, 0x36, 0x7A, (byte)0xDB, (byte)0xFF, 0x17, (byte)0xFE, (byte)0xF3, (byte)0xA7, 0x05 }, blip1.UID)); Assert.IsTrue(Arrays.Equals(new byte[]{ (byte)0xC7, 0x15, 0x69, 0x2D, (byte)0xE5, (byte)0x89, (byte)0xA3, 0x6F, 0x66, 0x03, (byte)0xD6, 0x24, (byte)0xF7, (byte)0xDB, 0x1D, 0x13 }, blip1.PrimaryUID)); //Serialize and Read again byte[] ser = bse1.Serialize(); EscherBSERecord bse2 = new EscherBSERecord(); bse2.FillFields(ser, 0, new DefaultEscherRecordFactory()); Assert.AreEqual(bse1.RecordId, bse2.RecordId); Assert.AreEqual(bse1.Options, bse2.Options); Assert.AreEqual(bse1.BlipTypeWin32, bse2.BlipTypeWin32); Assert.AreEqual(bse1.BlipTypeMacOS, bse2.BlipTypeMacOS); Assert.IsTrue(Arrays.Equals(bse1.UID, bse2.UID)); Assert.AreEqual(bse1.Tag, bse2.Tag); Assert.AreEqual(bse1.Size, bse2.Size); EscherMetafileBlip blip2 = (EscherMetafileBlip)bse1.BlipRecord; Assert.AreEqual(blip1.Options, blip2.Options); Assert.AreEqual(blip1.RecordId, blip2.RecordId); Assert.AreEqual(blip1.UID, blip2.UID); Assert.AreEqual(blip1.PrimaryUID, blip2.PrimaryUID); Assert.IsTrue(Arrays.Equals(blip1.PictureData, blip1.PictureData)); }
public void Test47143() { byte[] data = _samples.ReadFile("47143.dat"); EscherBSERecord bse = new EscherBSERecord(); bse.FillFields(data, 0, new DefaultEscherRecordFactory()); bse.ToString(); //assert that toString() works Assert.IsTrue(bse.BlipRecord is EscherMetafileBlip); EscherMetafileBlip blip = (EscherMetafileBlip)bse.BlipRecord; blip.ToString(); //assert that toString() works byte[] remaining = blip.RemainingData; Assert.IsNotNull(remaining); byte[] ser = bse.Serialize(); //serialize and assert against the source data Assert.IsTrue(Arrays.Equals(data, ser)); }