int main(int argc, const char *argv[]) { int c; int quiet = 0; int ok = 1; for (c = 1; ok && c < argc; ++c) { if (!strcmp(argv[c], "-quiet")) { quiet = 1; } else if (!strcmp(argv[c], "-help") || !strcmp(argv[c], "-h")) { printf("webp_quality [-h][-quiet] webp_files...\n"); return 0; } else { const char* const filename = argv[c]; const uint8_t* data = NULL; size_t data_size = 0; int q; ok = ImgIoUtilReadFile(filename, &data, &data_size); if (!ok) break; q = VP8EstimateQuality(data, data_size); if (!quiet) printf("[%s] ", filename); if (q < 0) { fprintf(stderr, "Not a WebP file, or not a lossy WebP file.\n"); ok = 0; } else { if (!quiet) { printf("Estimated quality factor: %d\n", q); } else { printf("%d\n", q); // just print the number } } free((void*)data); } } return ok ? 0 : 1; }
static HRESULT OpenInputStream(const char* filename, IStream** stream) { HRESULT hr = S_OK; if (!strcmp(filename, "-")) { const uint8_t* data = NULL; size_t data_size = 0; const int ok = ImgIoUtilReadFile(filename, &data, &data_size); if (ok) { HGLOBAL image = GlobalAlloc(GMEM_MOVEABLE, data_size); if (image != NULL) { void* const image_mem = GlobalLock(image); if (image_mem != NULL) { memcpy(image_mem, data, data_size); GlobalUnlock(image); IFS(CreateStreamOnHGlobal(image, TRUE, stream)); } else { hr = E_FAIL; } } else { hr = E_OUTOFMEMORY; } free((void*)data); } else { hr = E_FAIL; } } else { IFS(SHCreateStreamOnFileA(filename, STGM_READ, stream)); } if (FAILED(hr)) { fprintf(stderr, "Error opening input file %s (%08lx)\n", filename, hr); } return hr; }
static int ReadFileToWebPData(const char* const filename, WebPData* const webp_data) { const uint8_t* data; size_t size; if (!ImgIoUtilReadFile(filename, &data, &size)) return 0; webp_data->bytes = data; webp_data->size = size; return 1; }
static size_t ReadPicture(const char* const filename, WebPPicture* const pic, int keep_alpha) { const uint8_t* data = NULL; size_t data_size = 0; WebPImageReader reader = NULL; int ok = ImgIoUtilReadFile(filename, &data, &data_size); if (!ok) goto Error; pic->use_argb = 1; // force ARGB reader = WebPGuessImageReader(data, data_size); ok = (reader != NULL) && reader(data, data_size, pic, keep_alpha, NULL); Error: if (!ok) { fprintf(stderr, "Error! Could not process file %s\n", filename); } free((void*)data); return ok ? data_size : 0; }
int LoadWebP(const char* const in_file, const uint8_t** data, size_t* data_size, WebPBitstreamFeatures* bitstream) { VP8StatusCode status; WebPBitstreamFeatures local_features; if (!ImgIoUtilReadFile(in_file, data, data_size)) return 0; if (bitstream == NULL) { bitstream = &local_features; } status = WebPGetFeatures(*data, *data_size, bitstream); if (status != VP8_STATUS_OK) { free((void*)*data); *data = NULL; *data_size = 0; PrintWebPError(in_file, status); return 0; } return 1; }
int main(int argc, char* argv[]) { int c; int ok = 0; for (c = 1; c < argc; ++c) { const char* file = NULL; const uint8_t* webp = NULL; size_t webp_size = 0; if (!strcmp(argv[c], "-h")) { printf("Usage: %s [-h] image.webp [more_files.webp...]\n", argv[0]); return 0; } else { file = argv[c]; } if (file == NULL) continue; if (!ImgIoUtilReadFile(file, &webp, &webp_size)) { fprintf(stderr, "Error opening file: %s\n", file); goto Error; } if (webp_size != (size_t)(int)webp_size) { fprintf(stderr, "File too large.\n"); goto Error; } ok = WebpToSDL((const char*)webp, (int)webp_size); free((void*)webp); if (!ok) { fprintf(stderr, "Error decoding file %s\n", file); goto Error; } ProcessEvents(); } ok = 1; Error: SDL_Quit(); return ok ? 0 : 1; }