Example #1
0
        public void TestToString()
        {
            EscherBSERecord record = CreateRecord();
            String          nl     = Environment.NewLine;

            Assert.AreEqual("EscherBSERecord:" + nl +
                            "  RecordId: 0xF007" + nl +
                            "  Version: 0x0001" + '\n' +
                            "  Instance: 0x0000" + '\n' +
                            "  BlipTypeWin32: 5" + nl +
                            "  BlipTypeMacOS: 5" + nl +
                            "  SUID: [01, 02, 03, 04, 05, 06, 07, 08, 09, 0A, 0B, 0C, 0D, 0E, 0F, 00, ]" + nl +
                            "  Tag: 1" + nl +
                            "  Size: 0" + nl +
                            "  Ref: 2" + nl +
                            "  Offset: 3" + nl +
                            "  Usage: 4" + nl +
                            "  Name: 5" + nl +
                            "  Unused2: 6" + nl +
                            "  Unused3: 7" + nl +
                            "  blipRecord: null" + nl +
                            "  Extra Data:" + nl +
                            "No Data" + nl
                            , record.ToString());
        }
Example #2
0
        public void TestClonePictures()
        {
            IWorkbook        wb  = HSSFTestDataSamples.OpenSampleWorkbook("SimpleWithImages.xls");
            InternalWorkbook iwb = ((HSSFWorkbook)wb).Workbook;

            iwb.FindDrawingGroup();

            for (int pictureIndex = 1; pictureIndex <= 4; pictureIndex++)
            {
                EscherBSERecord bse = iwb.GetBSERecord(pictureIndex);
                Assert.AreEqual(1, bse.Ref);
            }

            wb.CloneSheet(0);
            for (int pictureIndex = 1; pictureIndex <= 4; pictureIndex++)
            {
                EscherBSERecord bse = iwb.GetBSERecord(pictureIndex);
                Assert.AreEqual(2, bse.Ref);
            }

            wb.CloneSheet(0);
            for (int pictureIndex = 1; pictureIndex <= 4; pictureIndex++)
            {
                EscherBSERecord bse = iwb.GetBSERecord(pictureIndex);
                Assert.AreEqual(3, bse.Ref);
            }
        }
Example #3
0
        /**
         * Performs a recursive search for pictures in the given list of escher records.
         *
         * @param escherRecords the escher records.
         * @param pictures the list to populate with the pictures.
         */
        private void SearchForPictures(IList escherRecords, List <Picture> pictures)
        {
            foreach (EscherRecord escherRecord in escherRecords)
            {
                if (escherRecord is EscherBSERecord)
                {
                    EscherBSERecord  bse  = (EscherBSERecord)escherRecord;
                    EscherBlipRecord blip = bse.BlipRecord;
                    if (blip != null)
                    {
                        pictures.Add(new Picture(blip.PictureData));
                    }
                    else if (bse.Offset > 0)
                    {
                        // Blip stored in delay stream, which in a word doc, is the main stream
                        EscherRecordFactory recordFactory = new DefaultEscherRecordFactory();
                        EscherRecord        record        = recordFactory.CreateRecord(_mainStream, bse.Offset);

                        if (record is EscherBlipRecord)
                        {
                            record.FillFields(_mainStream, bse.Offset, recordFactory);
                            blip = (EscherBlipRecord)record;
                            pictures.Add(new Picture(blip.PictureData));
                        }
                    }
                }

                // Recursive call.
                SearchForPictures(escherRecord.ChildRecords, pictures);
            }
        }
Example #4
0
        public void TestBSEPictureRef()
        {
            HSSFWorkbook wb = new HSSFWorkbook();

            HSSFSheet        sh     = wb.CreateSheet("Pictures") as HSSFSheet;
            HSSFPatriarch    dr     = sh.CreateDrawingPatriarch() as HSSFPatriarch;
            HSSFClientAnchor anchor = new HSSFClientAnchor();

            InternalSheet ish = HSSFTestHelper.GetSheetForTest(sh);

            //register a picture
            byte[] data1 = new byte[] { 1, 2, 3 };
            int    idx1  = wb.AddPicture(data1, PictureType.JPEG);

            Assert.AreEqual(1, idx1);
            HSSFPicture p1 = dr.CreatePicture(anchor, idx1) as HSSFPicture;

            EscherBSERecord bse = wb.Workbook.GetBSERecord(idx1);

            Assert.AreEqual(bse.Ref, 1);
            dr.CreatePicture(new HSSFClientAnchor(), idx1);
            Assert.AreEqual(bse.Ref, 2);

            HSSFShapeGroup gr = dr.CreateGroup(new HSSFClientAnchor());

            gr.CreatePicture(new HSSFChildAnchor(), idx1);
            Assert.AreEqual(bse.Ref, 3);
        }
