WebP API 문서

이 섹션에서는 WebP 라이브러리로 이동해야 합니다. 이 API 설명은 1.4.0.

헤더 및 라이브러리

libwebp설치하면 이름이 webp/인 디렉터리가 생성됩니다. 플랫폼의 일반적인 위치에 설치됩니다. 예를 들어 Unix 플랫폼에서는 다음 헤더 파일이 /usr/local/include/webp/

decode.h
encode.h
types.h

라이브러리는 일반 라이브러리 디렉터리에 있습니다. static 및 동적 라이브러리는 Unix 플랫폼의 /usr/local/lib/에 있습니다.

간단한 디코딩 API

디코딩 API를 사용하여 시작하려면 라이브러리 및 헤더 파일을 설명한 대로 설치합니다. 에서 확인하세요.

다음과 같이 C/C++ 코드에 디코딩 API 헤더를 포함합니다.

#include "webp/decode.h"
int WebPGetInfo(const uint8_t* data, size_t data_size, int* width, int* height);

이 함수는 WebP 이미지 헤더의 유효성을 검사하고 이미지 너비를 가져옵니다. 지정할 수 있습니다. 포인터 *width*height는 포인터로 간주되는 경우 NULL를 전달할 수 있습니다. 관련성이 없습니다.

입력 속성

데이터
WebP 이미지 데이터를 가리키는 포인터
data_size
다음을 포함하는 data가 가리키는 메모리 블록의 크기입니다. 이미지 데이터입니다.

반환 값

거짓
(a) 형식 오류가 발생한 경우 반환되는 오류 코드입니다.
true
성공 시 *width*height는 반환이 성공하는 경우에만 유효합니다.
너비
정수 값입니다. 범위는 1~16,383으로 제한됩니다.
높이
정수 값입니다. 범위는 1에서 16383까지 제한됩니다.
struct WebPBitstreamFeatures {
  int width;          // Width in pixels.
  int height;         // Height in pixels.
  int has_alpha;      // True if the bitstream contains an alpha channel.
  int has_animation;  // True if the bitstream is an animation.
  int format;         // 0 = undefined (/mixed), 1 = lossy, 2 = lossless
}

VP8StatusCode WebPGetFeatures(const uint8_t* data,
                              size_t data_size,
                              WebPBitstreamFeatures* features);

이 함수는 비트스트림에서 특성을 검색합니다. *features 구조는 비트스트림에서 수집된 정보로 채워집니다.

입력 속성

데이터
WebP 이미지 데이터를 가리키는 포인터
data_size
다음을 포함하는 data가 가리키는 메모리 블록의 크기입니다. 이미지 데이터입니다.

반환 값

VP8_STATUS_OK
특성을 성공적으로 가져온 경우.
VP8_STATUS_NOT_ENOUGH_DATA
헤더에서 특성을 가져오기 위해 더 많은 데이터가 필요한 경우

그 밖의 경우에는 추가 VP8StatusCode 오류 값입니다.

기능
WebBitstreamFeatures 구조를 가리키는 포인터입니다.
uint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size, int* width, int* height);
uint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size, int* width, int* height);
uint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size, int* width, int* height);
uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size, int* width, int* height);
uint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size, int* width, int* height);

이러한 함수는 data가 가리키는 WebP 이미지를 디코딩합니다.

  • WebPDecodeRGBA[r0, g0, b0, a0, r1, g1, b1, a1, ...] 순서로 RGBA 이미지 샘플을 반환합니다.
  • WebPDecodeARGB[a0, r0, g0, b0, a1, r1, g1, b1, ...] 순서로 ARGB 이미지 샘플을 반환합니다.
  • WebPDecodeBGRA는 BGRA 이미지 샘플을 [b0, g0, r0, a0, b1, g1, r1, a1, ...] 순서로 반환합니다.
  • WebPDecodeRGB[r0, g0, b0, r1, g1, b1, ...] 순서로 RGB 이미지 샘플을 반환합니다.
  • WebPDecodeBGR는 BGR 이미지 샘플을 [b0, g0, r0, b1, g1, r1, ...] 순서로 반환합니다.

이러한 함수를 호출하는 코드는 데이터 버퍼를 삭제해야 함 WebPFree()와 함께 이러한 함수에서 반환된 (uint8_t*)입니다.

입력 속성

