| 1 | /*
|
|---|
| 2 | * This is public domain software - that is, you can do whatever you want
|
|---|
| 3 | * with it, and include it software that is licensed under the GNU or the
|
|---|
| 4 | * BSD license, or whatever other licence you choose, including proprietary
|
|---|
| 5 | * closed source licenses. I do ask that you leave this header in tact.
|
|---|
| 6 | *
|
|---|
| 7 | * If you make modifications to this code that you think would benefit the
|
|---|
| 8 | * wider community, please send me a copy and I'll post it on my site.
|
|---|
| 9 | *
|
|---|
| 10 | * If you make use of this code, I'd appreciate hearing about it.
|
|---|
| 11 | * drew@drewnoakes.com
|
|---|
| 12 | * Latest version of this software kept at
|
|---|
| 13 | * http://drewnoakes.com/
|
|---|
| 14 | */
|
|---|
| 15 | package com.drew.imaging.jpeg;
|
|---|
| 16 |
|
|---|
| 17 | import java.io.*;
|
|---|
| 18 | import java.util.ArrayList;
|
|---|
| 19 | import java.util.HashMap;
|
|---|
| 20 | import java.util.List;
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * Holds a collection of Jpeg data segments. This need not necessarily be all segments
|
|---|
| 24 | * within the Jpeg. For example, it may be convenient to port about only the non-image
|
|---|
| 25 | * segments when analysing (or serializing) metadata.
|
|---|
| 26 | */
|
|---|
| 27 | public class JpegSegmentData implements Serializable
|
|---|
| 28 | {
|
|---|
| 29 | static final long serialVersionUID = 7110175216435025451L;
|
|---|
| 30 |
|
|---|
| 31 | /** A map of byte[], keyed by the segment marker */
|
|---|
| 32 | private final HashMap _segmentDataMap;
|
|---|
| 33 |
|
|---|
| 34 | public JpegSegmentData()
|
|---|
| 35 | {
|
|---|
| 36 | _segmentDataMap = new HashMap(10);
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | public void addSegment(byte segmentMarker, byte[] segmentBytes)
|
|---|
| 40 | {
|
|---|
| 41 | List segmentList = getOrCreateSegmentList(segmentMarker);
|
|---|
| 42 | segmentList.add(segmentBytes);
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | public byte[] getSegment(byte segmentMarker)
|
|---|
| 46 | {
|
|---|
| 47 | return getSegment(segmentMarker, 0);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | public byte[] getSegment(byte segmentMarker, int occurrence)
|
|---|
| 51 | {
|
|---|
| 52 | final List segmentList = getSegmentList(segmentMarker);
|
|---|
| 53 |
|
|---|
| 54 | if (segmentList==null || segmentList.size()<=occurrence)
|
|---|
| 55 | return null;
|
|---|
| 56 | else
|
|---|
| 57 | return (byte[]) segmentList.get(occurrence);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | public int getSegmentCount(byte segmentMarker)
|
|---|
| 61 | {
|
|---|
| 62 | final List segmentList = getSegmentList(segmentMarker);
|
|---|
| 63 | if (segmentList==null)
|
|---|
| 64 | return 0;
|
|---|
| 65 | else
|
|---|
| 66 | return segmentList.size();
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | public void removeSegmentOccurrence(byte segmentMarker, int occurrence)
|
|---|
| 70 | {
|
|---|
| 71 | final List segmentList = (List)_segmentDataMap.get(new Byte(segmentMarker));
|
|---|
| 72 | segmentList.remove(occurrence);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | public void removeSegment(byte segmentMarker)
|
|---|
| 76 | {
|
|---|
| 77 | _segmentDataMap.remove(new Byte(segmentMarker));
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | private List getSegmentList(byte segmentMarker)
|
|---|
| 81 | {
|
|---|
| 82 | return (List)_segmentDataMap.get(new Byte(segmentMarker));
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | private List getOrCreateSegmentList(byte segmentMarker)
|
|---|
| 86 | {
|
|---|
| 87 | List segmentList;
|
|---|
| 88 | Byte key = new Byte(segmentMarker);
|
|---|
| 89 | if (_segmentDataMap.containsKey(key)) {
|
|---|
| 90 | segmentList = (List)_segmentDataMap.get(key);
|
|---|
| 91 | } else {
|
|---|
| 92 | segmentList = new ArrayList();
|
|---|
| 93 | _segmentDataMap.put(key, segmentList);
|
|---|
| 94 | }
|
|---|
| 95 | return segmentList;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | public boolean containsSegment(byte segmentMarker)
|
|---|
| 99 | {
|
|---|
| 100 | return _segmentDataMap.containsKey(new Byte(segmentMarker));
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | public static void ToFile(File file, JpegSegmentData segmentData) throws IOException
|
|---|
| 104 | {
|
|---|
| 105 | ObjectOutputStream outputStream = null;
|
|---|
| 106 | try
|
|---|
| 107 | {
|
|---|
| 108 | outputStream = new ObjectOutputStream(new FileOutputStream(file));
|
|---|
| 109 | outputStream.writeObject(segmentData);
|
|---|
| 110 | }
|
|---|
| 111 | finally
|
|---|
| 112 | {
|
|---|
| 113 | if (outputStream!=null)
|
|---|
| 114 | outputStream.close();
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | public static JpegSegmentData FromFile(File file) throws IOException, ClassNotFoundException
|
|---|
| 119 | {
|
|---|
| 120 | ObjectInputStream inputStream = null;
|
|---|
| 121 | try
|
|---|
| 122 | {
|
|---|
| 123 | inputStream = new ObjectInputStream(new FileInputStream(file));
|
|---|
| 124 | return (JpegSegmentData)inputStream.readObject();
|
|---|
| 125 | }
|
|---|
| 126 | finally
|
|---|
| 127 | {
|
|---|
| 128 | if (inputStream!=null)
|
|---|
| 129 | inputStream.close();
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|