Image Dividing in C for DCT - c

Can anyone please tell us how to divide the image into 8X8 blocks?
I can read the image, but not divide it into an 8x8 submatrix for DCT.
int main()
{
FILE *image_raw;
unsigned char **matriz_image;
int i, j;
int rows=1080, colums=1920;
matriz_image = (unsigned char **) malloc (rows*sizeof(unsigned char *));
//i create dinamic colums
for(i=0; i<rows; i++)
{
matriz_image[i] = (unsigned char *) malloc (colums*sizeof(unsigned char ));
}
//i open image raw
image_raw = fopen("imag.dat", "r+b");
//i copy values to matriz_image
for (i = 0; i < rows; ++i)
{
fread(matriz_image[i], sizeof(unsigned char ), colums, image_raw);
}
for(i=0; i<rows; i++)
{
for(j=0; j<colums; j++)
{
// printf("%i ",*(*(matriz_image+i)+j));
printf("%i ",matriz_image[i][j]);
}
printf("\n");
}

You could do something like this:
void dct(unsigned char **m, int baserow, int basecol)
{
for (int row = baserow, endrow = baserow + 8; row < endrow; ++row)
for (int col = basecol, endcol = basecol + 8; col < endcol; ++col)
; // operate on m[row][col]
}
int do_dcts(unsigned char **m, int num_rows, int num_cols)
{
if (num_rows <= 0 || num_rows % 8 || num_cols <= 0 || num_cols % 8)
return -1;
for (int row = 0; row < num_rows; row += 8)
for (int col = 0; col < num_cols; col += 8)
dct(m, row, col);
return 0;
}
You are wasting space and worsening your memory locality by implementing your 2D array using two levels of pointers. It's better to do one allocation and then offset into the array appropriately like so:
int main()
{
FILE *image_raw;
unsigned char *matriz_image;
int i, j;
int rows=1080, colums=1920;
matriz_image = malloc(rows*colums*sizeof(unsigned char));
...
If you can make rows and colums constants or have VLAs, then you can do:
unsigned char (*m)[colums] = (unsigned char (*)[colums]) matriz_image;
m[5][2] = 2; // double indexed access without extra pointers + allocs
Similarly you can pass m's kind of pointer to your matrix to your functions to operate on it.
If you can't make rows and colums be compile-time constants and you don't have VLAs, then you can write helper fcns to do pointer arithmetic for you:
inline unsigned char *get_row(unsigned char *m, int numcols, int row)
{
return &m[row * num_cols];
}
inline unsigned char *get_elem(unsigned char *m, int numcols, int row, int col)
{
return &m[row * num_cols + col];
}
...
*get_elem(m, colums, 5, 2) = 2; // double indexing not as nice but good memory usage
If you really need to get fast for these operations, then as you read your image in, you could reorganize it to lay the 8x8 bytes blocks contiguously in memory to have the best possible cache performance:
// organize m like m[rows * colums / 64][8][8]; so first index is an 8x8 block #
for (int k = 0; k < rows / 8; ++k) // read all rows in chunks of 8
for (int i = 0; i < 8; ++i) // read 8 rows
for (int j = 0; j < colums / 8; ++j) // read 1 row in 8 byte chunks
fread(&m[k * 8 * colums + i * 8 + j * 64], 1, 8, image_raw);
...
typedef unsigned char (*block_ptr)[8];
inline block_ptr get_block(unsigned char *m, int num_cols, int block_num)
{
return (block_ptr) &m[block_num * 64];
}
inline block_ptr get_block2(unsigned char *m, int num_cols, int row, int col)
{
if (row % 8 || col % 8)
return NULL;
return (block_ptr) &m[row * num_cols + col * 8];
}
...
for (int k = 0; k < rows * colums / 64; ++k)
{
block_ptr block = get_block(m, num_colums, k);
for (int i = 0; i < 8; ++i)
for (int j = 0; j < 8; ++j)
; // operate on block[i][j];
}

Related

Slice variable length char array

I have a variable string, that I need to slice into smaller strings, the main string should be treated as a bidimensional array with a certain width and height MxM, and the smaller strings should be cut in blocks of NxN size. So for example, if I have the following string, char source[17] = "ABCDEFGHIJKLMNS0" and his bidimensional size is 4x4, and the size of the smaller blocks are 2x2, the smaller blocks should be ABEF, CDGH, IJMN, KLSO.
In other words, the string should be seeing as
ABCD
EFGH
IJKL
MNSO
and NxN should be cut from it, like:
AB
EF
Always with the constraint that these blocks should be linear arrays as the main string.
I have tried with 3 nested for, with the following code, but I didn't know how to calc the index of the main array in order to cut the blocks that way
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
char pixelsSource[17] = "ABCDEFGHIJKLMNS0";
char pixelsTarget[4][5];
int Y = 0;
int X = 0;
for (int block = 0; block < 4; block++)
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
pixelsTarget[block][(i * 2) + j] = pixelsSource[(i * 2) + j];
printf("[%d][%d] = [%d] \n", block, (i * 2) + j, (i * 2));
}
}
}
for (int block = 0; block < 4; block++)
{
printf("%s\n", pixelsTarget[block]);
}
}
I broke this down into a more piece-by-piece way and generalized it for M & N. Here's the code, with inline comments:
#include <stdio.h>
#define M 4
#define N 2
int main(void)
{
// source data is an M * M string(plus null terminator)
char pixelsSource[M * M + 1] = "ABCDEFGHIJKLMNSO";
// destination is an array of N*N strings; there are M*M/N*N of them
char pixelsTarget[(M*M)/(N*N)][N*N + 1];
// iterate over the source array; blockX and blockY are the coordinate of the top-left corner
// of the sub-block to be extracted
for (int blockX = 0; blockX < M; blockX += N)
{
for (int blockY = 0; blockY < M; blockY += N)
{
int dstWord = blockX/N + blockY;
// for each letter in the sub-block, copy that letter over to the destination array
for (int y = 0; y < N; y++)
{
for (int x = 0; x < N; x++)
{
int dstIndex = y*N + x;
int srcIndex = (blockY + y)*M + blockX + x;
printf("[%d][%d] = [%d]\n", dstWord, dstIndex, srcIndex);
pixelsTarget[dstWord][dstIndex] = pixelsSource[srcIndex];
}
}
// null-terminate destination word
pixelsTarget[dstWord][N*N] = '\0';
}
}
// output
for (int block = 0; block < (M * M) / (N * N); block++)
{
printf("%s\n", pixelsTarget[block]);
}
}

