main.cpp 3.24 KB
Newer Older
1
#include "blc_core.h"
Arnaud Blanchard's avatar
Arnaud Blanchard committed
2
#include <math.h> //sinf
3
4

int main(int argc, char **argv){
Arnaud Blanchard's avatar
Arnaud Blanchard committed
5
    blc_array vector, matrix, matrix_copy, pixel_array;
6
7
    size_t i;
    int width, height;
8
    
Arnaud Blanchard's avatar
Arnaud Blanchard committed
9
    fprintf(stderr, "\nDefining a sinusoïd of 32 uchars vector\n\nProperties:\n");
Arnaud Blanchard's avatar
Arnaud Blanchard committed
10
    //We define the properties of an array of type unsigned char (UIN8) with undef format (i.e. the user use it as he wants), of one dimension (vector) of 32 values.
Arnaud Blanchard's avatar
Arnaud Blanchard committed
11
    vector.def_array('UIN8', 'NDEF', 1, 32);
Arnaud Blanchard's avatar
Arnaud Blanchard committed
12
    //The property .dims (list of all dimensions) has been allocated.
Arnaud Blanchard's avatar
Arnaud Blanchard committed
13
    
14
15
16
17
    //Be careful the .data memory is not allocated ! This is usefull if you want to associate the data to your own memory.
    //We display the properties
    vector.fprint_properties(stderr);
    fprintf(stderr, "\n");
18
    
19
20
21
    //We allocate the memory
    vector.allocate();
    
Arnaud Blanchard's avatar
Arnaud Blanchard committed
22
    // We set a sinusoïd in the vector (converting [-1f, 1f] -> [0, 255];
Arnaud Blanchard's avatar
Arnaud Blanchard committed
23
    for(i=0; i!=vector.size; i++) vector.uchars[i]=128+127*sinf(i/2.f);
24
    
Arnaud Blanchard's avatar
Arnaud Blanchard committed
25
26
27
28
29
    fprintf(stderr, "Graph of the vector\n");

    //We graph the vector, with title "vector test", the height of 16 chars, the limit max 256 and limit min 0.
    //Text on abscissa "position" and text on ordonate "intensity"
    vector.fprint_graph_uchars(stderr, "Vector test", 16, 256, 0, "position", "intensity");
30

Arnaud Blanchard's avatar
Arnaud Blanchard committed
31
32
    fprintf(stderr, "\nDefining 3x5 float matrix\n\nDims:\n");

33
34
    //We define a matrix of 3x5
    matrix.def_array('FL32', 'NDEF', 2, 3, 5);
35
36
    
    //We allocate the content
37
    matrix.allocate();
38
    
39
40
41
42
43
    //We display  the dim sizes
    matrix.fprint_dims(stderr);
    fprintf(stderr, "\n");
    
    //We acquire the sizes of the dims
Arnaud Blanchard's avatar
Arnaud Blanchard committed
44
45
    width= matrix.dims[0].length;
    height= matrix.dims[1].length;
46
47
    fprintf(stderr, "width:%d, height:%d\n", width, height);
    
48
49
50
    //We set the data to zero everywhere
    memset(matrix.data, '0', matrix.size);
    
51
52
    //We set the first column of the matrix to 6.3
    for(i=0; i!=height; i++) matrix.floats[i*width]=6.3;
53
54
55
56
57
58
    
    //Write 33.3 in the center
    matrix.floats[1+width*2]=33.3;
    matrix.fprint_tsv(stderr);
    
    //We save the value as a blc file that we will be able to reread
59
60
    matrix.save_blc_file("blibs/blc_core/t_array/array.blc");
    matrix.save_tsv_file("blibs/blc_core/t_array/array.tsv"); //Bigger but readable in text mode or within spreadsheet, excel, matlab
61
62
    
    //We reload the matrix which should be identical
63
    matrix_copy.init_with_blc_file("blibs/blc_core/t_array/array.blc");
64
65
66
    
    //we compare the content of the two matrixes
    if (memcmp(matrix.data, matrix_copy.data, matrix.size)!=0) EXIT_ON_ARRAY_ERROR(&matrix,"The content of the saved and reloaded matrix are not the same");
Arnaud Blanchard's avatar
Arnaud Blanchard committed
67
    
Arnaud Blanchard's avatar
Arnaud Blanchard committed
68
    fprintf(stderr, "\nWe load an array of intensities and display the values\n\n");
Arnaud Blanchard's avatar
Arnaud Blanchard committed
69
70
    
    //Load the array
71
    pixel_array.init_with_blc_file("blibs/blc_core/lena32.blc");
Arnaud Blanchard's avatar
Arnaud Blanchard committed
72
    
Arnaud Blanchard's avatar
Arnaud Blanchard committed
73
    //Display the array as a array of decimals)
Arnaud Blanchard's avatar
Arnaud Blanchard committed
74
75
    pixel_array.fprint_surface_uchars(stderr);
    
76
    fprintf(stderr, "\nWe display the array of intensities as compact colored values\n\n");
Arnaud Blanchard's avatar
Arnaud Blanchard committed
77
78
79
80
81
    
    //Display the array as a compact surface of color (ansi_terminal). Ideal for images
    pixel_array.fprint_surface_uchars(stderr, 1);
    
    fprintf(stderr, "\n");
Arnaud Blanchard's avatar
Arnaud Blanchard committed
82
    //Print the scale
Arnaud Blanchard's avatar
Arnaud Blanchard committed
83
    blc_fprint_color_scale(stderr);
Arnaud Blanchard's avatar
Arnaud Blanchard committed
84
    
85
    return 0;
86
}