View | Details | Raw Unified | Return to bug 41223
Collapse All | Expand All

(-)src/java/org/apache/poi/hssf/usermodel/HSSFPictureData.java (+90 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Copyright 2002-2004   Apache Software Foundation
3
4
   Licensed under the Apache License, Version 2.0 (the "License");
5
   you may not use this file except in compliance with the License.
6
   You may obtain a copy of the License at
7
8
       http://www.apache.org/licenses/LICENSE-2.0
9
10
   Unless required by applicable law or agreed to in writing, software
11
   distributed under the License is distributed on an "AS IS" BASIS,
12
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
   See the License for the specific language governing permissions and
14
   limitations under the License.
15
==================================================================== */
16
17
18
package org.apache.poi.hssf.usermodel;
19
20
import org.apache.poi.ddf.EscherBitmapBlip;
21
22
/**
23
 * Represents binary data stored in the file.  Eg. A GIF, JPEG etc...
24
 *
25
 * @author Daniel Noll
26
 */
27
public class HSSFPictureData
28
{
29
    // MSOBI constants for various formats.
30
    public static final short MSOBI_WMF   = 0x2160;
31
    public static final short MSOBI_EMF   = 0x3D40;
32
    public static final short MSOBI_PICT  = 0x5420;
33
    public static final short MSOBI_PNG   = 0x6E00;
34
    public static final short MSOBI_JPEG  = 0x46A0;
35
    public static final short MSOBI_DIB   = 0x7A80;
36
    // Mask of the bits in the options used to store the image format.
37
    public static final short FORMAT_MASK = (short) 0xFFF0;
38
39
    /**
40
     * Underlying escher blip record containing the bitmap data.
41
     */
42
    private EscherBitmapBlip blip;
43
44
    /**
45
     * Constructs a picture object.
46
     *
47
     * @param blip the underlying blip record containing the bitmap data.
48
     */
49
    HSSFPictureData( EscherBitmapBlip blip )
50
    {
51
        this.blip = blip;
52
    }
53
54
    /**
55
     * Gets the picture data.
56
     *
57
     * @return the picture data.
58
     */
59
    public byte[] getData()
60
    {
61
        return blip.getPicturedata();
62
    }
63
64
    /**
65
     * Suggests a file extension for this image.
66
     *
67
     * @return the file extension.
68
     */
69
    public String suggestFileExtension()
70
    {
71
        switch (blip.getOptions() & FORMAT_MASK)
72
        {
73
            case MSOBI_WMF:
74
                return "wmf";
75
            case MSOBI_EMF:
76
                return "emf";
77
            case MSOBI_PICT:
78
                return "pict";
79
            case MSOBI_PNG:
80
                return "png";
81
            case MSOBI_JPEG:
82
                return "jpeg";
83
            case MSOBI_DIB:
84
                return "dib";
85
            default:
86
                return "";
87
        }
88
    }
89
}
90
0
  + *
91
  + *
(-)src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java (-11 / +84 lines)
Lines 25-30 Link Here
25
import org.apache.poi.ddf.EscherBSERecord;
25
import org.apache.poi.ddf.EscherBSERecord;
26
import org.apache.poi.ddf.EscherBitmapBlip;
26
import org.apache.poi.ddf.EscherBitmapBlip;
27
import org.apache.poi.ddf.EscherRecord;
27
import org.apache.poi.ddf.EscherRecord;
28
import org.apache.poi.ddf.EscherBlipRecord;
28
import org.apache.poi.hssf.eventmodel.EventRecordFactory;
29
import org.apache.poi.hssf.eventmodel.EventRecordFactory;
29
import org.apache.poi.hssf.model.Sheet;
30
import org.apache.poi.hssf.model.Sheet;
30
import org.apache.poi.hssf.model.Workbook;
31
import org.apache.poi.hssf.model.Workbook;
Lines 113-126 Link Here
113
    private HSSFDataFormat formatter;
114
    private HSSFDataFormat formatter;
114
115
115
116
116
    /// NOT YET SUPPORTED:
117
    /** Extended windows meta file */
117
    /** Extended windows meta file */
118
    //public static final int PICTURE_TYPE_EMF = 2;
118
    public static final int PICTURE_TYPE_EMF = 2;
119
    /** Windows Meta File */
119
    /** Windows Meta File */
120
    //public static final int PICTURE_TYPE_WMF = 3;
120
    public static final int PICTURE_TYPE_WMF = 3;
121
    /** Mac PICT format */
121
    /** Mac PICT format */
122
    //public static final int PICTURE_TYPE_PICT = 4;
122
    public static final int PICTURE_TYPE_PICT = 4;
123
124
    /** JPEG format */
123
    /** JPEG format */
125
    public static final int PICTURE_TYPE_JPEG = 5;
124
    public static final int PICTURE_TYPE_JPEG = 5;
126
    /** PNG format */
125
    /** PNG format */
Lines 216-221 Link Here
216
            Sheet sheet = Sheet.createSheet(records, sheetNum++, recOffset );
215
            Sheet sheet = Sheet.createSheet(records, sheetNum++, recOffset );
217
216
218
            recOffset = sheet.getEofLoc()+1;
217
            recOffset = sheet.getEofLoc()+1;
218
            if (recOffset == 1)
219
            {
220
                break;
221
            }
222
219
            HSSFSheet hsheet = new HSSFSheet(workbook, sheet);
223
            HSSFSheet hsheet = new HSSFSheet(workbook, sheet);
220
224
221
            sheets.add(hsheet);
225
            sheets.add(hsheet);
Lines 1270-1281 Link Here
1270
        byte[] uid = newUID();
1274
        byte[] uid = newUID();
1271
        EscherBitmapBlip blipRecord = new EscherBitmapBlip();
1275
        EscherBitmapBlip blipRecord = new EscherBitmapBlip();
1272
        blipRecord.setRecordId( (short) ( EscherBitmapBlip.RECORD_ID_START + format ) );
1276
        blipRecord.setRecordId( (short) ( EscherBitmapBlip.RECORD_ID_START + format ) );
1273
        if (format == HSSFWorkbook.PICTURE_TYPE_PNG)
1277
        switch (format)
1274
            blipRecord.setOptions( (short) 0x6E00 );  // MSOBI
1278
        {
1275
        else if (format == HSSFWorkbook.PICTURE_TYPE_JPEG)
1279
            case PICTURE_TYPE_EMF:
1276
            blipRecord.setOptions( (short) 0x46A0 );  // MSOBI
1280
                blipRecord.setOptions(HSSFPictureData.MSOBI_EMF);
1277
        else if (format == HSSFWorkbook.PICTURE_TYPE_DIB)
1281
                break;
1278
            blipRecord.setOptions( (short) 0x7A8 );  // MSOBI
1282
            case PICTURE_TYPE_WMF:
1283
                blipRecord.setOptions(HSSFPictureData.MSOBI_WMF);
1284
                break;
1285
            case PICTURE_TYPE_PICT:
1286
                blipRecord.setOptions(HSSFPictureData.MSOBI_PICT);
1287
                break;
1288
            case PICTURE_TYPE_PNG:
1289
                blipRecord.setOptions(HSSFPictureData.MSOBI_PNG);
1290
                break;
1291
            case HSSFWorkbook.PICTURE_TYPE_JPEG:
1292
                blipRecord.setOptions(HSSFPictureData.MSOBI_JPEG);
1293
                break;
1294
            case HSSFWorkbook.PICTURE_TYPE_DIB:
1295
                blipRecord.setOptions(HSSFPictureData.MSOBI_DIB);
1296
                break;
1297
        }
1279
1298
1280
        blipRecord.setUID( uid );
1299
        blipRecord.setUID( uid );
1281
        blipRecord.setMarker( (byte) 0xFF );
1300
        blipRecord.setMarker( (byte) 0xFF );
Lines 1296-1301 Link Here
1296
        return workbook.addBSERecord( r );
1315
        return workbook.addBSERecord( r );
1297
    }