데이터
WebP 이미지 데이터를 가리키는 포인터
data_size
다음을 포함하는 data가 가리키는 메모리 블록의 크기입니다. 이미지 데이터
너비
정수 값입니다. 범위는 현재 1부터 16,383까지로 제한됩니다.
높이
정수 값입니다. 범위는 현재 1에서 16383까지로 제한됩니다.

반환 값

uint8_t*
선형 RGBA/ARGB/BGRA/RGB/BGR에서 디코딩된 WebP 이미지 샘플을 가리키는 포인터 입니다.
uint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size,
                            uint8_t* output_buffer, int output_buffer_size, int output_stride);
uint8_t* WebPDecodeARGBInto(const uint8_t* data, size_t data_size,
                            uint8_t* output_buffer, int output_buffer_size, int output_stride);
uint8_t* WebPDecodeBGRAInto(const uint8_t* data, size_t data_size,
                            uint8_t* output_buffer, int output_buffer_size, int output_stride);
uint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size,
                           uint8_t* output_buffer, int output_buffer_size, int output_stride);
uint8_t* WebPDecodeBGRInto(const uint8_t* data, size_t data_size,
                           uint8_t* output_buffer, int output_buffer_size, int output_stride);

이러한 함수는 위 함수의 변형이며 이미지를 직접 디코딩합니다. 사전 할당된 버퍼 output_buffer에 전달됩니다. 이 버퍼는 output_buffer_size로 표시됩니다. 이 스토리지가 충분한 경우 (또는 오류가 발생한 경우) NULL가 반환됩니다. 그렇지 않으면 편의를 위해 output_buffer가 반환됩니다.

output_stride 매개변수는 다음 값 사이의 거리 (바이트 단위)를 지정합니다. 스캔라인입니다. 따라서 output_buffer_size은(는) 최소 output_stride * picture - height

입력 속성

데이터
WebP 이미지 데이터를 가리키는 포인터
data_size
다음을 포함하는 data가 가리키는 메모리 블록의 크기입니다. 이미지 데이터
output_buffer_size
정수 값입니다. 할당된 버퍼의 크기
output_stride
정수 값입니다. 스캔라인 사이의 거리를 지정합니다.

반환 값

output_buffer
디코딩된 WebP 이미지를 가리키는 포인터
uint8_t*
함수가 성공하면 output_buffer, 그 외에는 NULL입니다.

고급 디코딩 API

WebP 디코딩은 고급 API를 지원하여 자르기 및 크기 조정은 메모리가 제한된 경우에 매우 유용합니다. 중요한 역할을 할 것입니다. 기본적으로 메모리 사용량은 출력 크기, 즉 빠른 미리보기나 확대하여 축소합니다. 일부 CPU 저장 가능 우연히도 말이죠

WebP 디코딩은 전체 이미지 디코딩과 증분의 두 가지 변형으로 제공됩니다. 디코딩하는 방법을 보여줍니다. 사용자는 선택적으로 디코딩하는 데 사용될 수 있습니다. 다음 코드 샘플은 고급 디코딩 API 사용 단계를 설명합니다.

먼저 구성 객체를 초기화해야 합니다.

#include "webp/decode.h"

WebPDecoderConfig config;
CHECK(WebPInitDecoderConfig(&config));

// One can adjust some additional decoding options:
config.options.no_fancy_upsampling = 1;
config.options.use_scaling = 1;
config.options.scaled_width = scaledWidth();
config.options.scaled_height = scaledHeight();
// etc.

디코딩 옵션은 WebPDecoderConfig 내에 수집됩니다. 구조:

struct WebPDecoderOptions {
  int bypass_filtering;             // if true, skip the in-loop filtering
  int no_fancy_upsampling;          // if true, use faster pointwise upsampler
  int use_cropping;                 // if true, cropping is applied first 
  int crop_left, crop_top;          // top-left position for cropping.
                                    // Will be snapped to even values.
  int crop_width, crop_height;      // dimension of the cropping area
  int use_scaling;                  // if true, scaling is applied afterward
  int scaled_width, scaled_height;  // final resolution
  int use_threads;                  // if true, use multi-threaded decoding
  int dithering_strength;           // dithering strength (0=Off, 100=full)
  int flip;                         // if true, flip output vertically
  int alpha_dithering_strength;     // alpha dithering strength in [0..100]
};