Subscripted value is neither array nor pointer nor vector in C program

I have a program that is passed a img_ptr of size 448x448, that is broken into 4 equal parts, each corner is applied filters, and now I am trying to reform the image back together using the 4 parts each stored in the subBlock subBlockList[];
I am being thrown this error:
"error: subscripted value is neither array nor pointer nor vector"
for each line: "subImageTempHolder[i][j] = subBlockList[x].vertMask[i][j]
How can I correct this, I suspect it has something to do with pointers.
Sub-block structure
struct subBlock {
unsigned char *horizMask;
unsigned char *vertMask;
unsigned char *combMask;
float localMean;
float localSD;
float localMedian;
};
static struct subBlock subBlockList[4];
#define BLOCK_ROW 224 //sub-block row
#define BLOCK_COL 224 //sub-block col
#define imageSize 448
Each position of the structure holds a quarter of the total image as follows:
subBlockList[0].vertMask contains a unsigned char* 2d (array of size 224x224) NW vals
subBlockList[1].vertMask contains a unsigned char* 2d (array of size 224x224) NE vals
subBlockList[2].vertMask contains a unsigned char* 2d (array of size 224x224) SW vals
subBlockList[3].vertMask contains a unsigned char* 2d (array of size 224x224) SE Vals
Function to take the 4 parts and put it back into one image_ptr (THE ONE THROWING ERRORS)
image_ptr buildImage(){
image_ptr retVal; // contains the image pointer to return
unsigned char subImageTempHolder[imageSize][imageSize];
int subBlockSize = 224;
//NW Corner
for (int i=0; i< subBlockSize; i++) { // 0<224
for (int j=0; j< subBlockSize; j++){ // 0<224
subImageTempHolder[i][j] = subBlockList[0].vertMask[i][j];
}
}
//NE Corner
for (int i=0; i< subBlockSize; i++) { //0 <224
for (int j=subBlockSize; j< imageSize; j++){ //224 < 448
subImageTempHolder[i][j] = subBlockList[1].vertMask[i][j];
}
}
//SW Corner
for (int i=subBlockSize; i< imageSize; i++) { //224 <448
for (int j=0; j< subBlockSize; j++){ //0 < 224
subImageTempHolder[i][j] = subBlockList[2].vertMask[i][j];
}
}
//SE Corner
for (int i=subBlockSize; i< imageSize; i++) { //224 < 448
for (int j=subBlockSize; j< imageSize; j++){ //224 <448
subImageTempHolder[i][j] = subBlockList[3].vertMask[i][j];
}
}
retVal = (image_ptr) subImageTempHolder;
return retVal;
}
It is being set like this:
subBlockList[blockPos].vertMask = verticalMask(block);
//I didnt include the function using line above ^ but you should get the idea.
unsigned char* verticalMask(unsigned char paramBlock[BLOCK_ROW][BLOCK_COL]) {
unsigned char retVal[BLOCK_ROW][BLOCK_COL]; //return value
double pixelVal;
double min = DBL_MAX;
double max = -DBL_MAX;
//3x3 Gy Sobel Mask
int Gy[3][3];
Gy[0][0] = 1; Gy[0][1] = 2; Gy[0][2] = 1;
Gy[1][0] = 0; Gy[1][1] = 0; Gy[1][2] = 0;
Gy[2][0] = -1; Gy[2][1] = -2; Gy[2][2] = -1;
//filtering
for (int y = 0; y<= BLOCK_COL-1; y++) {
for (int x=0; x <= BLOCK_ROW-1; x++) {
pixelVal = 0.0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
pixelVal += Gy[i+1][j+1] * paramBlock[y+i][x+j];
}
}
if (pixelVal < min) {
min = pixelVal;
}
if (pixelVal > min) {
max = pixelVal;
}
}
}
if((int)(max - min) == 0) {
printf("Error nothing exists");
}
//generate image
for (int y = 1; y < BLOCK_COL - 1; y++) {
for (int x = 1; x < BLOCK_ROW - 1; x++) {
pixelVal = 0.0;
for (int j = -1; j <= 1; j++) {
for (int i = -1; i <= 1; i++) {
pixelVal += Gy[j + 1][i + 1] * paramBlock[y + j][x + i];
}
}
pixelVal = max * (pixelVal - min) / (max - min); //MAX_BRIGHTNESS
retVal[y][x] = (unsigned char)pixelVal;
}
}
return retVal;
}
It's the vertMask[i][j] in subBlockList[0].vertMask[i][j].
subBlockList[0] returns a struct subBlock. .vertMask is an array of unsigned characters. .vertMask[i] returns the ith unsigned char in .vertMask. .vertMask[i][j] is asking for the jth element of an unsigned char which doesn't make sense.

