PDF z cvičení (ZS 2021)
cvicenie02.pdf
Zdrojové súbory z cvičení (ZS 2021)
// Tema: assert, static_assert
// Autor: Vladislav Novak
#include <iostream>
#include <cassert>
#include <cstdint>
#include <cstdio>
using namespace std;
// ---------------------------------------------------------------------------------------------------------------------
// https://en.wikipedia.org/wiki/GIF#File_format
// https://www.file-recovery.com/gif-signature-format.htm
struct Gif {
char signature[3];
char version[3];
uint16_t width;
uint16_t height;
// ...
};
static_assert(sizeof(uint16_t) == 2 ); // uint16_t (ak je definovany) ma 16 bitov. Jeden bajt nemusi vzdy obsahovat 8 bitov.
static_assert(offsetof(Gif, version) == 3);
static_assert(offsetof(Gif, width) == 6);
static_assert(offsetof(Gif, height) == 8);
void printLine(const Gif *gif) {
assert(gif /* != nullptr */);
cout << "Gif: {signature: " << gif->signature[0] << gif->signature[1] << gif->signature[2]
<< ", version: " << gif->version[0] << gif->version[1] << gif->version[2]
<< ", width: " << gif->width
<< ", height: " << gif->height
<< "}" << endl;
}
bool read(const char *fileName, Gif *gif) {
assert(fileName /* != nullptr */);
assert(gif /* != nullptr */);
FILE *file = fopen(fileName, "rb");
if(file == nullptr) {
return false;
}
size_t readObjects = fread((void*)gif, sizeof(Gif), 1, file);
if(readObjects != 1) {
return false;
}
fclose(file);
return true;
}
int main() {
Gif image1 = {{'G', 'I', 'F'}, { '8', '7', 'a'}, 1280, 720};
printLine(&image1);
Gif *image2 = new Gif;
printLine(image2);
// Gif *image3 = nullptr;
// printLine(image3);
Gif image4;
if(read("waves.gif", &image4) == true) {
printLine(&image4);
}
else {
cerr << "chyba pri citani obrazku" << endl;
}
return EXIT_SUCCESS;
}