C++ returning multidimensional array - c++
This is a school project that I'm currently working on. I have a problem with returning the multidimensional array Map[25][60]. The method get_map() has to return the whole map so I can use it as a parameter in the function move_left().
// ProjectX v3.0.cpp : Defines the entry point for the console application.
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <thread>
#include <fstream>
using namespace std;
class wallz
{
int i;
int j;
public:
wallz();
~wallz();
void set_i(int def_i);
void set_j(int def_j);
int get_i();
int get_j();
void move_left(int Map[25][60], wallz wallz_temp);
};
wallz::wallz()
{
}
wallz::~wallz()
{
}
void wallz::set_i(int def_i)
{
i = def_i;
}
void wallz::set_j(int def_j)
{
j = def_j;
}
int wallz::get_i()
{
return i;
}
int wallz::get_j()
{
return j;
}
void wallz::move_left(int Map[25][60], wallz wallz_temp)
{
int x = 0;
while (x == 0)
{
if (Map[wallz_temp.get_i()][wallz_temp.get_j()] == 11)
{
Map[wallz_temp.get_i()][wallz_temp.get_j()] = 0;
Map[wallz_temp.get_i() - 1][wallz_temp.get_j()] = 11;
}
if (Map[wallz_temp.get_i() - 1][wallz_temp.get_j()] == 11)
{
Map[wallz_temp.get_i()][wallz_temp.get_j()] = 11;
Map[wallz_temp.get_i() - 1][wallz_temp.get_j()] = 0;
}
}
}
class map
{
wallz wallz_mass[30];
int char_X;
int char_Y;
int i;
int j;
int Map[25][60];
public:
map(int Map[25][60]);
~map();
void map_print();
void controlz();
int get_map();
wallz get_wallz(int h);
void map1_set_wallz();
void map2_set_wallz();
void map3_set_wallz();
void map4_set_wallz();
void map5_set_wallz();
void move_left();
void move_right();
void move_horizontal();
void move_up();
void move_down();
void move_vertical();
};
map::map(int m[25][60])
{
char_X = 1;
char_Y = 1;
for (int i = 0; i<25; i++)
for (int j = 0; j<60; j++)
Map[i][j] = m[i][j];
}
map::~map()
{
}
void map::map_print()
{
int MapCounter = 0;
for (int i = 0; i<25; i++)
{
for (int j = 0; j<60; j++)
{
if (MapCounter == 60)
{
cout << "" << endl;
MapCounter = 0;
}
if (i == char_X && j == char_Y)
{
cout << char(35);
}
else if (Map[i][j] == 1)
{
cout << char(186);//vertikalna liniq
}
else if (Map[i][j] == 0)
{
cout << " ";
}
else if (Map[i][j] == 2)//horizontalna liniq
{
cout << char(205);
}
else if (Map[i][j] == 3)
{
cout << char(200);//dolen lqv ugul
}
else if (Map[i][j] == 4)
{
cout << char(201);//goren lqv ugul
}
else if (Map[i][j] == 5)
{
cout << char(188);//dolen desen ugul
}
else if (Map[i][j] == 6)
{
cout << char(187);//goren desen ugul
}
else if (Map[i][j] == 7)
{
cout << char(204);//lqv vodoprovod
}
else if (Map[i][j] == 8)
{
cout << char(185);//desen vodoprovod
}
else if (Map[i][j] == 9)
{
cout << char(202);//goren vodoprovod
}
else if (Map[i][j] == 10)
{
cout << char(203);//desen vodoprovod
}
else if (Map[i][j] == 11)
{
cout << char(254); //
}
MapCounter++;
}
}
}
void map::controlz()
{
if (GetAsyncKeyState(VK_UP) != 0)
{
if (Map[char_X - 1][char_Y] == 0)
{
char_X--;
}
}
if (GetAsyncKeyState(VK_DOWN) != 0)
{
if (Map[char_X + 1][char_Y] == 0)
{
char_X++;
}
}
if (GetAsyncKeyState(VK_LEFT) != 0)
{
if (Map[char_X][char_Y - 1] == 0)
{
char_Y--;
}
}
if (GetAsyncKeyState(VK_RIGHT) != 0)
{
if (Map[char_X][char_Y + 1] == 0)
{
char_Y++;
}
}
}
int map::get_map()
{
return Map[25][60];
}
wallz map::get_wallz(int h)
{
return wallz_mass[h];
}
void map::map1_set_wallz()
{
wallz_mass[0].set_i(6);
wallz_mass[0].set_j(2);
}
void move_left(map map1)
{
map1.get_wallz(0).move_left(map1.get_map(), map1.get_wallz(0));
}
int main()
{
int x, y;
COORD ord;
ord.X = x = 0;
ord.Y = y = 0;
system("cls");
int Map[25][60] = {
4, 2, 6, 0, 0, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 0, 0, 0, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,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 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 7, 2, 2, 11, 2, 2, 2, 2, 2, 2, 2, 2, 9, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 9, 2, 2, 2, 2, 2, 2, 9, 2, 6, 2, 2, 2, 6, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1,
0, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 9, 2, 2, 2, 9, 2, 2, 2, 5,
};
map map1(Map);
bool stop = false;
while (stop == false)
{
map1.controlz();
map1.map_print();
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ord);
}
thread first(move_left, map1);
first.join();
system("pause>nul");
return 0;
}
from here
In C++, it is not possible to pass the entire block of memory
represented by an array to a function directly as an argument. But
what can be passed instead is its address. In practice, this has
almost the same effect, and it is a much faster and more efficient
operation.
You may find this forum post helpful for learning about this exact thing. Here is a quick example on how to operate on the contents of a multidimensional array (taken from the forum).
const int MAX_ROWS = 4;
const int MAX_COLS = 4;
void printMatrix(int matrix[MAX_ROWS][MAX_COLS], int r, int c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
}
See also: How do I use arrays in c++?
Related
C++ make function return array cross file
I'm looking to make a function return an array of set size, and assign the value of that array to another array. The function is located in one file and the array I will be assigning it's return value to is located in another. I'm attempting to do this in an SFML project. Whenever I call the array function, my program freezes and stops responding. generate_maze.cpp: #include <SFML/Graphics.hpp> #include "generate_maze.h" char* generate_maze() { char test_maze[169] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; return test_maze; } extract of main.cpp: #include <stdlib.h> #include <stdio.h> #include <time.h> #include <SFML/Graphics.hpp> #include "controls.h" #include "generate_maze.h" ... char* maze = generate_maze(); std::cout << (int) maze[0] << std::endl; generate_maze.h: char* generate_maze(); Thanks in advance for the help.
The code does not work because generate_maze() returns a pointer to a local variable, which is destroyed on exiting the function. To make it work one could design generate_maze() to allocate an array of char via new, then the allocated array survives after generate_maze() is finished. But that would also require to explicitly free the memory, after char *maze is no longer used. A much better c++-like solution is the following. If the size of the array is fixed, you should better use std::array to store the result of generate_maze(), as: #include <array> std::array<char, 169> generate_maze() { return { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; } If the size of the array returned by generate_maze() is not fixed, the correct way is to use std::vector and declare generate_maze() as #include <vector> std::vector<char> generate_maze();
generate_maze returns the address of test_maze[0], which is a local variable. You should never ever return addresses of (or references to) local variables from a function. Allocate the maze on the heap instead and return that: char* generate_maze() { char local[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; char* ret = new char[sizeof local]; memcpy(ret, local, sizeof local); return ret; } Alternatively, just return a std::vector which does its own memory management. std::vector<char> generate_maze() { return {0, 0, 0, ...}; }
generate_maze() returns pointer to local array, which is destroyed when function exits. To solve this problem, you can define test_maze static: static char test_maze[]. This will create one permanent array for all generaze_maze() function calls. If you going to implement generate_maze() to return different maze arrays, better use std::vector or std::array.
Define const array in source file
I want to define a constant array as data member of class in its source file. The array has 256 elements, and there is no relation between them. class Foo { private: const int myArray[256] = { 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0, 0, 1, 3, 0, 0, 0, 0, 1, 2, 0, 0, 0, 2, 2, 1, 0, 3, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; }; My Questions: 1- Is it better to shift the definition of the array to the source file? 2- Is there anyway to define this array in a source file instead of the header that defines the class?
in the header: static const int myArray[256]; in the cpp file const int Foo::myArray[256] = { // numbers }; I'm assuming here you don't need one per class instance. Regarding the question of where it's better to put it; I would put it in the cpp file, but that's mostly a matter of style.
Nested glCallList in OpenGL c++
I am currently trying to program Pacman in C++ using OpenGL. I am currently trying to create a display list to draw the maze. My thoughts were to create a square tile and then simply display said tile if matrix indicates a wall segment at some position. My code for this is: const unsigned int FREE = 0; const unsigned int WALL = 1; const unsigned int GHOST_WALL = 2; const unsigned int matrix[22][19] = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1}, {1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1}, {1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 1, 0, 1, 1, 2, 1, 1, 0, 1, 0, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1}, {1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1}, {1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1}, {1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1}, {1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} }; GLuint tile_handle = 0; GLuint maze_handle = 0; /* * Initialize maze by precompiling its components */ void maze_init() { // request a single display list handle for the tile tile_handle = glGenLists(1); glNewList(tile_handle, GL_COMPILE); glColor3f(0, 0, 0.7f); // Blue glBegin(GL_QUADS); glVertex2i(0,0); glVertex2i(1,0); glVertex2i(1,1); glVertex2i(0,1); glEnd(); glEndList(); // request a single display list handle for the entire maze maze_handle = glGenLists(1); glNewList(maze_handle, GL_COMPILE); glColor3f(0, 0, 0.7f); // Blue for(int row = 0; row < 22; row--) { for(int col = 0; col < 19; col++) { if(matrix[21-row][col] == WALL) { glPushMatrix(); glScaled(30, 30, 1); glTranslated(col, row, 0); glCallList(tile_handle); glPopMatrix(); } } } glEndList(); } I then use maze_init() in my main initialisation method and use the call glCallList(maze_handle); in my main display function. This compiles fine but when I try to run it, it gives me a Bus error: 10 error. Now I tried the same without the loops in the definition of glNewList(maze_handle, GL_COMPILE); and it ran fine. My question is therefore this: why do the loops (or the conditional statement) inside the call list create a bus error? I tried a simple loop inside glNewList(maze_handle, GL_COMPILE); that made calls to DISTINCT display lists and it ran fine. So is the problem here that I make a repeated call to the same display list? For some reason I just can't seem to find the problem here ...
Best lossless compression technique for serializing a foot pressure map
I am working with pressure-sensing of human feet, and I need to transmit frames in realtime over serial. The typical frame is like below, consisting of a flat background and blobs of non-flat data: The speed of transmission is currently a bottleneck due to micro-controller overhead caused by Serial.send commands, so the engineer is using Run Length Encoding to compress the image, which seems good due to the flat, continuous background, but we would like to compress it even further. I tried the "Coordinate List" encoding format (List<i, j, val> where val > 0), but the size is similar enough to RLE to not make a significant difference. While researching a bit on SO, people say "don't reinvent the wheel, there are a lot of tried-and-tested compression algorithms for any kind of image", so I wonder what would be the best for the type of image displayed below, considering: Compression performance (since it is to be performed by a micro-controller); Size - since it is to be sent by serial, which is currently a bottleneck (sic). Other approach would be to use "sparse-matrix" concepts (instead of "image-compression" concepts), and it looks like there is something like CRS, or CSR, which I couldn't quite understand how to implement and how to serialize properly, and even less how it would compare with image-compression techniques. UPDATE: I created a Gist with the data I used to create the image. These were the results of compression methods (one byte per entry): plain: ([n_rows, n_columns, *data]): 2290 bytes; coordinate list: ([*(i, j, val)]): 936 bytes; run length encoding: ([*(rowlength, rle-pairs)]): 846 bytes; list of lists: 690 bytes; compact list of lists: (see Gist) 498 bytes;
Proposed algorithm Below is a possible algorithm that is using only simple operations [1] with a low memory footprint (no pun intended). It seems to work reasonably well but, of course, it should be tested on several different data sets in order to have a more precise idea of its efficiency. Divide the matrix into 13x11 blocks of 4x4 pixels For each block: If the block is empty, emit bit '0' If the block is not empty: emit bit '1' emit 16-bit bitmask of non-zero pixels in this block emit 8-bit value representing the minimum value (other than 0) found in this block if there's only one non-zero pixel, stop here [2] emit 3-bit value representing the number of bits required to encode each non-zero pixel in this block: b = ceil(log2(max + 1 - min)) emit non-zero pixel data as N x b bits It is based on the following observations: Many blocks in the matrix are empty Non-empty blocks at the frontier of the footprint usually have many empty cells (the 'pressure' / 'no pressure' transition on the sensors is abrupt) [1] There's notably no floating point operation. The log2() operation that is used in the description of the algorithm can easily be replaced by simple comparisons against 1, 2, 4, 8, 16, ... up to 256. [2] This is a minor optimization that will not trigger very often. The decoder will have to detect that there's only one bit set in the bitmask by computing for instance: (msk & -msk) == msk. Block encoding example Let's consider the following block: 0, 0, 0, 0 12, 0, 0, 0 21, 20, 0, 0 28, 23, 0, 0 The bitmask of non-zero pixels is: 0, 0, 0, 0 1, 0, 0, 0 = 0000100011001100 1, 1, 0, 0 1, 1, 0, 0 The minimum value is 12 (00001100) and the number of bits required to encode each non-zero pixel is 5 (101), as log2(28 + 1 - 12) ~= 4.09. Finally, let's encode non-zero pixels: [ 12, 21, 20, 28, 23 ] - [ 12, 12, 12, 12, 12 ] ------------------------ = [ 0, 9, 8, 16, 11 ] = [ 00000, 01001, 01000, 10000, 01011 ] So, the final encoding for this block would be: 1 0000100011001100 00001100 101 00000 01001 01000 10000 01011 which is 53 bits long (as opposed to 16 * 8 = 128 bits in uncompressed format). However, the biggest gain comes from empty blocks which are encoded as one single bit. The fact that there are many empty blocks in the matrix is an important assumption in this algorithm. Demo Here is some JS demonstration code working on your original data set: var nEmpty, nFilled; function compress(matrix) { var x, y, data = ''; nEmpty = nFilled = 0; for(y = 0; y < 44; y += 4) { for(x = 0; x < 52; x += 4) { data += compressBlock(matrix, x, y); } } console.log("Empty blocks: " + nEmpty); console.log("Filled blocks: " + nFilled); console.log("Average bits per block: " + (data.length / (nEmpty + nFilled)).toFixed(2)); console.log("Average bits per filled block: " + ((data.length - nEmpty) / nFilled).toFixed(2)); console.log("Final packed size: " + data.length + " bits --> " + ((data.length + 7) >> 3) + " bytes"); } function compressBlock(matrix, x, y) { var min = 0x100, max = 0, msk = 0, data = [], width, v, x0, y0; for(y0 = 0; y0 < 4; y0++) { for(x0 = 0; x0 < 4; x0++) { if(v = matrix[y + y0][x + x0]) { msk |= 1 << (15 - y0 * 4 - x0); data.push(v); min = Math.min(min, v); max = Math.max(max, v); } } } if(msk) { nFilled++; width = Math.ceil(Math.log(max + 1 - min) / Math.log(2)); data = data.map(function(v) { return bin(v - min, width); }).join(''); return '1' + bin(msk, 16) + bin(min, 8) + ((msk & -msk) == msk ? '' : bin(width, 3) + data); } nEmpty++; return '0'; } function bin(n, sz) { var b = n.toString(2); return Array(sz + 1 - b.length).join('0') + b; } compress([ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ]); The final output is 349 bytes long. Empty blocks: 102 Filled blocks: 41 Average bits per block: 19.50 Average bits per filled block: 65.51 Final packed size: 2788 bits --> 349 bytes
I would test JPEG-LS. It is a very fast algorithm and provides state-of-the art lossless compression results for many types of images. In particular, its prediction algorithm will provide results comparable to RLE for the flat regions, and much better results for the foot areas. Since you are transmitting several frames, and these frames are likely to be very similar, you may want to try to subtract one frame from the next before applying JPEG-LS (you will probably need to remap the pixels to positive integers before using JPEG-LS, though). If you don't need strictly lossless compression (i.e., if you can tolerate some distortion in the reconstructed images), you can test the near-lossless mode, which bounds the maximum absolute error introduced in any given pixel. You can find a very good and complete implementation here https://jpeg.org/jpegls/software.html.
Static reshape of Eigen matrix
I am experimenting with doing bicubic interpolation of some gridded data using Eigen, and I can't figure out how to reshape the 16x1 column vector of coefficients into a 4x4 matrix. Ideally I would like to do something along the lines of https://bitbucket.org/eigen/eigen/pull-request/41/reshape/diff without any copying, but I can't make heads or tails of the docs. Alternatively, a map would be fine as well, but I can't figure out how to use a map on an already existing matrix. More here: http://en.wikipedia.org/wiki/Bicubic_interpolation /// The inverse of the A matrix for the bicubic interpolation /// (http://en.wikipedia.org/wiki/Bicubic_interpolation) static const double Ainv_data[16*16] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, 3, 0, 0, -2, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, -2, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, 3, 0, 0, -2, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, -2, 0, 0, 1, 1, 0, 0, -3, 0, 3, 0, 0, 0, 0, 0, -2, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, 0, 3, 0, 0, 0, 0, 0, -2, 0, -1, 0, 9, -9, -9, 9, 6, 3, -6, -3, 6, -6, 3, -3, 4, 2, 2, 1, -6, 6, 6, -6, -3, -3, 3, 3, -4, 4, -2, 2, -2, -2, -1, -1, 2, 0, -2, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, -2, 0, 0, 0, 0, 0, 1, 0, 1, 0, -6, 6, 6, -6, -4, -2, 4, 2, -3, 3, -3, 3, -2, -1, -2, -1, 4, -4, -4, 4, 2, 2, -2, -2, 2, -2, 2, -2, 1, 1, 1, 1}; Eigen::Matrix<double, 16, 16> Ainv(Ainv_data); Eigen::Matrix<double, 16, 1> f; f.setRandom(); Eigen::Matrix<double, 16, 1> alpha = Ainv*f; // This next line works, but it is making a copy, right? Eigen::Matrix<double, 4, 4> a(alpha.data());
The last line is indeed doing a copy, so you can use a Map as follow: Map<Matrix4d,Eigen::Aligned> a(alpha.data()); a behaves like a Matrix4d and it is read-write. The Eigen::Aligned flag tells Eigen that the pointer you pass to Map is properly aligned for vectorization. The only difference with a pure Matrix4d is that the C++ type is not the same.