Optimizing Matrix multiplication in C with Bit Packing

I'm currently attempting to write an algorithm for optimizing matrix multiplication over GF(2) using bit-packing. Both matrices A and B are provided in column major order so I start by copying A into row-major order and then packing the values into 8-bit integers and using parity checking to speed up operations. I need to be able to test square matrices of up to 2048x2048, however, my current implementation provides the correct answer up to 24x24 and then fails to compute the correct result. Any help would be appreciated.
//Method which packs an array of integers into 8 bits
uint8_t pack(int *toPack) {
int i;
uint8_t A;
A = 0;
for (i = 0; i < 8; i++) {
A = (A << 1) | (uint8_t)toPack[i];
}
return A;
}
//Method for doing matrix multiplication over GF(2)
void matmul_optimized(int n, int *A, int *B, int *C) {
int i, j, k;
//Copying values of A into a row major order matrix.
int *A_COPY = malloc(n * n * sizeof(int));
int copy_index = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
A_COPY[copy_index] = A[i + j * n];
copy_index++;
}
}
//Size of the data data type integers will be packed into
const int portion_size = 8;
int portions = n / portion_size;
//Pointer space reserved to store packed integers in row major order
uint8_t *compressedA = malloc(n * portions * sizeof(uint8_t));
uint8_t *compressedB = malloc(n * portions * sizeof(uint8_t));
int a[portion_size];
int b[portion_size];
for (i = 0; i < n; i++) {
for (j = 0; j < portions; j++) {
for (k = 0; k < portion_size; k++) {
a[k] = A_COPY[i * n + j * portion_size + k];
b[k] = B[i * n + j * portion_size + k];
}
compressedA[i * n + j] = pack(a);
compressedB[i * n + j] = pack(b);
}
}
//Calculating final matrix using parity checking and XOR on A and B
int cij;
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
int cIndex = i + j * n;
cij = C[cIndex];
for (k = 0; k < portions; ++k) {
uint8_t temp = compressedA[k + i * n] & compressedB[k + j * n];
temp ^= temp >> 4;
temp ^= temp >> 2;
temp ^= temp >> 1;
uint8_t parity = temp & (uint8_t)1;
cij = cij ^ parity;
}
C[cIndex] = cij;
}
}
free(compressedA);
free(compressedB);
free(A_COPY);
}
I have two remarks:
you should probably initialize cij to 0 instead of cij = C[cIndex];. It seems incorrect to update the destination matrix instead of storing the result of A * B. Your code might work for small matrices by coincidence because the destination matrix C happens to be all zeroes for this size.
it is risky to compute the allocation size as malloc(n * n * sizeof(int)); because n * n might overflow with int n if int is smaller than size_t. Given the sizes you work with, it is probably not a problem here, but it is a good idea to always use the sizeof as the first operand to force conversion to size_t of the following ones:
int *A_COPY = malloc(sizeof(*A_COPY) * n * n);