Example #5
0
        public void SetBackgroundImage(int pictureIndex)
        {
            SetPropertyValue(new EscherSimpleProperty(EscherProperties.FILL__PATTERNTEXTURE, false, true, pictureIndex));
            SetPropertyValue(new EscherSimpleProperty(EscherProperties.FILL__FILLTYPE, false, false, FILL_TYPE_PICTURE));
            EscherBSERecord bse = ((HSSFWorkbook)((HSSFPatriarch)Patriarch).Sheet.Workbook).Workbook.GetBSERecord(pictureIndex);

            bse.Ref = (bse.Ref + 1);
        }
Example #6
0
        private EscherBlipRecord GetBitmapRecord(int bitmapIndex)
        {
            List <EscherContainerRecord> bContainers = _escherRecordHolder
                                                       .GetBStoreContainers();

            if (bContainers == null || bContainers.Count != 1)
            {
                return(null);
            }

            EscherContainerRecord bContainer = bContainers[0];
            IList bitmapRecords = bContainer.ChildRecords;

            if (bitmapRecords.Count < bitmapIndex)
            {
                return(null);
            }

            EscherRecord imageRecord = (EscherRecord)bitmapRecords[bitmapIndex - 1];

            if (imageRecord is EscherBlipRecord)
            {
                return((EscherBlipRecord)imageRecord);
            }

            if (imageRecord is EscherBSERecord)
            {
                EscherBSERecord bseRecord = (EscherBSERecord)imageRecord;

                EscherBlipRecord blip = bseRecord.BlipRecord;
                if (blip != null)
                {
                    return(blip);
                }

                if (bseRecord.Offset > 0)
                {
                    /*
                     * Blip stored in delay stream, which in a word doc, is the main
                     * stream
                     */
                    EscherRecordFactory recordFactory = new DefaultEscherRecordFactory();
                    EscherRecord        record        = recordFactory.CreateRecord(_mainStream,
                                                                                   bseRecord.Offset);

                    if (record is EscherBlipRecord)
                    {
                        record.FillFields(_mainStream, bseRecord.Offset,
                                          recordFactory);
                        return((EscherBlipRecord)record);
                    }
                }
            }

            return(null);
        }
Example #7
0
        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));
        }
Example #8
0
        internal override void AfterInsert(HSSFPatriarch patriarch)
        {
            EscherAggregate agg = patriarch.GetBoundAggregate();

            agg.AssociateShapeToObjRecord(GetEscherContainer().GetChildById(EscherClientDataRecord.RECORD_ID), GetObjRecord());
            EscherBSERecord bse =
                (patriarch.Sheet.Workbook as HSSFWorkbook).Workbook.GetBSERecord(PictureIndex);

            bse.Ref = (bse.Ref + 1);
        }
Example #9
0
        public void ResetBackgroundImage()
        {
            EscherSimpleProperty property = (EscherSimpleProperty)GetOptRecord().Lookup(EscherProperties.FILL__PATTERNTEXTURE);

            if (null != property)
            {
                EscherBSERecord bse = ((HSSFWorkbook)((HSSFPatriarch)Patriarch).Sheet.Workbook).Workbook.GetBSERecord(property.PropertyValue);
                bse.Ref = (bse.Ref - 1);
                GetOptRecord().RemoveEscherProperty(EscherProperties.FILL__PATTERNTEXTURE);
            }
            SetPropertyValue(new EscherSimpleProperty(EscherProperties.FILL__FILLTYPE, false, false, FILL_TYPE_SOLID));
        }
Example #10
0
        public void TestSerialize()
        {
            EscherBSERecord r = CreateRecord();

            byte[] data         = new byte[8 + 36];
            int    bytesWritten = r.Serialize(0, data);

            Assert.AreEqual(44, bytesWritten);
            Assert.AreEqual("[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, ]",
                            HexDump.ToHex(data));
        }
Example #11
0
        /// <summary>
        /// Creates a picture.
        /// </summary>
        /// <param name="anchor">the client anchor describes how this Group is attached
        /// to the sheet.</param>
        /// <param name="pictureIndex">Index of the picture.</param>
        /// <returns>the newly created shape.</returns>
        public IPicture CreatePicture(HSSFClientAnchor anchor, int pictureIndex)
        {
            HSSFPicture shape = new HSSFPicture(null, (HSSFClientAnchor)anchor);

            shape.PictureIndex = pictureIndex;
            shape.Anchor       = (HSSFClientAnchor)anchor;
            AddShape(shape);

            EscherBSERecord bse = (_sheet.Workbook as HSSFWorkbook).Workbook.GetBSERecord(pictureIndex);

            bse.Ref = (bse.Ref + 1);
            return(shape);
        }
