1 /*
2  * Copyright (C) 2013 The Android Open Source Project
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 #ifndef JPEG_READER_H_
17 #define JPEG_READER_H_
18 
19 #include "jerr_hook.h"
20 #include "jni_defines.h"
21 #include "jpeg_config.h"
22 
23 #include <stdint.h>
24 
25 /**
26  * JpegReader wraps libjpeg's decompression functionality and a
27  * java InputStream object.  Read calls return data from the
28  * InputStream that has been decompressed.
29  */
30 class JpegReader {
31 public:
32     JpegReader();
33     ~JpegReader();
34 
35     /**
36      * Call setup with a valid InputStream reference and pixel format.
37      * If this method is successful, the contents of width and height will
38      * be set to the dimensions of the bitmap to be read.
39      *
40      * ***This method will result in the jpeg file header being read
41      * from the InputStream***
42      *
43      * Returns J_SUCCESS on success or a negative error code.
44      */
45     int32_t setup(JNIEnv *env, jobject in, int32_t* width, int32_t* height,
46             Jpeg_Config::Format format);
47 
48     /**
49      * Decompresses bytes from the InputStream and writes at most count
50      * bytes into the buffer, bytes, starting at some offset.  Passing a
51      * NULL as the bytes pointer effectively skips those bytes.
52      *
53      * ***This method will result in bytes being read from the InputStream***
54      *
55      * Returns the number of bytes written into the input buffer or a
56      * negative error code.
57      */
58     int32_t read(int8_t * bytes, int32_t offset, int32_t count);
59 
60     /**
61      * Updates the environment pointer.  Call this before read or reset
62      * in any jni function call.
63      */
64     void updateEnv(JNIEnv *env);
65 
66     /**
67      * Frees any java global references held by the JpegReader, destroys
68      * the decompress structure, and frees allocations in libjpeg's pools.
69      */
70     int32_t reset();
71 
72 private:
73     void formatPixels(uint8_t* buf, int32_t len);
74     struct jpeg_decompress_struct mInfo;
75     ErrManager mErrorManager;
76 
77     JSAMPLE* mScanlineBuf;
78     JSAMPLE* mScanlineIter;
79     int32_t mScanlineBuflen;
80     int32_t mScanlineUnformattedBuflen;
81     int32_t mScanlineBytesRemaining;
82 
83     Jpeg_Config::Format mFormat;
84     bool mFinished;
85     bool mSetup;
86 };
87 
88 #endif // JPEG_READER_H_
89