blob: de6294dabaeb98cccfca682bbcab7477811ce293 [file] [log] [blame]
Wei-Ta Chen4b6f4942009-09-01 17:44:49 +08001/*
2 * Copyright (C) 2009 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
17#define LOG_TAG "OmxJpegDecoder"
18#include <sys/time.h>
19#include <utils/Log.h>
20
21#include <binder/ProcessState.h>
22
23#include "SkBitmap.h"
24#include "SkImageDecoder.h"
25#include "SkStream.h"
26#include "omx_jpeg_decoder.h"
27
Wei-Ta Chenb3550122009-12-03 23:19:19 +080028class SkJPEGImageDecoder : public SkImageDecoder {
29public:
30 virtual Format getFormat() const {
31 return kJPEG_Format;
32 }
33
34protected:
Mike Reed945a9df2010-03-03 13:24:37 -050035 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode);
Wei-Ta Chenb3550122009-12-03 23:19:19 +080036};
37
Wei-Ta Chen4b6f4942009-09-01 17:44:49 +080038int nullObjectReturn(const char msg[]) {
39 if (msg) {
40 SkDebugf("--- %s\n", msg);
41 }
42 return -1;
43}
44
45static int64_t getNowUs() {
46 struct timeval tv;
47 gettimeofday(&tv, NULL);
48
49 return tv.tv_usec + (int64_t) tv.tv_sec * 1000000;
50}
51
52int testDecodeBounds(SkImageDecoder* decoder, SkStream* stream,
53 SkBitmap* bitmap) {
54 int64_t startTime = getNowUs();
Mike Reed42a1d082014-07-07 18:06:18 -040055 SkColorType prefColorType = kN32_SkColorType;
Wei-Ta Chen4b6f4942009-09-01 17:44:49 +080056 SkImageDecoder::Mode decodeMode = SkImageDecoder::kDecodeBounds_Mode;
57
58 // Decode the input stream and then use the bitmap.
Mike Reed42a1d082014-07-07 18:06:18 -040059 if (!decoder->decode(stream, bitmap, prefColorType, decodeMode)) {
Wei-Ta Chen4b6f4942009-09-01 17:44:49 +080060 return nullObjectReturn("decoder->decode returned false");
61 } else {
62 int64_t delay = getNowUs() - startTime;
63 printf("WidthxHeight: %dx%d\n", bitmap->width(), bitmap->height());
64 printf("Decoding Time in BoundsMode %.1f msec.\n", delay / 1000.0f);
65 return 0;
66 }
67}
68
69int testDecodePixels(SkImageDecoder* decoder, SkStream* stream,
70 SkBitmap* bitmap) {
71 int64_t startTime = getNowUs();
Mike Reed42a1d082014-07-07 18:06:18 -040072 SkColorType prefColorType = kN32_SkColorType;
Wei-Ta Chen4b6f4942009-09-01 17:44:49 +080073 SkImageDecoder::Mode decodeMode = SkImageDecoder::kDecodePixels_Mode;
74
75 // Decode the input stream and then use the bitmap.
Mike Reed42a1d082014-07-07 18:06:18 -040076 if (!decoder->decode(stream, bitmap, prefColorType, decodeMode)) {
Wei-Ta Chen4b6f4942009-09-01 17:44:49 +080077 return nullObjectReturn("decoder->decode returned false");
78 } else {
79 int64_t delay = getNowUs() - startTime;
80 printf("Decoding Time in PixelsMode %.1f msec.\n", delay / 1000.0f);
Andreas Huber987dbde2009-12-16 13:13:27 -080081 const char* filename = "/sdcard/omxJpegDecodedBitmap.rgba";
Wei-Ta Chen4b6f4942009-09-01 17:44:49 +080082 return storeBitmapToFile(bitmap, filename);
83 }
84}
85
86int testDecoder(SkImageDecoder* decoder, char* filename) {
87 // test DecodeMode == Pixels
88 SkStream* stream = new SkFILEStream(filename);
89 SkBitmap* bitmap = new SkBitmap;
90 testDecodePixels(decoder, stream, bitmap);
91 delete bitmap;
92
93 // test DecodeMode == Bounds
94 stream = new SkFILEStream(filename);
95 bitmap = new SkBitmap;
96 testDecodeBounds(decoder, stream, bitmap);
97 delete bitmap;
98
99 delete decoder;
100 return 0;
101}
102
103int main(int argc, char** argv) {
104 android::ProcessState::self()->startThreadPool();
105
106 printf("Decoding jpeg with libjpeg...\n");
107 SkJPEGImageDecoder* libjpeg = new SkJPEGImageDecoder;
108 testDecoder(libjpeg, argv[1]);
109
110 printf("\nDecoding jpeg with OMX...\n");
111 OmxJpegImageDecoder* omx = new OmxJpegImageDecoder;
112 testDecoder(omx, argv[1]);
113 return 0;
114}