Example #12
0
        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));
        }
Example #13
0
        /// <summary>
        /// Return the dimension of this image
        /// </summary>
        /// <returns>image dimension</returns>
        public Size GetImageDimension()
        {
            EscherBSERecord bse = (_patriarch.Sheet.Workbook as HSSFWorkbook).Workbook.GetBSERecord(PictureIndex);

            byte[] data = bse.BlipRecord.PictureData;
            //int type = bse.BlipTypeWin32;

            using (MemoryStream ms = new MemoryStream(data))
            {
                using (Image img = Image.FromStream(ms))
                {
                    return(img.Size);
                }
            }
        }
Example #14
0
        /// <summary>
        /// Return the dimension of the embedded image in pixel
        /// </summary>
        /// <returns>image dimension</returns>
        public Size GetImageDimension()
        {
            InternalWorkbook iwb = (_patriarch.Sheet.Workbook as HSSFWorkbook).Workbook;
            EscherBSERecord  bse = iwb.GetBSERecord(PictureIndex);

            byte[] data = bse.BlipRecord.PictureData;
            //int type = bse.BlipTypeWin32;

            using (MemoryStream ms = RecyclableMemory.GetStream(data))
            {
                using (Image img = Image.Load(ms))
                {
                    return(img.Size());
                }
            }
        }
Example #15
0
        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);
        }
Example #16
0
        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));
        }
Example #17
0
        /// <summary>
        /// Return the dimension of this image
        /// </summary>
        /// <returns>image dimension</returns>
        public Size GetImageDimension()
        {
            EscherBSERecord bse = patriarch.sheet.book.GetBSERecord(pictureIndex);

            byte[] data = bse.BlipRecord.PictureData;
            //int type = bse.BlipTypeWin32;

            Image img = Image.FromStream(new MemoryStream(data));

            return(img.Size);

            //switch (type)
            //{
            //    //we can calculate the preferred size only for JPEG and PNG
            //    //other formats like WMF, EMF and PICT are not supported in Java
            //    case HSSFWorkbook.PICTURE_TYPE_JPEG:
            //    case HSSFWorkbook.PICTURE_TYPE_PNG:
            //    case HSSFWorkbook.PICTURE_TYPE_DIB:
            //        try
            //        {
            //            //Read the image using javax.imageio.*
            //            ImageInputStream iis = ImageIO.CreateImageInputStream(new MemoryStream(data));
            //            IEnumerator i = ImageIO.GetImageReaders(iis);
            //            ImageReader r = (ImageReader)i.Current;
            //            r.SetInput(iis);
            //            BufferedImage img = r.Read(0);

            //            int[] dpi = GetResolution(r);
            //            size.width = img.Width* 96 / dpi[0];
            //            size.height = img.Height * 96 / dpi[1];

            //        }
            //        catch (IOException e)
            //        {
            //            //silently return if ImageIO failed to Read the image
            //            log.Log(POILogger.WARN, e);
            //        }

            //        break;
            //}
            //return size;
        }
Example #18
0
        /**
         * Performs a recursive search for pictures in the given list of escher records.
         *
         * @param escherRecords the escher records.
         * @param pictures the list to populate with the pictures.
         */
        private void SearchForPictures(IList escherRecords, List <Picture> pictures)
        {
            foreach (EscherRecord escherRecord in escherRecords)
            {
                if (escherRecord is EscherBSERecord)
                {
                    EscherBSERecord  bse  = (EscherBSERecord)escherRecord;
                    EscherBlipRecord blip = bse.BlipRecord;
                    if (blip != null)
                    {
                        pictures.Add(new Picture(blip.PictureData));
                    }
                    else if (bse.Offset > 0)
                    {
                        try
                        {
                            // Blip stored in delay stream, which in a word doc, is the main stream
                            IEscherRecordFactory recordFactory = new DefaultEscherRecordFactory();
                            EscherRecord         record        = recordFactory.CreateRecord(_mainStream, bse.Offset);

                            if (record is EscherBlipRecord)
                            {
                                record.FillFields(_mainStream, bse.Offset, recordFactory);
                                blip = (EscherBlipRecord)record;
                                pictures.Add(new Picture(blip.PictureData));
                            }
                        }
                        catch (Exception exc)
                        {
                            logger.Log(
                                POILogger.WARN,
                                "Unable to load picture from BLIB record at offset #",
                                bse.Offset, exc);
                        }
                    }
                }

                // Recursive call.
                SearchForPictures(escherRecord.ChildRecords, pictures);
            }
        }
Example #19
0
        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);
        }