1316
    }
1298
1317
1318
    /**
1319
     * Gets all pictures from the Workbook.
1320
     *
1321
     * @return the list of pictures (a list of {@link HSSFPictureData} objects.)
1322
     */
1323
    public List getAllPictures()
1324
    {
1325
        List pictures = new ArrayList();
1326
        Iterator recordIter = workbook.getRecords().iterator();
1327
        while (recordIter.hasNext())
1328
        {
1329
            Object obj = recordIter.next();
1330
            if (obj instanceof AbstractEscherHolderRecord)
1331
            {
1332
                ((AbstractEscherHolderRecord) obj).decode();
1333
                List escherRecords = ((AbstractEscherHolderRecord) obj).getEscherRecords();
1334
                searchForPictures(escherRecords, pictures);
1335
            }
1336
        }
1337
        return pictures;
1338
    }
1339
1340
    /**
1341
     * Performs a recursive search for pictures in the given list of escher records.
1342
     *
1343
     * @param escherRecords the escher records.
1344
     * @param pictures the list to populate with the pictures.
1345
     */
1346
    private void searchForPictures(List escherRecords, List pictures)
1347
    {
1348
        Iterator recordIter = escherRecords.iterator();
1349
        while (recordIter.hasNext())
1350
        {
1351
            Object obj = recordIter.next();
1352
            if (obj instanceof EscherRecord)
1353
            {
1354
                EscherRecord escherRecord = (EscherRecord) obj;
1355
1356
                if (escherRecord instanceof EscherBSERecord)
1357
                {
1358
                    EscherBlipRecord blip = ((EscherBSERecord) escherRecord).getBlipRecord();
1359
                    if (blip instanceof EscherBitmapBlip)
1360
                    {
1361
                        // TODO: Some kind of structure.
1362
                        pictures.add(new HSSFPictureData((EscherBitmapBlip) blip));
1363
                    }
1364
                }
1365
1366
                // Recursive call.
1367
                searchForPictures(escherRecord.getChildRecords(), pictures);
1368
            }
1369
        }
1370
    }
1371
1299
    private byte[] newUID()
1372
    private byte[] newUID()
1300
    {
1373
    {
1301
        byte[] bytes = new byte[16];
1374
        byte[] bytes = new byte[16];

Return to bug 41223