Skip to content

Commit

Permalink
Adding exercices
Browse files Browse the repository at this point in the history
  • Loading branch information
StePoli-00 committed Jul 28, 2023
1 parent cf785ca commit 6889dc6
Show file tree
Hide file tree
Showing 281 changed files with 902,683 additions and 0 deletions.
Binary file added Esami/Esame 1/CR-MONO1-10-chest.pgm
Binary file not shown.
19 changes: 19 additions & 0 deletions Esami/Esame 1/frog_bin.pgm

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions Esami/Esame 1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "pgm16.h"

int main(int argc, char** argv) {

if (argc != 2) {
return 1;
}
mat<uint16_t> img;
uint16_t maxvalue;
std::string filename(argv[1]);
bool ret=load(filename, img, maxvalue);
return ret;
}
46 changes: 46 additions & 0 deletions Esami/Esame 1/mat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include <vector>
#include <cassert>

template <typename T>
class mat {
int rows_, cols_;
std::vector<T> data_;
public:
mat(int rows = 0, int cols = 0) {
resize(rows, cols);
}

void resize(int rows, int cols) {
assert(rows >= 0 && cols >= 0);
rows_ = rows;
cols_ = cols;
data_.resize(rows*cols);
}

int rows() const { return rows_; }
int cols() const { return cols_; }

T& operator()(int r, int c) {
assert(r >= 0 && c >= 0 && r < rows_ && c < cols_);
return data_[r*cols_ + c];
}
const T& operator()(int r, int c) const {
assert(r >= 0 && c >= 0 && r < rows_ && c < cols_);
return data_[r*cols_ + c];
}

T* data() { return data_.data(); }
const T* data() const { return data_.data(); }
int size() const { return rows_ * cols_; }

char* rawdata() { return reinterpret_cast<char*>(data_.data()); }
const char* rawdata() const { return reinterpret_cast<const char*>(data_.data()); }
int rawsize() const { return rows_ * cols_ * sizeof(T); }

auto begin() { return data_.begin(); }
auto begin() const { return data_.begin(); }
auto end() { return data_.end(); }
auto end() const { return data_.end(); }
};
73 changes: 73 additions & 0 deletions Esami/Esame 1/pgm16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "pgm16.h"
#include <sstream>
#include <intrin.h>
#include <fstream>
bool load(const std::string& filename, mat<uint16_t>& img, uint16_t& maxvalue) {

if (filename.empty())
{
return 1;
}

int H, W;
uint16_t ch;
std::string comment, magic_number,line;
//std::ifstream is(filename, std::ios::binary);
std::ifstream is(filename, std::ios::binary);
if (!is)
{
return false;
}

std::getline(is, magic_number);

if (magic_number != "P5")
{
return false;
}
std::getline(is, line); //commento
if (line[0] != '#')
{
return false;
}
if (!(is >> W))
{
return false;
}
if (!(is >> H))
{
return false;
}
if (!(is >> maxvalue))
{
return false;
}

img.resize(H, W);
is.get();
for (int r = 0; r < img.rows(); r++)
{
for (int c = 0; c < img.cols(); c++)
{
if (maxvalue < 256) {

img(r, c) = is.get();
}
else
{
is.read(reinterpret_cast<char*>(&ch),sizeof(uint16_t));
uint8_t b1 = (ch >> 8) & 0xFF;
uint8_t b2 = ch & 0xFF;
img(r, c) = (b2<<8)+b1;
//oppure
//img(r, c) = _byteswap_ushort(ch);


}

}
}

return true;

}
4 changes: 4 additions & 0 deletions Esami/Esame 1/pgm16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include <string>
#include "mat.h"
bool load(const std::string& filename, mat<uint16_t>& img, uint16_t& maxvalue);
Loading

0 comments on commit 6889dc6

Please sign in to comment.