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_WRITER_H_
17a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk#define JPEG_WRITER_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 * JpegWriter wraps libjpeg's compression functionality and a
27a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * java OutputStream object.  Write calls result in input data
28a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * being compressed and written to the OuputStream.
29a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk */
30a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunkclass JpegWriter {
31a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunkpublic:
32a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    JpegWriter();
33a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    ~JpegWriter();
34a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
35a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    /**
36a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * Call setup with a valid OutputStream reference, bitmap height and
37a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * width, pixel format, and compression quality in range (0, 100].
38a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     *
39a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * Returns J_SUCCESS on success or a negative error code.
40a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     */
41a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    int32_t setup(JNIEnv *env, jobject out, int32_t width, int32_t height,
42a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk            Jpeg_Config::Format format, int32_t quality);
43a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
44a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    /**
45a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * Compresses bytes from the input buffer.
46a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     *
47a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * ***This method will result in bytes being written to the OutputStream***
48a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     *
49a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * Returns J_SUCCESS on success or a negative error code.
50a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     */
51a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    int32_t write(int8_t* bytes, int32_t length);
52a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
53a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    /**
54a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * Updates the environment pointer.  Call this before write or reset
55a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * in any jni function call.
56a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     */
57a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    void updateEnv(JNIEnv *env);
58a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
59a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    /**
60a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * Frees any java global references held by the JpegWriter, destroys
61a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     * the compress structure, and frees allocations in libjpeg's pools.
62a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk     */
63a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    int32_t reset();
64a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
65a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    static const int32_t DEFAULT_X_DENSITY;
66a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    static const int32_t DEFAULT_Y_DENSITY;
67a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    static const int32_t DEFAULT_DENSITY_UNIT;
68a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunkprivate:
69a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    void formatPixels(uint8_t* buf, int32_t len);
70a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    struct jpeg_compress_struct mInfo;
71a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    ErrManager mErrorManager;
72a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
73a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    JSAMPLE* mScanlineBuf;
74a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    JSAMPLE* mScanlineIter;
75a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    int32_t mScanlineBuflen;
76a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    int32_t mScanlineBytesRemaining;
77a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
78a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    Jpeg_Config::Format mFormat;
79a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    bool mFinished;
80a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk    bool mSetup;
81a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk};
82a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk
83a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk#endif // JPEG_WRITER_H_
84