1a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk/*
2a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * Copyright (C) 2013 The Android Open Source Project
3a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk *
4a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * Licensed under the Apache License, Version 2.0 (the "License");
5a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * you may not use this file except in compliance with the License.
6a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * You may obtain a copy of the License at
7a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk *
8a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk *      http://www.apache.org/licenses/LICENSE-2.0
9a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk *
10a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * Unless required by applicable law or agreed to in writing, software
11a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * distributed under the License is distributed on an "AS IS" BASIS,
12a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * See the License for the specific language governing permissions and
14a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * limitations under the License.
15a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk */
16a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk#ifndef JPEG_READER_H_
17a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk#define JPEG_READER_H_
18a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
19a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk#include "jerr_hook.h"
20a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk#include "jni_defines.h"
21a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk#include "jpeg_config.h"
22a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
23a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk#include <stdint.h>
24a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
25a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk/**
26a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * JpegReader wraps libjpeg's decompression functionality and a
27a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * java InputStream object.  Read calls return data from the
28a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * InputStream that has been decompressed.
29a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk */
30a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunkclass JpegReader {
31a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunkpublic:
32a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    JpegReader();
33a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    ~JpegReader();
34a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
35a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    /**
36a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * Call setup with a valid InputStream reference and pixel format.
37a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * If this method is successful, the contents of width and height will
38a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * be set to the dimensions of the bitmap to be read.
39a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     *
40a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * ***This method will result in the jpeg file header being read
41a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * from the InputStream***
42a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     *
43a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * Returns J_SUCCESS on success or a negative error code.
44a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     */
45a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    int32_t setup(JNIEnv *env, jobject in, int32_t* width, int32_t* height,
46a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk            Jpeg_Config::Format format);
47a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
48a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    /**
49a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * Decompresses bytes from the InputStream and writes at most count
50a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * bytes into the buffer, bytes, starting at some offset.  Passing a
51a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * NULL as the bytes pointer effectively skips those bytes.
52a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     *
53a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * ***This method will result in bytes being read from the InputStream***
54a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     *
55a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * Returns the number of bytes written into the input buffer or a
56a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * negative error code.
57a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     */
58a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    int32_t read(int8_t * bytes, int32_t offset, int32_t count);
59a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
60a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    /**
61a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * Updates the environment pointer.  Call this before read or reset
62a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * in any jni function call.
63a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     */
64a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    void updateEnv(JNIEnv *env);
65a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
66a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    /**
67a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * Frees any java global references held by the JpegReader, destroys
68a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * the decompress structure, and frees allocations in libjpeg's pools.
69a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     */
70a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    int32_t reset();
71a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
72a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunkprivate:
73a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    void formatPixels(uint8_t* buf, int32_t len);
74a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    struct jpeg_decompress_struct mInfo;
75a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    ErrManager mErrorManager;
76a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
77a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    JSAMPLE* mScanlineBuf;
78a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    JSAMPLE* mScanlineIter;
79a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    int32_t mScanlineBuflen;
80a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    int32_t mScanlineUnformattedBuflen;
81a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    int32_t mScanlineBytesRemaining;
82a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
83a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    Jpeg_Config::Format mFormat;
84a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    bool mFinished;
85a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    bool mSetup;
86a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk};
87a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
88a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk#endif // JPEG_READER_H_
89