| 1 | /*
|
|---|
| 2 | * Copyright 2002-2012 Drew Noakes
|
|---|
| 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 | * More information about this project is available at:
|
|---|
| 17 | *
|
|---|
| 18 | * http://drewnoakes.com/code/exif/
|
|---|
| 19 | * http://code.google.com/p/metadata-extractor/
|
|---|
| 20 | */
|
|---|
| 21 | package com.drew.metadata.jpeg;
|
|---|
| 22 |
|
|---|
| 23 | import com.drew.lang.BufferBoundsException;
|
|---|
| 24 | import com.drew.lang.BufferReader;
|
|---|
| 25 | import com.drew.lang.annotations.NotNull;
|
|---|
| 26 | import com.drew.metadata.Metadata;
|
|---|
| 27 | import com.drew.metadata.MetadataReader;
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * Decodes Jpeg SOF0 data, populating a <code>Metadata</code> object with tag values in a <code>JpegDirectory</code>.
|
|---|
| 31 | *
|
|---|
| 32 | * @author Darrell Silver http://www.darrellsilver.com and Drew Noakes http://drewnoakes.com
|
|---|
| 33 | */
|
|---|
| 34 | public class JpegReader implements MetadataReader
|
|---|
| 35 | {
|
|---|
| 36 | /**
|
|---|
| 37 | * Performs the Jpeg data extraction, adding found values to the specified
|
|---|
| 38 | * instance of <code>Metadata</code>.
|
|---|
| 39 | */
|
|---|
| 40 | public void extract(@NotNull final BufferReader reader, @NotNull Metadata metadata)
|
|---|
| 41 | {
|
|---|
| 42 | JpegDirectory directory = metadata.getOrCreateDirectory(JpegDirectory.class);
|
|---|
| 43 |
|
|---|
| 44 | try {
|
|---|
| 45 | // data precision
|
|---|
| 46 | int dataPrecision = reader.getUInt8(JpegDirectory.TAG_JPEG_DATA_PRECISION);
|
|---|
| 47 | directory.setInt(JpegDirectory.TAG_JPEG_DATA_PRECISION, dataPrecision);
|
|---|
| 48 |
|
|---|
| 49 | // process height
|
|---|
| 50 | int height = reader.getUInt16(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT);
|
|---|
| 51 | directory.setInt(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT, height);
|
|---|
| 52 |
|
|---|
| 53 | // process width
|
|---|
| 54 | int width = reader.getUInt16(JpegDirectory.TAG_JPEG_IMAGE_WIDTH);
|
|---|
| 55 | directory.setInt(JpegDirectory.TAG_JPEG_IMAGE_WIDTH, width);
|
|---|
| 56 |
|
|---|
| 57 | // number of components
|
|---|
| 58 | int numberOfComponents = reader.getUInt8(JpegDirectory.TAG_JPEG_NUMBER_OF_COMPONENTS);
|
|---|
| 59 | directory.setInt(JpegDirectory.TAG_JPEG_NUMBER_OF_COMPONENTS, numberOfComponents);
|
|---|
| 60 |
|
|---|
| 61 | // for each component, there are three bytes of data:
|
|---|
| 62 | // 1 - Component ID: 1 = Y, 2 = Cb, 3 = Cr, 4 = I, 5 = Q
|
|---|
| 63 | // 2 - Sampling factors: bit 0-3 vertical, 4-7 horizontal
|
|---|
| 64 | // 3 - Quantization table number
|
|---|
| 65 | int offset = 6;
|
|---|
| 66 | for (int i = 0; i < numberOfComponents; i++) {
|
|---|
| 67 | int componentId = reader.getUInt8(offset++);
|
|---|
| 68 | int samplingFactorByte = reader.getUInt8(offset++);
|
|---|
| 69 | int quantizationTableNumber = reader.getUInt8(offset++);
|
|---|
| 70 | JpegComponent component = new JpegComponent(componentId, samplingFactorByte, quantizationTableNumber);
|
|---|
| 71 | directory.setObject(JpegDirectory.TAG_JPEG_COMPONENT_DATA_1 + i, component);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | } catch (BufferBoundsException ex) {
|
|---|
| 75 | directory.addError(ex.getMessage());
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|