C - Read matrix of unknown size from file

I have file that has 30 matrices and each matrix has unknown size of rows and columns(with a max size of 1000). For instance:
0 5 2
5 0 2
1 6 0
0 9 7 4
3 0 9 1
9 1 0 4
9 4 1 0
I need to read each matrix into a 2d array. What would be the most efficient way of doing this?
This is what I have so far:
int** mat=malloc(1000000*sizeof(int*));
for(i=0;i<1000000;++i)
mat[i]=malloc(4*sizeof(int));
while(!feof(file))
{
for(i=0;i<1000;i++)
{
for(j=0;j<1000;j++){
fscanf(file,"%d%*[^\n]%*c",&mat[i][j]);
printf("%d\n", mat[i][j]);
}
}
}
Well the most efficient way is definitely not that. First figure out how big an array you need, then allocate it.
Apparently some matrices are small, so there is no need to allocate the maximum size 1000x1000. One way is to put the matrix in a structure to make it easier to keep track of size:
struct s_matrix
{
int **matrix;
int N; //one side of the square matrix N x N
};
typedef struct s_matrix Matrix;
Then allocate and free the matrix
void allocate_matrix(Matrix *m, int N)
{
m->N = N;
m->matrix = (int**)malloc(N * sizeof(int*));
*m->matrix = (int*)malloc(N * N * sizeof(int));
for (int i = 0; i < N; i++)
m->matrix[i] = *m->matrix + i * N;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
m->matrix[i][j] = 0;
}
void free_matrix(Matrix *m)
{
if (!m) return;
if (!m->matrix) return;
free(*m->matrix);
free(m->matrix);
}
Now we can declare how many matrices we need. It looks like this number is fixed at 30, so I don't think we need dynamic allocation.
int main()
{
const int max_count = 30;
Matrix list[max_count];
for (int i = 0; i < max_count; i++)
list[i].matrix = NULL;
allocate_matrix(&list[0], 3);//allocate 3x3 matrix
allocate_matrix(&list[1], 1000);//1000x1000
allocate_matrix(&list[2], 4000);//4000x4000
int **m;
m = list[0].matrix;
m[0][0] = 0;
m[0][1] = 1;
m[0][2] = 2;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
printf("%02d %s", m[i][j], (j == 2) ? "\n" : "");
//...
for (int i = 0; i < max_count; i++)
free_matrix(&list[i]);
printf("hey it worked, or maybe it didn't, or it did but there is memory leak\n");
return 0;
}

