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