James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 1 | // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 | // |
James Zern | d640614 | 2013-06-06 23:05:58 -0700 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license |
| 4 | // that can be found in the COPYING file in the root of the source |
| 5 | // tree. An additional intellectual property rights grant can be found |
| 6 | // in the file PATENTS. All contributing project authors may |
| 7 | // be found in the AUTHORS file in the root of the source tree. |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 8 | // ----------------------------------------------------------------------------- |
| 9 | // |
| 10 | // Utility functions used by the example programs. |
| 11 | // |
| 12 | |
| 13 | #include "./example_util.h" |
James Zern | 5927e15 | 2014-08-29 19:07:17 -0700 | [diff] [blame^] | 14 | |
| 15 | #if defined(_WIN32) |
| 16 | #include <fcntl.h> // for _O_BINARY |
| 17 | #include <io.h> // for _setmode() |
| 18 | #endif |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame] | 21 | #include <string.h> |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 22 | |
James Zern | 4a0e739 | 2014-04-22 19:33:22 -0700 | [diff] [blame] | 23 | #include "webp/decode.h" |
| 24 | #include "./stopwatch.h" |
| 25 | |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 26 | // ----------------------------------------------------------------------------- |
| 27 | // File I/O |
| 28 | |
James Zern | 5927e15 | 2014-08-29 19:07:17 -0700 | [diff] [blame^] | 29 | FILE* ExUtilSetBinaryMode(FILE* file) { |
| 30 | #if defined(_WIN32) |
| 31 | if (_setmode(_fileno(file), _O_BINARY) == -1) { |
| 32 | fprintf(stderr, "Failed to reopen file in O_BINARY mode.\n"); |
| 33 | return NULL; |
| 34 | } |
| 35 | #endif |
| 36 | return file; |
| 37 | } |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame] | 38 | |
| 39 | int ExUtilReadFromStdin(const uint8_t** data, size_t* data_size) { |
James Zern | 5927e15 | 2014-08-29 19:07:17 -0700 | [diff] [blame^] | 40 | static const size_t kBlockSize = 16384; // default initial size |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame] | 41 | size_t max_size = 0; |
| 42 | size_t size = 0; |
| 43 | uint8_t* input = NULL; |
| 44 | |
| 45 | if (data == NULL || data_size == NULL) return 0; |
| 46 | *data = NULL; |
| 47 | *data_size = 0; |
| 48 | |
| 49 | while (!feof(stdin)) { |
| 50 | // We double the buffer size each time and read as much as possible. |
| 51 | const size_t extra_size = (max_size == 0) ? kBlockSize : max_size; |
| 52 | void* const new_data = realloc(input, max_size + extra_size); |
| 53 | if (new_data == NULL) goto Error; |
| 54 | input = (uint8_t*)new_data; |
| 55 | max_size += extra_size; |
| 56 | size += fread(input + size, 1, extra_size, stdin); |
| 57 | if (size < max_size) break; |
| 58 | } |
| 59 | if (ferror(stdin)) goto Error; |
| 60 | *data = input; |
| 61 | *data_size = size; |
| 62 | return 1; |
| 63 | |
| 64 | Error: |
| 65 | free(input); |
| 66 | fprintf(stderr, "Could not read from stdin\n"); |
| 67 | return 0; |
| 68 | } |
| 69 | |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 70 | int ExUtilReadFile(const char* const file_name, |
| 71 | const uint8_t** data, size_t* data_size) { |
| 72 | int ok; |
| 73 | void* file_data; |
| 74 | size_t file_size; |
| 75 | FILE* in; |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame] | 76 | const int from_stdin = (file_name == NULL) || !strcmp(file_name, "-"); |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 77 | |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame] | 78 | if (from_stdin) return ExUtilReadFromStdin(data, data_size); |
| 79 | |
| 80 | if (data == NULL || data_size == NULL) return 0; |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 81 | *data = NULL; |
| 82 | *data_size = 0; |
| 83 | |
| 84 | in = fopen(file_name, "rb"); |
| 85 | if (in == NULL) { |
| 86 | fprintf(stderr, "cannot open input file '%s'\n", file_name); |
| 87 | return 0; |
| 88 | } |
| 89 | fseek(in, 0, SEEK_END); |
| 90 | file_size = ftell(in); |
| 91 | fseek(in, 0, SEEK_SET); |
| 92 | file_data = malloc(file_size); |
| 93 | if (file_data == NULL) return 0; |
| 94 | ok = (fread(file_data, file_size, 1, in) == 1); |
| 95 | fclose(in); |
| 96 | |
| 97 | if (!ok) { |
James Zern | 14d42af | 2013-03-20 16:59:35 -0700 | [diff] [blame] | 98 | fprintf(stderr, "Could not read %d bytes of data from file %s\n", |
| 99 | (int)file_size, file_name); |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 100 | free(file_data); |
| 101 | return 0; |
| 102 | } |
| 103 | *data = (uint8_t*)file_data; |
| 104 | *data_size = file_size; |
| 105 | return 1; |
| 106 | } |
| 107 | |
Urvang Joshi | e9a15a3 | 2012-11-07 14:41:15 -0800 | [diff] [blame] | 108 | int ExUtilWriteFile(const char* const file_name, |
| 109 | const uint8_t* data, size_t data_size) { |
| 110 | int ok; |
| 111 | FILE* out; |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame] | 112 | const int to_stdout = (file_name == NULL) || !strcmp(file_name, "-"); |
Urvang Joshi | e9a15a3 | 2012-11-07 14:41:15 -0800 | [diff] [blame] | 113 | |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame] | 114 | if (data == NULL) { |
Urvang Joshi | e9a15a3 | 2012-11-07 14:41:15 -0800 | [diff] [blame] | 115 | return 0; |
| 116 | } |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame] | 117 | out = to_stdout ? stdout : fopen(file_name, "wb"); |
Urvang Joshi | e9a15a3 | 2012-11-07 14:41:15 -0800 | [diff] [blame] | 118 | if (out == NULL) { |
| 119 | fprintf(stderr, "Error! Cannot open output file '%s'\n", file_name); |
| 120 | return 0; |
| 121 | } |
| 122 | ok = (fwrite(data, data_size, 1, out) == 1); |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame] | 123 | if (out != stdout) fclose(out); |
Urvang Joshi | e9a15a3 | 2012-11-07 14:41:15 -0800 | [diff] [blame] | 124 | return ok; |
| 125 | } |
| 126 | |
James Zern | 4a0e739 | 2014-04-22 19:33:22 -0700 | [diff] [blame] | 127 | //------------------------------------------------------------------------------ |
| 128 | // WebP decoding |
| 129 | |
skal | 0b747b1 | 2014-07-21 15:44:43 +0200 | [diff] [blame] | 130 | static const char* const kStatusMessages[VP8_STATUS_NOT_ENOUGH_DATA + 1] = { |
James Zern | 4a0e739 | 2014-04-22 19:33:22 -0700 | [diff] [blame] | 131 | "OK", "OUT_OF_MEMORY", "INVALID_PARAM", "BITSTREAM_ERROR", |
| 132 | "UNSUPPORTED_FEATURE", "SUSPENDED", "USER_ABORT", "NOT_ENOUGH_DATA" |
| 133 | }; |
| 134 | |
James Zern | 1b2fe14 | 2014-04-26 14:26:13 -0700 | [diff] [blame] | 135 | static void PrintAnimationWarning(const WebPDecoderConfig* const config) { |
| 136 | if (config->input.has_animation) { |
| 137 | fprintf(stderr, |
| 138 | "Error! Decoding of an animated WebP file is not supported.\n" |
| 139 | " Use webpmux to extract the individual frames or\n" |
| 140 | " vwebp to view this image.\n"); |
| 141 | } |
| 142 | } |
| 143 | |
James Zern | 4a0e739 | 2014-04-22 19:33:22 -0700 | [diff] [blame] | 144 | void ExUtilPrintWebPError(const char* const in_file, int status) { |
| 145 | fprintf(stderr, "Decoding of %s failed.\n", in_file); |
| 146 | fprintf(stderr, "Status: %d", status); |
| 147 | if (status >= VP8_STATUS_OK && status <= VP8_STATUS_NOT_ENOUGH_DATA) { |
| 148 | fprintf(stderr, "(%s)", kStatusMessages[status]); |
| 149 | } |
| 150 | fprintf(stderr, "\n"); |
| 151 | } |
| 152 | |
| 153 | int ExUtilLoadWebP(const char* const in_file, |
| 154 | const uint8_t** data, size_t* data_size, |
| 155 | WebPBitstreamFeatures* bitstream) { |
| 156 | VP8StatusCode status; |
Pascal Massimino | f0b65c9 | 2014-04-26 01:10:52 -0700 | [diff] [blame] | 157 | WebPBitstreamFeatures local_features; |
James Zern | 4a0e739 | 2014-04-22 19:33:22 -0700 | [diff] [blame] | 158 | if (!ExUtilReadFile(in_file, data, data_size)) return 0; |
| 159 | |
Pascal Massimino | f0b65c9 | 2014-04-26 01:10:52 -0700 | [diff] [blame] | 160 | if (bitstream == NULL) { |
| 161 | bitstream = &local_features; |
| 162 | } |
| 163 | |
James Zern | 4a0e739 | 2014-04-22 19:33:22 -0700 | [diff] [blame] | 164 | status = WebPGetFeatures(*data, *data_size, bitstream); |
| 165 | if (status != VP8_STATUS_OK) { |
| 166 | free((void*)*data); |
| 167 | *data = NULL; |
| 168 | *data_size = 0; |
| 169 | ExUtilPrintWebPError(in_file, status); |
| 170 | return 0; |
| 171 | } |
| 172 | return 1; |
| 173 | } |
| 174 | |
James Zern | 1b2fe14 | 2014-04-26 14:26:13 -0700 | [diff] [blame] | 175 | //------------------------------------------------------------------------------ |
| 176 | |
James Zern | 4a0e739 | 2014-04-22 19:33:22 -0700 | [diff] [blame] | 177 | VP8StatusCode ExUtilDecodeWebP(const uint8_t* const data, size_t data_size, |
James Zern | 1b2fe14 | 2014-04-26 14:26:13 -0700 | [diff] [blame] | 178 | int verbose, WebPDecoderConfig* const config) { |
James Zern | 4a0e739 | 2014-04-22 19:33:22 -0700 | [diff] [blame] | 179 | Stopwatch stop_watch; |
| 180 | VP8StatusCode status = VP8_STATUS_OK; |
James Zern | 1b2fe14 | 2014-04-26 14:26:13 -0700 | [diff] [blame] | 181 | if (config == NULL) return VP8_STATUS_INVALID_PARAM; |
James Zern | 4a0e739 | 2014-04-22 19:33:22 -0700 | [diff] [blame] | 182 | |
James Zern | 1b2fe14 | 2014-04-26 14:26:13 -0700 | [diff] [blame] | 183 | PrintAnimationWarning(config); |
James Zern | 4a0e739 | 2014-04-22 19:33:22 -0700 | [diff] [blame] | 184 | |
James Zern | 1b2fe14 | 2014-04-26 14:26:13 -0700 | [diff] [blame] | 185 | StopwatchReset(&stop_watch); |
James Zern | 4a0e739 | 2014-04-22 19:33:22 -0700 | [diff] [blame] | 186 | |
| 187 | // Decoding call. |
James Zern | 1b2fe14 | 2014-04-26 14:26:13 -0700 | [diff] [blame] | 188 | status = WebPDecode(data, data_size, config); |
| 189 | |
| 190 | if (verbose) { |
| 191 | const double decode_time = StopwatchReadAndReset(&stop_watch); |
| 192 | fprintf(stderr, "Time to decode picture: %.3fs\n", decode_time); |
| 193 | } |
| 194 | return status; |
| 195 | } |
| 196 | |
| 197 | VP8StatusCode ExUtilDecodeWebPIncremental( |
| 198 | const uint8_t* const data, size_t data_size, |
| 199 | int verbose, WebPDecoderConfig* const config) { |
| 200 | Stopwatch stop_watch; |
| 201 | VP8StatusCode status = VP8_STATUS_OK; |
| 202 | if (config == NULL) return VP8_STATUS_INVALID_PARAM; |
| 203 | |
| 204 | PrintAnimationWarning(config); |
| 205 | |
| 206 | StopwatchReset(&stop_watch); |
| 207 | |
| 208 | // Decoding call. |
| 209 | { |
James Zern | 4a0e739 | 2014-04-22 19:33:22 -0700 | [diff] [blame] | 210 | WebPIDecoder* const idec = WebPIDecode(data, data_size, config); |
| 211 | if (idec == NULL) { |
| 212 | fprintf(stderr, "Failed during WebPINewDecoder().\n"); |
| 213 | return VP8_STATUS_OUT_OF_MEMORY; |
| 214 | } else { |
| 215 | status = WebPIUpdate(idec, data, data_size); |
| 216 | WebPIDelete(idec); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if (verbose) { |
| 221 | const double decode_time = StopwatchReadAndReset(&stop_watch); |
| 222 | fprintf(stderr, "Time to decode picture: %.3fs\n", decode_time); |
| 223 | } |
| 224 | return status; |
| 225 | } |
| 226 | |
| 227 | // ----------------------------------------------------------------------------- |