00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
#include "idima.h"
00037
#include "idima-private.h"
00038
#include "iderr.h"
00039
#include "idprint.h"
00040
#include <stdlib.h>
00041
00042
void _IdFreeLinesOfPlane(
void **pi,
int n);
00043
00044
00045
00046
00047
00048
00049
00050
00051
void _IdFreePlanesOfBloc(
void ***,
int,
int);
00052
00053 void _IdFreeLinesOfPlane(
void **pi,
int n)
00054 {
00055
#ifdef _TURBOC_
00056
int i;
00057
for (i = 0; i < n; i++)
00058
if (pi[i]) {
00059 free(pi[i]);
00060 pi[i] = 0;
00061 }
00062
#else
00063
if (pi[0]) {
00064 free(pi[0]);
00065 pi[0] = 0;
00066 }
00067
#endif
00068
}
00069
00070 int _IdAllocLinesOfPlane(
void *piv,
int sx,
int sy,
int si)
00071 {
00072
00073
char **pi = (
char **) piv;
00074
int i = 0;
00075
00076
#ifdef _TURBOC_
00077
for (i = 0; i < sy; i++) {
00078
if ((pi[i] = (
char *) calloc(1, sx * si)) == 0) {
00079
_IdFreeLinesOfPlane((
void **) pi, sy);
00080
return 0;
00081 }
00082 }
00083
00084
#else
00085
00086
00087
00088
if ((pi[0] = (
char *) calloc(1, sx * sy * si)))
00089
for (i = 1; i < sy; i++)
00090 pi[i] = pi[0] + sx * si * i;
00091
else {
00092
IdErrPrintf(
"EchecAlloc ligne %d (lgr %d : %d*%d*%d).", i,
00093 sx * sy * si, sx, sy, si);
00094
return 0;
00095 }
00096
00097
#endif
00098
return 1;
00099 }
00100
00101 void **
_IdAllocPlan(
int sx,
int sy,
int si)
00102 {
00103
char **pi;
00104
00105 pi = (
char **) calloc(sy,
sizeof(
void *));
00106
if (pi) {
00107
if (!
_IdAllocLinesOfPlane(pi, sx, sy, si)) {
00108
IdErrPrintf
00109 (
"Echec _IdAllocLinesOfPlane. Memoire saturee.");
00110 free(pi);
00111 pi = (
char **) 0;
00112 }
00113 }
00114
return ((
void **) pi);
00115 }
00116
00117 void _IdFreePlan(
void **pi,
int n)
00118 {
00119
if (pi) {
00120
_IdFreeLinesOfPlane(pi, n);
00121 free(pi);
00122 }
00123 }
00124
00125
00126
static int _IdAllocPlanesOfBloc(
void *piv,
int sx,
int sy,
int sz,
int si)
00127 {
00128
00129
char ***pi = (
char ***) piv;
00130
int i;
00131
for (i = 0; i < sz; i++) {
00132
if (0 == (pi[i] = (
char **)
_IdAllocPlan(sx, sy, si))) {
00133
IdErrPrintf
00134 (
"Echec Alloc Plan %d (sx %d sy %d si %d).", i,
00135 sx, sy, si);
00136
_IdFreePlanesOfBloc((
void ***) pi, sz, sy);
00137
return 0;
00138 }
00139 }
00140
return 1;
00141 }
00142
00143 void _IdFreePlanesOfBloc(
void ***pi,
int nbp,
int nbl)
00144 {
00145
int i;
00146
for (i = 0; i < nbp; i++)
00147
if (pi[i]) {
00148
_IdFreeLinesOfPlane(pi[i], nbl);
00149 pi[i] = 0;
00150 }
00151 }
00152
00153
00154 void ***
_IdAllocBloc(
int sx,
int sy,
int sz,
int si)
00155 {
00156
char ***pi;
00157
00158 pi = (
char ***) calloc(sz,
sizeof(
void **));
00159
if (pi) {
00160
if (!_IdAllocPlanesOfBloc(pi, sx, sy, sz, si)) {
00161
IdErrPrintf
00162 (
"Echec Alloc dans _IdAllocPlanesOfBloc (sx %d sy %d sz %d si %d",
00163 sx, sy, sz, si);
00164 free(pi);
00165 pi = (
char ***) 0;
00166 }
00167
00168 }
else
00169
IdErrPrintf(
"Echec Alloc tabPointeurs pour Bloc");
00170
00171
return ((
void ***) pi);
00172 }
00173
00174
00175 void _IdFreeBloc(
void ***pi,
int nbp,
int nbl)
00176 {
00177
if (pi) {
00178
_IdFreePlanesOfBloc(pi, nbp, nbl);
00179 free(pi);
00180 }
00181 }
00182
00188 void _IdImaFree(PPIMAGE * pi)
00189 {
00190
if (*pi) {
00191
if (
_IdImaPrivate(*pi)->subObject == 0)
00192
00193
00194
00195
00196 free((*pi)[0]);
00197
00198
00199
if (
_IdImaPrivate(*pi)->_message)
00200 free((
_IdImaPrivate(*pi)->_message));
00201
_IdImaPrivate(*pi)->_message = 0;
00202 if (
_IdImaPrivate(*pi)->_fichier)
00203 free((
_IdImaPrivate(*pi)->_fichier));
00204
_IdImaPrivate(*pi)->_fichier = 0;
00205 free(
_IdImaPrivate(*pi));
00206 *pi = 0;
00207 }
00208 }
00209
00221 PPIMAGE IdImaAlloc(
int sx,
00222
int sy,
00223
int ty)
00224 {
00225
PRIVATE_IMAGE *si;
00226
PPIMAGE pi = NULL;
00227
if (!(ty &
IDO_TYPE) ==
IMA) {
00228
IdErrPrintf
00229 (
"Allocation impossible. Objet de type incompatible(%x)\n",
00230 ty);
00231
IdErrno =
IDERR_WRONG_TYPE;
00232
return (
PPIMAGE) 0;
00233 }
00234 si =
00235 (
PRIVATE_IMAGE *) calloc(1,
sizeof(
PRIVATE_IMAGE) +
00236 sy *
sizeof(
void *));
00237
if (si) {
00238 si->
DimX = sx;
00239 si->
DimY = sy;
00240 si->
Type = ty;
00241 si->
subObject = 0;
00242 si->
UsedNbX = 0;
00243 si->
UsedNbY = 0;
00244 si->
_fichier = 0;
00245 si->
_message = 0;
00246 pi = (
PPIMAGE) & (si[1]);
00247
if (ty ==
IMA_BIT) {
00248 sx = (sx + 7) >> 3;
00249 ty =
IMA_UCHAR;
00250 }
00251
if (!
_IdAllocLinesOfPlane(pi, sx, sy,
IdSizeOfType(pi))) {
00252
IdImaFree(pi);
00253
IdErrPrintf
00254 (
"Echec d'allocation d'image. Memoire saturee.");
00255
IdErrno =
IDERR_ALLOC_MEM;
00256
return (
PPIMAGE) 0;
00257 }
00258 }
else {
00259
IdErrPrintf
00260 (
"Echec d'allocation PRIVATE_IMAGE. Memoire saturee.");
00261 }
00262
return (pi);
00263 }
00264
00275 PPIMAGE IdImaClone(PPIMAGE sim)
00276 {
00277
return IdImaAllocLikeImage(sim);
00278 }
00279
00290 PPIMAGE IdImaAllocLikeImage (
void * sim)
00291 {
00292
if ( !sim ) {
00293
IdErrPrintf (
"IdImaAllocLikeImage : can't clone NULL.");
00294
return (
PPIMAGE)0;
00295 }
00296
return IdImaAlloc(
IdImaDimX(sim),
IdImaDimY(sim),
IdImaType(sim));
00297 }