Allocate 3D matrix in one big chunk

I'd like to allocate a 3D matrix in one big chunk. It should be possible to access this matrix in the [i][j][k] fashion, without having to calculate the linearized index every time.
I think it should be something like below, but I'm having trouble filling the ...
double ****matrix = (double ****) malloc(...)
for (int i = 0; i < imax; i++) {
matrix[i] = &matrix[...]
for (int j = 0; j < jmax; j++) {
matrix[i][j] = &matrix[...]
for (int k = 0; k < kmax; k++) {
matrix[i][j][k] = &matrix[...]
}
}
}
For the single allocation to be possible and work, you need to lay out the resulting memory like this:
imax units of double **
imax * jmax units of double *
imax * jmax * kmax units of double
Further, the 'imax units of double **' must be allocated first; you can reorder the other two sections, but it is most sensible to deal with them in the order listed.
You also need to be able to assume that double and double * (and double **, but that's not much of a stretch) are sufficiently well aligned that you can simply allocate the chunks contiguously. That is going to hold OK on most 64-bit systems with type double, but be aware of the possibility that it does not hold on 32-bit systems or for other types than double (basically, the assumption could be problematic when sizeof(double) != sizeof(double *)).
With those caveats made, then this code works cleanly (tested on Mac OS X 10.10.2 with GCC 4.9.1 and Valgrind version valgrind-3.11.0.SVN):
#include <stdio.h>
#include <stdlib.h>
typedef double Element;
static Element ***alloc_3d_matrix(size_t imax, size_t jmax, size_t kmax)
{
size_t i_size = imax * sizeof(Element **);
size_t j_size = imax * jmax * sizeof(Element *);
size_t k_size = imax * jmax * kmax * sizeof(Element);
Element ***matrix = malloc(i_size + j_size + k_size);
if (matrix == 0)
return 0;
printf("i = %zu, j = %zu, k = %zu; sizes: i = %zu, j = %zu, k = %zu; "
"%zu bytes total\n",
imax, jmax, kmax, i_size, j_size, k_size, i_size + j_size + k_size);
printf("matrix = %p .. %p\n", (void *)matrix,
(void *)((char *)matrix + i_size + j_size + k_size));
Element **j_base = (void *)((char *)matrix + imax * sizeof(Element **));
printf("j_base = %p\n", (void *)j_base);
for (size_t i = 0; i < imax; i++)
{
matrix[i] = &j_base[i * jmax];
printf("matrix[%zu] = %p (%p)\n",
i, (void *)matrix[i], (void *)&matrix[i]);
}
Element *k_base = (void *)((char *)j_base + imax * jmax * sizeof(Element *));
printf("k_base = %p\n", (void *)k_base);
for (size_t i = 0; i < imax; i++)
{
for (size_t j = 0; j < jmax; j++)
{
matrix[i][j] = &k_base[(i * jmax + j) * kmax];
printf("matrix[%zu][%zu] = %p (%p)\n",
i, j, (void *)matrix[i][j], (void *)&matrix[i][j]);
}
}
/* Diagnostic only */
for (size_t i = 0; i < imax; i++)
{
for (size_t j = 0; j < jmax; j++)
{
for (size_t k = 0; k < kmax; k++)
printf("matrix[%zu][%zu][%zu] = %p\n",
i, j, k, (void *)&matrix[i][j][k]);
}
}
return matrix;
}
int main(void)
{
size_t i_max = 3;
size_t j_max = 4;
size_t k_max = 5;
Element ***matrix = alloc_3d_matrix(i_max, j_max, k_max);
if (matrix == 0)
{
fprintf(stderr, "Failed to allocate matrix[%zu][%zu][%zu]\n", i_max, j_max, k_max);
return 1;
}
for (size_t i = 0; i < i_max; i++)
{
for (size_t j = 0; j < j_max; j++)
{
for (size_t k = 0; k < k_max; k++)
matrix[i][j][k] = (i + 1) * 100 + (j + 1) * 10 + k + 1;
}
}
for (size_t i = 0; i < i_max; i++)
{
for (size_t j = 0; j < j_max; j++)
{
for (size_t k = k_max; k > 0; k--)
printf("[%zu][%zu][%zu] = %6.0f\n", i, j, k-1, matrix[i][j][k-1]);
}
}
free(matrix);
return 0;
}
Example output (with some boring bits omitted):
i = 3, j = 4, k = 5; sizes: i = 24, j = 96, k = 480; 600 bytes total
matrix = 0x100821630 .. 0x100821888
j_base = 0x100821648
matrix[0] = 0x100821648 (0x100821630)
matrix[1] = 0x100821668 (0x100821638)
matrix[2] = 0x100821688 (0x100821640)
k_base = 0x1008216a8
matrix[0][0] = 0x1008216a8 (0x100821648)
matrix[0][1] = 0x1008216d0 (0x100821650)
matrix[0][2] = 0x1008216f8 (0x100821658)
matrix[0][3] = 0x100821720 (0x100821660)
matrix[1][0] = 0x100821748 (0x100821668)
matrix[1][1] = 0x100821770 (0x100821670)
matrix[1][2] = 0x100821798 (0x100821678)
matrix[1][3] = 0x1008217c0 (0x100821680)
matrix[2][0] = 0x1008217e8 (0x100821688)
matrix[2][1] = 0x100821810 (0x100821690)
matrix[2][2] = 0x100821838 (0x100821698)
matrix[2][3] = 0x100821860 (0x1008216a0)
matrix[0][0][0] = 0x1008216a8
matrix[0][0][1] = 0x1008216b0
matrix[0][0][2] = 0x1008216b8
matrix[0][0][3] = 0x1008216c0
matrix[0][0][4] = 0x1008216c8
matrix[0][1][0] = 0x1008216d0
matrix[0][1][1] = 0x1008216d8
matrix[0][1][2] = 0x1008216e0
matrix[0][1][3] = 0x1008216e8
matrix[0][1][4] = 0x1008216f0
matrix[0][2][0] = 0x1008216f8
…
matrix[2][2][4] = 0x100821858
matrix[2][3][0] = 0x100821860
matrix[2][3][1] = 0x100821868
matrix[2][3][2] = 0x100821870
matrix[2][3][3] = 0x100821878
matrix[2][3][4] = 0x100821880
[0][0][4] = 115
[0][0][3] = 114
[0][0][2] = 113
[0][0][1] = 112
[0][0][0] = 111
[0][1][4] = 125
[0][1][3] = 124
[0][1][2] = 123
[0][1][1] = 122
[0][1][0] = 121
[0][2][4] = 135
…
[2][2][0] = 331
[2][3][4] = 345
[2][3][3] = 344
[2][3][2] = 343
[2][3][1] = 342
[2][3][0] = 341
There is a lot of diagnostic output in the code shown.
This code will work with C89 (and C99 and C11), without requiring support for variable-length arrays or VLAs — though since I declare variables in for loops, the code as written requires C99 or later, but it can easily be fixed to declare the variables outside the for loops and it can then compile with C89.
This can be done with one simple malloc() call in C (not in C++, though, there are no variable length arrays in C++):
void foo(int imax, int jmax, int kmax) {
double (*matrix)[jmax][kmax] = malloc(imax*sizeof(*matrix));
//Allocation done. Now fill the matrix:
for(int i = 0; i < imax; i++) {
for(int j = 0; j < jmax; j++) {
for(int k = 0; k < kmax; k++) {
matrix[i][j][k] = ...
}
}
}
}
Note that C allows jmax and kmax to be dynamic values that are only known at runtime. That is the ability that's missing in C++, which makes C arrays much more powerful than their C++ counterpart.
The only drawback of this approach, as WhozCraig rightly notes, is that you can't return the resulting matrix as the return value of the function without resorting to a void*. However, you can return it by reference like this:
void foo(int imax, int jmax, int kmax, double (**outMatrix)[jmax][kmax]) {
*outMatrix = malloc(imax*sizeof(**outMatrix));
double (*matrix)[jmax][kmax] = *outMatrix; //avoid having to write (*outMatrix)[i][j][k] everywhere
... //as above
}
This function would need to be called like this:
int imax = ..., jmax = ..., kmax = ...;
double (*myMatrix)[jmax][kmax];
foo(imax, jmax, kmax, &myMatrix);
That way you get full type checking on the inner two dimension sizes even though they are runtime values.
Note: This was intended to be a comment but it got too long, until it turned into a proper answer.
You can't use a single chunk of memory without performing some calculations.
Note that the beginning of each row is marked by the formula
// row_begin is the memory address of the row at index row_idx
row_begin = row_idx * jmax * kmax
And then, each column depends on where the row starts:
// column_begin is the memory address of the column
// at index column_idx of the row starting at row_begin
column_begin = row_begin + column_idx * kmax
Which, using absolute addresses (relative to the matrix pointer, of course) translates to:
column_begin = (row_idx * jmax * kmax) + column_idx * kmax
Finally, getting the k-index of an element is very straightforward, following the previous rule this could turn in an infinite recursion:
// element address = row_address + column_address + element_k_index
element_k_idx = column_begin + element_k_idx
Which translates to
element_k_idx = (row_idx * jmax * kmax) + column_idx * kmax + element_k_idx
This works for me:
void foo(int imax, int jmax, int kmax)
{
// Allocate memory for all the numbers.
// Think of this as (imax*jmax) number of memory chunks,
// with each chunk containing kmax doubles.
double* data_0 = malloc(imax*jmax*kmax*sizeof(double));
// Allocate memory for the previus dimension of pointers.
// This of this as imax number of memory chunks,
// with each chunk containing jmax double*.
double** data_1 = malloc(imax*jmax*sizeof(double*));
// Allocate memory for the previus dimension of pointers.
double*** data_2 = malloc(imax*sizeof(double**));
for (int i = 0; i < imax; i++)
{
data_2[i] = &data_1[i*jmax];
for (int j = 0; j < jmax; j++)
{
data_1[i*jmax+j] = &data_0[(i*jmax+j)*kmax];
}
}
// That is the matrix.
double ***matrix = data_2;
for (int i = 0; i < imax; i++)
{
for (int j = 0; j < jmax; j++)
{
for (int k = 0; k < kmax; k++)
{
matrix[i][j][k] = i+j+k;
}
}
}
for (int i = 0; i < imax; i++)
{
for (int j = 0; j < jmax; j++)
{
for (int k = 0; k < kmax; k++)
{
printf("%lf ", matrix[i][j][k]);
}
printf("\n");
}
}
// Deallocate memory
free(data_2);
free(data_1);
free(data_0);
}

Resources