'C++ Load WebP file into Array
I am attempting to load a WebP image file into an array, but the WebPGetInfo function does not seem to be working as advertised. The width and height variables are not being updated to the values of the width and height of the input image.
Provided is the code I have for saving out an array as a WebP image, which does work, and the code for loading a WebP image into an array, which does not work. Included are comments to show prior unsuccessful attempts, as well as next steps.
If someone could let me know either what the error is or if there is a better function to use that would be greatly appreciated.
#include "bitmap.hh"
#include <webp/decode.h>
#include <webp/encode.h>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <random>
#include "webp/mux_types.h"
using namespace std;
bitmap::bitmap(uint32_t w, uint32_t h, uint32_t color)
: w(w), h(h), rgb(new uint32_t[w * h]) {
for (uint32_t i = 0; i < w * h; i++) rgb[i] = color;
}
bitmap::~bitmap() { delete[] rgb; }
void bitmap::clear(uint32_t color) {
for (uint32_t i = 0; i < w * h; i++) rgb[i] = color;
}
void bitmap::save(const char filename[]) {
uint8_t* out;
size_t s = WebPEncodeRGBA((uint8_t*)rgb, w, h, 4 * w, 100, &out);
ofstream f(filename, ios::binary);
f.write((char*)out, s);
WebPFree(out);
}
void bitmap::load(const char filename[]) {
// int s = WebPGetInfo(rgb, w * h * 4, w, h, out); potentially?
cout << '\n';
cout << "load function" << '\n';
// https://www.tutorialspoint.com/how-can-i-get-a-file-s-size-in-cplusplus
ifstream in_file(filename, ios::binary);
in_file.seekg(0, ios::end);
int file_size = in_file.tellg();
cout << "Size of the file is"
<< " " << file_size << " "
<< "bytes" << '\n';
uint8_t* out = new uint8_t[file_size];
in_file.read((char*)out, file_size);
ofstream out_file("out_test.webp", ios::binary);
out_file.write((char*)out, file_size);
//
// skip the header
// https://developers.google.com/speed/webp/docs/riff_container
// header is presumed to be 12 bytes
uint8_t skip = 12;
// https://developers.google.com/speed/webp/docs/api
// attempts to get the width and height of the input image, but the cout
// shows that WebPGetInfo does not work as expected
// attempted using WebPDecodeRGBA but no luck there either
int width = 0;
int height = 0;
int s = WebPGetInfo(&out[skip], file_size, &width, &height);
// use WebPDecodeRGBA maybe?
cout << file_size << ' ' << width << ' ' << height << ' ' << s << '\n';
// prints out the elements in the out array, but most entries are blank or
// garbage, with no elements being from "RIFF" item in header as expected
cout << "printing array" << '\n';
for (int i = 0; i < 20; i++) {
cout << "i = " << i << ' ' << out[i] << '\n';
}
// The width and height had not been updating properly. However, the following steps are
// what the code should do if the functions above worked as expected:
/*
w = width;
h = height;
uint8_t pixels_split = WebPGetInfo(&out[skip], file_size, &width, &height);
uint32_t pixels_merge[file_size];
for (int i = 0; i < file_size; i+=4) {
uint8_t r = pixels_split[i];
uint8_t g = pixels_split[i+1];
uint8_t b = pixels_split[i+2];
uint8_t T = pixels_split[i+3];
pixels_merge[i] = r *(256*256*256) + g *(256*256) + b *256 + T3;
}
rgb = pixels_merge;
(do something to alter the image)
bitmap::save("test_out.webp");
*/
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
