-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix.c
92 lines (77 loc) · 2.09 KB
/
matrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "matrix.h"
/*memory allocation to place the Image Matrix */
int Matrix_Allocate_Old(int intHeight,int intWidth,unsigned char *** pMat)
{
int i,intCount,j;
*pMat = (unsigned char **)malloc(intHeight*sizeof(unsigned char *));
if ( (*pMat) != NULL) /*allocation successful*/
{
for(i=0;i<=intHeight-1;i++)
{
// place reservation for the three componants RGB
(*pMat)[i]=(unsigned char *)malloc(3*intWidth*sizeof(unsigned char));
if ((*pMat)[i] == NULL) //allocation error
{
printf("allocation simple matrix old memory error\n");
//we need to free the matrix memory that is already allocated
for(j=i-1;j<=0;j--)
free(((*pMat)[j]));
free(*pMat);
return FALSE;
}
}
}
else /*allocation error*/
{
printf("allocation simple matrix old memory error\n");
return FALSE;
}
return TRUE;
}
/* we need to free the matrix memory */
void Matrix_Free_Old(int intHeight,unsigned char *** pMat)
{
int i;
for(i=0;i<=intHeight-1;i++)
free((*pMat)[i]);
free(*pMat);
printf("simple matrix old memory free\n");
}
/*memory allocation to place the Matrix */
int Matrix_Allocate(int intHeight,int intWidth,unsigned char *** pMat)
{
int i,intCount,j;
*pMat = (unsigned char **)malloc(intHeight*sizeof(unsigned char *));
if ( (*pMat) != NULL) /*allocation successful*/
{
for(i=0;i<=intHeight-1;i++)
{
// place reservation for one componant
(*pMat)[i]=(unsigned char *)malloc(intWidth*sizeof(unsigned char));
if ((*pMat)[i] == NULL) //allocation error
{
printf("allocation simple matrix memory error\n");
//we need to free the matrix memory that is already allocated
for(j=i-1;j<=0;j--)
free(((*pMat)[j]));
free(*pMat);
return FALSE;
}
}
}
else /*allocation error*/
{
printf("allocation simple matrix memory error\n");
return FALSE;
}
return TRUE;
}
/* we need to free the matrix memory */
void Matrix_Free(int intHeight,unsigned char *** pMat)
{
int i;
for(i=0;i<=intHeight-1;i++)
free((*pMat)[i]);
free(*pMat);
printf("simple matrix memory free\n");
}