선택적으로 비트스트림 기능을 config.input로 읽을 수 있습니다. 미리 알아두어야 할 경우를 위해 준비한 것입니다 예를 들어 어느 정도 투명해야 합니다. 이렇게 하면 비트스트림의 헤더를 파싱하므로 비트스트림이 유효한 WebP처럼 보이는지 확인합니다.

CHECK(WebPGetFeatures(data, data_size, &config.input) == VP8_STATUS_OK);

그런 다음 디코딩 메모리 버퍼를 제공하려면 디코딩 메모리 버퍼를 설정해야 합니다 디코더에 의존하지 않고 직접 구현될 수도 있습니다. 이제 전체 버퍼 크기뿐만 아니라 메모리에 대한 포인터를 제공하고 선 스트라이드 (스캔라인 사이의 바이트 단위 거리)입니다.

// Specify the desired output colorspace:
config.output.colorspace = MODE_BGRA;
// Have config.output point to an external buffer:
config.output.u.RGBA.rgba = (uint8_t*)memory_buffer;
config.output.u.RGBA.stride = scanline_stride;
config.output.u.RGBA.size = total_size_of_the_memory_buffer;
config.output.is_external_memory = 1;

이미지를 디코딩할 준비가 되었습니다. 디코딩에는 두 가지 가능한 변형이 있습니다. 이미지 다음을 사용하여 한 번에 이미지를 디코딩할 수 있습니다.

CHECK(WebPDecode(data, data_size, &config) == VP8_STATUS_OK);

또는 증분 메서드를 사용하여 디코더를 점진적으로 디코딩할 수 있습니다. 새 바이트를 사용할 수 있게 되면 이미지를 삭제합니다.

WebPIDecoder* idec = WebPINewDecoder(&config.output);
CHECK(idec != NULL);
while (additional_data_is_available) {
  // ... (get additional data in some new_data[] buffer)
  VP8StatusCode status = WebPIAppend(idec, new_data, new_data_size);
  if (status != VP8_STATUS_OK && status != VP8_STATUS_SUSPENDED) {
    break;
  }
  // The above call decodes the current available buffer.
  // Part of the image can now be refreshed by calling
  // WebPIDecGetRGB()/WebPIDecGetYUVA() etc.
}
WebPIDelete(idec);  // the object doesn't own the image memory, so it can
                    // now be deleted. config.output memory is preserved.

디코딩된 이미지가 이제 config.output에 있습니다 (또는 config.output.u.RGBA에 있음). 이 경우 요청된 출력 색상 공간이 MODE_BGRA이었기 때문입니다. 이미지 사용 가능 다른 방식으로 저장, 표시 또는 처리되지 않을 수 있습니다. 이후에는 구성 객체에 할당된 메모리만 회수하면 됩니다. 메모리가 외부에 있거나 WebPDecode()에 의해 할당:

WebPFreeDecBuffer(&config.output);

이 API를 사용하면 이미지를 YUV 및 YUVA 형식으로 디코딩할 수도 있습니다. 각각 MODE_YUVMODE_YUVA입니다. 이 형식은 Y'CbCr의 하위 클래스.

단순 인코딩 API

RGBA 샘플의 배열을 인코딩하는 데 매우 간단한 몇 가지 함수가 제공됨 가장 일반적인 레이아웃으로 표시됩니다. webp/encode.h에 선언됩니다. 헤더를

size_t WebPEncodeRGB(const uint8_t* rgb, int width, int height, int stride, float quality_factor, uint8_t** output);
size_t WebPEncodeBGR(const uint8_t* bgr, int width, int height, int stride, float quality_factor, uint8_t** output);
size_t WebPEncodeRGBA(const uint8_t* rgba, int width, int height, int stride, float quality_factor, uint8_t** output);
size_t WebPEncodeBGRA(const uint8_t* bgra, int width, int height, int stride, float quality_factor, uint8_t** output);

품질 계수 quality_factor의 범위는 0에서 100 사이이며 압축 중 손실과 품질을 제어합니다. 값 0은 출력 크기가 작으며 100이 가장 높은 품질이고 출력 크기입니다. 성공하면 압축된 바이트가 *output에 배치됩니다. 바이트 단위의 크기가 반환됩니다. 그렇지 않으면 0이 반환됩니다. 정의합니다. 호출자는 *output에서 WebPFree()를 호출해야 합니다. 메모리 회수를 위한 포인터입니다.

