mirror of
https://gitlab.uni-freiburg.de/opensourcevdi/win32-vd_agent
synced 2025-12-26 21:37:53 +00:00
This allows to reuse the code to save a DIB to a file. Signed-off-by: Frediano Ziglio <fziglio@redhat.com> Acked-by: Uri Lublin <uril@redhat.com>
91 lines
2.7 KiB
C++
91 lines
2.7 KiB
C++
/*
|
|
Copyright (C) 2017 Red Hat, Inc.
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU General Public License as
|
|
published by the Free Software Foundation; either version 2 of
|
|
the License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#undef NDEBUG
|
|
#include <assert.h>
|
|
#include <vector>
|
|
|
|
#include "vdcommon.h"
|
|
#include "image.h"
|
|
#include "imagepng.h"
|
|
|
|
static void
|
|
save_dib_to_file(ImageCoder& coder, const uint8_t *raw_dib, const char *filename)
|
|
{
|
|
const BITMAPINFO& info(*(BITMAPINFO*) raw_dib);
|
|
const uint8_t *raw_bits = &raw_dib[sizeof(BITMAPINFOHEADER) + 4 * info.bmiHeader.biClrUsed];
|
|
|
|
long size = 0;
|
|
uint8_t *raw_file = coder.from_bitmap(info, raw_bits, size);
|
|
assert(raw_file && size > 0);
|
|
|
|
FILE *f = fopen(filename, "wb");
|
|
assert(f);
|
|
assert(fwrite(raw_file, 1, size, f) == (unsigned long) size);
|
|
fclose(f);
|
|
free(raw_file);
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
ImageCoder *coder = create_png_coder();
|
|
|
|
assert(coder);
|
|
if (argc < 2) {
|
|
fprintf(stderr, "Usage: %s <in-image> [<out-bmp> [<out-png>]]\n", argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
// read all file into memory
|
|
FILE *f = fopen(argv[1], "rb");
|
|
assert(f);
|
|
assert(fseek(f, 0, SEEK_END) == 0);
|
|
long len = ftell(f);
|
|
assert(len > 0);
|
|
assert(fseek(f, 0, SEEK_SET) == 0);
|
|
|
|
std::vector<uint8_t> data(len);
|
|
assert(fread(&data[0], 1, len, f) == (unsigned long) len);
|
|
fclose(f);
|
|
|
|
size_t dib_size = coder->get_dib_size(&data[0], len);
|
|
assert(dib_size);
|
|
std::vector<uint8_t> out(dib_size);
|
|
memset(&out[0], 0xcc, dib_size);
|
|
coder->get_dib_data(&out[0], &data[0], len);
|
|
|
|
// looks like many tools wants this header so craft it
|
|
BITMAPFILEHEADER head;
|
|
memset(&head, 0, sizeof(head));
|
|
head.bfType = 'B'+'M'*256u;
|
|
head.bfSize = sizeof(head) + dib_size;
|
|
BITMAPINFOHEADER& info(*(BITMAPINFOHEADER*)&out[0]);
|
|
head.bfOffBits = sizeof(head) + sizeof(BITMAPINFOHEADER) + 4 * info.biClrUsed;
|
|
|
|
f = fopen(argc > 2 ? argv[2] : "out.bmp", "wb");
|
|
assert(f);
|
|
assert(fwrite(&head, 1, sizeof(head), f) == sizeof(head));
|
|
assert(fwrite(&out[0], 1, dib_size, f) == dib_size);
|
|
fclose(f);
|
|
|
|
// convert back to PNG
|
|
save_dib_to_file(*coder, &out[0], argc > 3 ? argv[3] : "out.png");
|
|
|
|
return 0;
|
|
}
|
|
|