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 | #ifndef WEBP_EXAMPLES_EXAMPLE_UTIL_H_ |
| 14 | #define WEBP_EXAMPLES_EXAMPLE_UTIL_H_ |
| 15 | |
James Zern | 5927e15 | 2014-08-29 19:07:17 -0700 | [diff] [blame] | 16 | #include <stdio.h> |
James Zern | 8955da2 | 2014-04-28 14:56:19 -0700 | [diff] [blame] | 17 | #include "webp/decode.h" |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 18 | |
James Zern | 605a712 | 2013-11-25 14:43:12 -0800 | [diff] [blame] | 19 | #ifdef __cplusplus |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 20 | extern "C" { |
| 21 | #endif |
| 22 | |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 23 | //------------------------------------------------------------------------------ |
| 24 | // String parsing |
| 25 | |
| 26 | // Parses 'v' using strto(ul|l|d)(). If error is non-NULL, '*error' is set to |
| 27 | // true on failure while on success it is left unmodified to allow chaining of |
| 28 | // calls. An error is only printed on the first occurrence. |
| 29 | uint32_t ExUtilGetUInt(const char* const v, int base, int* const error); |
| 30 | int ExUtilGetInt(const char* const v, int base, int* const error); |
| 31 | float ExUtilGetFloat(const char* const v, int* const error); |
| 32 | |
| 33 | //------------------------------------------------------------------------------ |
| 34 | // File I/O |
| 35 | |
James Zern | 5927e15 | 2014-08-29 19:07:17 -0700 | [diff] [blame] | 36 | // Reopen file in binary (O_BINARY) mode. |
| 37 | // Returns 'file' on success, NULL otherwise. |
| 38 | FILE* ExUtilSetBinaryMode(FILE* file); |
| 39 | |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 40 | // Allocates storage for entire file 'file_name' and returns contents and size |
| 41 | // in 'data' and 'data_size'. Returns 1 on success, 0 otherwise. '*data' should |
| 42 | // be deleted using free(). |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame] | 43 | // If 'file_name' is NULL or equal to "-", input is read from stdin by calling |
| 44 | // the function ExUtilReadFromStdin(). |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 45 | int ExUtilReadFile(const char* const file_name, |
| 46 | const uint8_t** data, size_t* data_size); |
| 47 | |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame] | 48 | // Same as ExUtilReadFile(), but reads until EOF from stdin instead. |
| 49 | int ExUtilReadFromStdin(const uint8_t** data, size_t* data_size); |
| 50 | |
Urvang Joshi | e9a15a3 | 2012-11-07 14:41:15 -0800 | [diff] [blame] | 51 | // Write a data segment into a file named 'file_name'. Returns true if ok. |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame] | 52 | // If 'file_name' is NULL or equal to "-", output is written to stdout. |
Urvang Joshi | e9a15a3 | 2012-11-07 14:41:15 -0800 | [diff] [blame] | 53 | int ExUtilWriteFile(const char* const file_name, |
| 54 | const uint8_t* data, size_t data_size); |
| 55 | |
James Zern | 4a0e739 | 2014-04-22 19:33:22 -0700 | [diff] [blame] | 56 | //------------------------------------------------------------------------------ |
| 57 | // WebP decoding |
| 58 | |
| 59 | // Prints an informative error message regarding decode failure of 'in_file'. |
| 60 | // 'status' is treated as a VP8StatusCode and if valid will be printed as a |
| 61 | // text string. |
| 62 | void ExUtilPrintWebPError(const char* const in_file, int status); |
| 63 | |
| 64 | // Reads a WebP from 'in_file', returning the contents and size in 'data' and |
Pascal Massimino | f0b65c9 | 2014-04-26 01:10:52 -0700 | [diff] [blame] | 65 | // 'data_size'. If not NULL, 'bitstream' is populated using WebPGetFeatures(). |
James Zern | 4a0e739 | 2014-04-22 19:33:22 -0700 | [diff] [blame] | 66 | // Returns true on success. |
| 67 | int ExUtilLoadWebP(const char* const in_file, |
| 68 | const uint8_t** data, size_t* data_size, |
James Zern | 8955da2 | 2014-04-28 14:56:19 -0700 | [diff] [blame] | 69 | WebPBitstreamFeatures* bitstream); |
James Zern | 4a0e739 | 2014-04-22 19:33:22 -0700 | [diff] [blame] | 70 | |
James Zern | 1b2fe14 | 2014-04-26 14:26:13 -0700 | [diff] [blame] | 71 | // Decodes the WebP contained in 'data'. |
| 72 | // 'config' is a structure previously initialized by WebPInitDecoderConfig(). |
| 73 | // 'config->output' should have the desired colorspace selected. 'verbose' will |
| 74 | // cause decode timing to be reported. |
James Zern | 4a0e739 | 2014-04-22 19:33:22 -0700 | [diff] [blame] | 75 | // Returns the decoder status. On success 'config->output' will contain the |
| 76 | // decoded picture. |
James Zern | 8955da2 | 2014-04-28 14:56:19 -0700 | [diff] [blame] | 77 | VP8StatusCode ExUtilDecodeWebP(const uint8_t* const data, size_t data_size, |
| 78 | int verbose, WebPDecoderConfig* const config); |
James Zern | 4a0e739 | 2014-04-22 19:33:22 -0700 | [diff] [blame] | 79 | |
James Zern | 1b2fe14 | 2014-04-26 14:26:13 -0700 | [diff] [blame] | 80 | // Same as ExUtilDecodeWebP(), but using the incremental decoder. |
James Zern | 8955da2 | 2014-04-28 14:56:19 -0700 | [diff] [blame] | 81 | VP8StatusCode ExUtilDecodeWebPIncremental( |
James Zern | 1b2fe14 | 2014-04-26 14:26:13 -0700 | [diff] [blame] | 82 | const uint8_t* const data, size_t data_size, |
James Zern | 8955da2 | 2014-04-28 14:56:19 -0700 | [diff] [blame] | 83 | int verbose, WebPDecoderConfig* const config); |
James Zern | 1b2fe14 | 2014-04-26 14:26:13 -0700 | [diff] [blame] | 84 | |
James Zern | 605a712 | 2013-11-25 14:43:12 -0800 | [diff] [blame] | 85 | #ifdef __cplusplus |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 86 | } // extern "C" |
| 87 | #endif |
| 88 | |
| 89 | #endif // WEBP_EXAMPLES_EXAMPLE_UTIL_H_ |