입력 배열은 패킹된 바이트 배열 (각 채널당 하나씩, 와 같은 텍스트일 수 있습니다. stride는 한 행에서 다음 행으로 이동하는 데 필요한 바이트 수입니다. 예를 들면 다음과 같습니다. BGRA 레이아웃은 다음과 같습니다.

무손실 인코딩을 위한 동등한 함수(서명 포함)가 있습니다.

size_t WebPEncodeLosslessRGB(const uint8_t* rgb, int width, int height, int stride, uint8_t** output);
size_t WebPEncodeLosslessBGR(const uint8_t* bgr, int width, int height, int stride, uint8_t** output);
size_t WebPEncodeLosslessRGBA(const uint8_t* rgba, int width, int height, int stride, uint8_t** output);
size_t WebPEncodeLosslessBGRA(const uint8_t* bgra, int width, int height, int stride, uint8_t** output);

손실 버전과 같은 이러한 함수는 라이브러리의 기본값을 사용합니다. 설정을 변경할 수 있습니다. 무손실의 경우 이는 '정확'함을 의미합니다. 이(가) 사용 중지되었습니다. RGB 값 투명한 영역이 수정되어 압축을 개선할 수 있습니다. 이를 방지하려면 WebPEncode()하고 WebPConfig::exact1로 설정합니다.

고급 인코딩 API

내부적으로 인코더에는 여러 고급 인코딩 매개변수가 포함되어 있습니다. 압축 간의 균형을 더 잘 맞추는 데 유용할 수 있습니다. 처리 시간을 단축할 수 있습니다. 이러한 매개변수는 WebPConfig 구조 내에서 수집됩니다. 이 구조에서 가장 많이 사용되는 필드는 다음과 같습니다.

struct WebPConfig {
  int lossless;           // Lossless encoding (0=lossy(default), 1=lossless).
  float quality;          // between 0 and 100. For lossy, 0 gives the smallest
                          // size and 100 the largest. For lossless, this
                          // parameter is the amount of effort put into the
                          // compression: 0 is the fastest but gives larger
                          // files compared to the slowest, but best, 100.
  int method;             // quality/speed trade-off (0=fast, 6=slower-better)

  WebPImageHint image_hint;  // Hint for image type (lossless only for now).

  // Parameters related to lossy compression only:
  int target_size;        // if non-zero, set the desired target size in bytes.
                          // Takes precedence over the 'compression' parameter.
  float target_PSNR;      // if non-zero, specifies the minimal distortion to
                          // try to achieve. Takes precedence over target_size.
  int segments;           // maximum number of segments to use, in [1..4]
  int sns_strength;       // Spatial Noise Shaping. 0=off, 100=maximum.
  int filter_strength;    // range: [0 = off .. 100 = strongest]
  int filter_sharpness;   // range: [0 = off .. 7 = least sharp]
  int filter_type;        // filtering type: 0 = simple, 1 = strong (only used
                          // if filter_strength > 0 or autofilter > 0)
  int autofilter;         // Auto adjust filter's strength [0 = off, 1 = on]
  int alpha_compression;  // Algorithm for encoding the alpha plane (0 = none,
                          // 1 = compressed with WebP lossless). Default is 1.
  int alpha_filtering;    // Predictive filtering method for alpha plane.
                          //  0: none, 1: fast, 2: best. Default if 1.
  int alpha_quality;      // Between 0 (smallest size) and 100 (lossless).
                          // Default is 100.
  int pass;               // number of entropy-analysis passes (in [1..10]).

  int show_compressed;    // if true, export the compressed picture back.
                          // In-loop filtering is not applied.
  int preprocessing;      // preprocessing filter (0=none, 1=segment-smooth)
  int partitions;         // log2(number of token partitions) in [0..3]
                          // Default is set to 0 for easier progressive decoding.
  int partition_limit;    // quality degradation allowed to fit the 512k limit on
                          // prediction modes coding (0: no degradation,
                          // 100: maximum possible degradation).
  int use_sharp_yuv;      // if needed, use sharp (and slow) RGB->YUV conversion
};

이러한 매개변수 대부분은 실험을 위해 액세스할 수 있습니다. cwebp 명령줄 도구 사용

입력 샘플은 WebPPicture 구조로 래핑해야 합니다. 이 구조는 입력 샘플을 RGBA 또는 YUVA 형식으로 저장할 수 있습니다. use_argb 플래그의 값

구조는 다음과 같이 구성됩니다.

struct WebPPicture {
  int use_argb;              // To select between ARGB and YUVA input.

  // YUV input, recommended for lossy compression.
  // Used if use_argb = 0.
  WebPEncCSP colorspace;     // colorspace: should be YUVA420 or YUV420 for now (=Y'CbCr).
  int width, height;         // dimensions (less or equal to WEBP_MAX_DIMENSION)
  uint8_t *y, *u, *v;        // pointers to luma/chroma planes.
  int y_stride, uv_stride;   // luma/chroma strides.
  uint8_t* a;                // pointer to the alpha plane
  int a_stride;              // stride of the alpha plane

  // Alternate ARGB input, recommended for lossless compression.
  // Used if use_argb = 1.
  uint32_t* argb;            // Pointer to argb (32 bit) plane.
  int argb_stride;           // This is stride in pixels units, not bytes.

  // Byte-emission hook, to store compressed bytes as they are ready.
  WebPWriterFunction writer;  // can be NULL
  void* custom_ptr;           // can be used by the writer.

  // Error code for the latest error encountered during encoding
  WebPEncodingError error_code;
};

이 구조에는 압축된 바이트를 내보낼 때 사용할 수 있게 됩니다 인메모리 작성기의 예는 아래를 참조하세요. 다른 작성기는 데이터를 파일에 직접 저장할 수 있습니다 (자세한 내용은 examples/cwebp.c).

고급 API를 사용하여 인코딩하는 일반적인 흐름은 다음과 같습니다. 있습니다.

먼저 압축 매개변수입니다. 같은 구성을 사용하여 몇 개의 다른 이미지를 나중에 압축하는 데 사용될 수도 있습니다

#include "webp/encode.h"

WebPConfig config;
if (!WebPConfigPreset(&config, WEBP_PRESET_PHOTO, quality_factor)) return 0;   // version error

// Add additional tuning:
config.sns_strength = 90;
config.filter_sharpness = 6;
config.alpha_quality = 90;
config_error = WebPValidateConfig(&config);  // will verify parameter ranges (always a good habit)

그런 다음 입력 샘플을 WebPPicture로 참조해야 합니다. 참조 또는 사본으로 제출할 수 있습니다. 다음은 확인할 수 있습니다 하지만 손쉽게 이미 할당된 샘플 배열입니다. WebPPictureView() 함수를 참고하세요.

// Setup the input data, allocating a picture of width x height dimension
WebPPicture pic;
if (!WebPPictureInit(&pic)) return 0;  // version error
pic.width = width;
pic.height = height;
if (!WebPPictureAlloc(&pic)) return 0;   // memory error

// At this point, 'pic' has been initialized as a container, and can receive the YUVA or RGBA samples.
// Alternatively, one could use ready-made import functions like WebPPictureImportRGBA(), which will take
// care of memory allocation. In any case, past this point, one will have to call WebPPictureFree(&pic)
// to reclaim allocated memory.

압축된 바이트를 방출하기 위해 새 바이트가 생성될 때마다 후크가 호출됩니다. 있습니다. 다음은 메모리 작성기가 선언된 간단한 예입니다. webp/encode.h 이 초기화는 압축할 각 사진:

// Set up a byte-writing method (write-to-memory, in this case):
WebPMemoryWriter writer;
WebPMemoryWriterInit(&writer);
pic.writer = WebPMemoryWrite;
pic.custom_ptr = &writer;

이제 입력 샘플을 압축하고 이후 메모리를 확보할 준비가 되었습니다.

int ok = WebPEncode(&config, &pic);
WebPPictureFree(&pic);   // Always free the memory associated with the input.
if (!ok) {
  printf("Encoding error: %d\n", pic.error_code);
} else {
  printf("Output size: %d\n", writer.size);
}

API 및 구조의 고급 사용에 대해서는 이는 webp/encode.h 헤더에 제공되는 문서를 참조하세요. 예시 코드 examples/cwebp.c를 읽어보면 도움이 될 수 있습니다. 덜 사용되는 매개변수를 탐색할 수 있습니다