summaryrefslogtreecommitdiffstats
path: root/gallerycommon/src/com/android/gallery3d/jpegstream/JPEGInputStream.java
blob: 44ccd4c6b90a8c8a5bb8402102029b7a4699d7fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.jpegstream;

import android.graphics.Point;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class JPEGInputStream extends FilterInputStream {
    private long JNIPointer = 0; // Used by JNI code. Don't touch.

    private boolean mValidConfig = false;
    private boolean mConfigChanged = false;
    private int mFormat = -1;
    private byte[] mTmpBuffer = new byte[1];
    private int mWidth = 0;
    private int mHeight = 0;

    public JPEGInputStream(InputStream in) {
        super(in);
    }

    public JPEGInputStream(InputStream in, int format) {
        super(in);
        setConfig(format);
    }

    public boolean setConfig(int format) {
        // Make sure format is valid
        switch (format) {
            case JpegConfig.FORMAT_GRAYSCALE:
            case JpegConfig.FORMAT_RGB:
            case JpegConfig.FORMAT_ABGR:
            case JpegConfig.FORMAT_RGBA:
                break;
            default:
                return false;
        }
        mFormat = format;
        mValidConfig = true;
        mConfigChanged = true;
        return true;
    }

    public Point getDimensions() throws IOException {
        if (mValidConfig) {
            applyConfigChange();
            return new Point(mWidth, mHeight);
        }
        return null;
    }

    @Override
    public int available() {
        return 0; // TODO
    }

    @Override
    public void close() throws IOException {
        cleanup();
        super.close();
    }

    @Override
    public synchronized void mark(int readlimit) {
        // Do nothing
    }

    @Override
    public boolean markSupported() {
        return false;
    }

    @Override
    public int read() throws IOException {
        read(mTmpBuffer, 0, 1);
        return 0xFF & mTmpBuffer[0];
    }

    @Override
    public int read(byte[] buffer) throws IOException {
        return read(buffer, 0, buffer.length);
    }

    @Override
    public int read(byte[] buffer, int offset, int count) throws IOException {
        if (offset < 0 || count < 0 || (offset + count) > buffer.length) {
            throw new ArrayIndexOutOfBoundsException(String.format(
                    " buffer length %d, offset %d, length %d",
                    buffer.length, offset, count));
        }
        if (!mValidConfig) {
            return 0;
        }
        applyConfigChange();
        int flag = JpegConfig.J_ERROR_FATAL;
        try {
            flag = readDecodedBytes(buffer, offset, count);
        } finally {
            if (flag < 0) {
                cleanup();
            }
        }
        if (flag < 0) {
            switch (flag) {
                case JpegConfig.J_DONE:
                    return -1; // Returns -1 after reading EOS.
                default:
                    throw new IOException("Error reading jpeg stream");
            }
        }
        return flag;
    }

    @Override
    public synchronized void reset() throws IOException {
        throw new IOException("Reset not supported.");
    }

    @Override
    public long skip(long byteCount) throws IOException {
        if (byteCount <= 0) {
            return 0;
        }
        // Shorten skip to a reasonable amount
        int flag = skipDecodedBytes((int) (0x7FFFFFFF & byteCount));
        if (flag < 0) {
            switch (flag) {
                case JpegConfig.J_DONE:
                    return 0; // Returns 0 after reading EOS.
                default:
                    throw new IOException("Error skipping jpeg stream");
            }
        }
        return flag;
    }

    @Override
    protected void finalize() throws Throwable {
        try {
            cleanup();
        } finally {
            super.finalize();
        }
    }

    private void applyConfigChange() throws IOException {
        if (mConfigChanged) {
            cleanup();
            Point dimens = new Point(0, 0);
            int flag = setup(dimens, in, mFormat);
            switch(flag) {
                case JpegConfig.J_SUCCESS:
                    break; // allow setup to continue
                case JpegConfig.J_ERROR_BAD_ARGS:
                    throw new IllegalArgumentException("Bad arguments to read");
                default:
                    throw new IOException("Error to reading jpeg headers.");
            }
            mWidth = dimens.x;
            mHeight = dimens.y;
            mConfigChanged = false;
        }
    }

    native private int setup(Point dimens, InputStream in, int format);

    native private void cleanup();

    native private int readDecodedBytes( byte[] inBuffer, int offset, int inCount);

    native private int skipDecodedBytes(int bytes);

    static {
        System.loadLibrary("jni_jpegstream");
    }
}