00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
#ifndef LHGeneralIncs_h
00012
#include "General.h"
00013
#endif
00014
00015
#ifndef LHTheRoutines_h
00016
#include "Routines.h"
00017
#endif
00018
00019
#if FARBR_FILES
00020
static FILE *stream1;
00021
#endif
00022 #define exit return
00023
#if ! realThing
00024
#ifdef DEBUG_OUTPUT
00025
#define __TYPES__
00026
#define kThisFile kLHTheRoutinesID
00027
#endif
00028
#endif
00029
00030 #define CLIPP(x,a) ((x)<(a)?(x):(a))
00031
00032
static void SetValues(
long current,
long inputDim,
long needBits, UINT8 **Poi );
00033
00034
OSErr
00035 CalcGridPoints4Cube (
long theCubeSize,
00036
long inputDim,
00037
long* theGridPoints,
00038
long* theGridBits )
00039 {
00040
long aSize,needBits,theSize;
00041
register long loop,gridPoints;
00042
OSErr err =
noErr;
00043
00044
LH_START_PROC(
"CalcGridPoints4Cube")
00045
00046
if( inputDim > 8 )
00047 {
00048 err =
cmparamErr;
00049
goto CleanupAndExit;
00050 }
00051 aSize = theCubeSize / inputDim;
00052 needBits=0;
00053
do
00054 {
00055 needBits++;
00056 gridPoints = (1<<needBits);
00057 theSize = gridPoints;
00058
for( loop=1; loop<inputDim; ++loop)
00059 theSize *= gridPoints;
00060 }
while( aSize >= theSize );
00061 needBits--;
00062
if( needBits <= 0 )
00063 {
00064 err =
cmparamErr;
00065
goto CleanupAndExit;
00066 }
00067 *theGridBits = needBits;
00068 *theGridPoints = (1<<needBits);
00069 CleanupAndExit:
00070
LH_END_PROC(
"CalcGridPoints4Cube")
00071
return err;
00072 }
00073
00074
OSErr
00075 MakeCube(
long inputDim,
00076
long *theCubeSize,
00077 CUBE_DATA_TYPE *theCube,
00078
long *theBits )
00079 {
00080
long needBits,theSize;
00081
long i,gridPoints;
00082
OSErr err =
noErr;
00083
UINT8 *cube =
nil;
00084
CUBE_DATA_TYPE tempCube;
00085
00086
00087
00088
00089
00090
LH_START_PROC(
"MakeCube")
00091 err =
CalcGridPoints4Cube(*theCubeSize, inputDim, &gridPoints, &needBits);
00092
if (err)
00093
goto CleanupAndExit;
00094
00095 theSize = gridPoints;
00096
for( i=1; i<inputDim; ++i)
00097 theSize *= gridPoints;
00098
00099 *theCubeSize = theSize * inputDim;
00100 tempCube =
ALLOC_DATA(theSize * inputDim, &err);
00101
if (err !=
noErr)
00102
goto CleanupAndExit;
00103
LOCK_DATA(tempCube);
00104 cube = (
UINT8*)
DATA_2_PTR(tempCube);
00105
00106
if( inputDim == 3)
00107 {
00108
register long aShift;
00109
register long j,k;
00110
register UINT8 aI, aJ;
00111 aShift = (8 - needBits) ;
00112
00113
00114
00115
00116
00117
00118
00119
00120
for( i=0; i<gridPoints; ++i){
00121 aI = (
UINT8)(i<< aShift);
00122
for( j=0; j<gridPoints; ++j){
00123 aJ = (
UINT8)(j<< aShift);
00124
for( k=0; k<gridPoints; ++k){
00125 *cube++ = aI;
00126 *cube++ = aJ;
00127 *cube++ =(
UINT8)(k<< aShift);
00128 }
00129 }
00130 }
00131 }
00132
else
00133 {
00134
SetValues( 0, inputDim, needBits, &cube );
00135 }
00136 *theBits = needBits;
00137
UNLOCK_DATA(tempCube);
00138 *theCube = tempCube;
00139 CleanupAndExit:
00140
LH_END_PROC(
"MakeCube")
00141
return err;
00142 }
00143
00144 #define UWE 2
00145
#ifdef UWE
00146
void SetValues16(
long current,
long inputDim,
long needBits, UINT16 **Poi );
00147 OSErr MakeCube16(
long inputDim,
00148
long *theCubeSize,
00149 CUBE_DATA_TYPE *theCube,
00150
long *theBits,
00151
long *theExtraSize )
00152 {
00153
long needBits,theSize,aExtraSize;
00154
long i,gridPoints;
00155
OSErr err =
noErr;
00156
UINT16 *cube =
nil;
00157
CUBE_DATA_TYPE tempCube;
00158
00159
LH_START_PROC(
"MakeCube16")
00160
00161
00162
00163
00164 err =
CalcGridPoints4Cube(*theCubeSize, inputDim, &gridPoints, &needBits);
00165
if (err)
00166
goto CleanupAndExit;
00167
00168 theSize = 1;
00169 aExtraSize = 1;
00170
for( i=0; i<(inputDim-1); ++i){
00171 theSize *= gridPoints;
00172 aExtraSize += theSize;
00173 }
00174
00175
#ifdef ALLOW_MMX
00176
aExtraSize++;
00177
#endif
00178
theSize *= gridPoints;
00179
00180 *theExtraSize = aExtraSize;
00181 *theCubeSize = theSize * inputDim;
00182 tempCube =
ALLOC_DATA( (theSize+aExtraSize) * inputDim*2, &err);
00183
if (err !=
noErr)
00184
goto CleanupAndExit;
00185
LOCK_DATA(tempCube);
00186 cube = (
UINT16*)
DATA_2_PTR(tempCube);
00187
00188
if( inputDim == 3)
00189 {
00190
register long aShift;
00191
register long j,k;
00192
register UINT16 aI, aJ, aK;
00193 aShift = (16 - needBits) ;
00194
00195
00196
00197
00198
00199
00200
00201
00202
for( i=0; i<gridPoints; ++i){
00203 aI = (
UINT16)(i<< aShift);
00204 aI |= aI >> needBits;
00205 aI |= aI >> (2*needBits);
00206 aI |= aI >> (4*needBits);
00207
for( j=0; j<gridPoints; ++j){
00208 aJ = (
UINT16)(j<< aShift);
00209 aJ |= aJ >> needBits;
00210 aJ |= aJ >> (2*needBits);
00211 aJ |= aJ >> (4*needBits);
00212
for( k=0; k<gridPoints; ++k){
00213 *cube++ = aI;
00214 *cube++ = aJ;
00215 aK = (
UINT16)(k<< aShift);
00216 aK |= aK >> needBits;
00217 aK |= aK >> (2*needBits);
00218 aK |= aK >> (4*needBits);
00219 *cube++ = aK;
00220 }
00221 }
00222 }
00223 }
00224
else if( inputDim == 4)
00225 {
00226
register long aShift;
00227
register long j,k,l;
00228
register UINT16 aI, aJ, aK, aL;
00229 aShift = (16 - needBits) ;
00230
00231
00232
00233
00234
00235
00236
00237
00238
for( i=0; i<gridPoints; ++i){
00239 aI = (
UINT16)(i<< aShift);
00240 aI |= aI >> needBits;
00241 aI |= aI >> (2*needBits);
00242 aI |= aI >> (4*needBits);
00243
for( j=0; j<gridPoints; ++j){
00244 aJ = (
UINT16)(j<< aShift);
00245 aJ |= aJ >> needBits;
00246 aJ |= aJ >> (2*needBits);
00247 aJ |= aJ >> (4*needBits);
00248
for( k=0; k<gridPoints; ++k){
00249 aK = (
UINT16)(k<< aShift);
00250 aK |= aK >> needBits;
00251 aK |= aK >> (2*needBits);
00252 aK |= aK >> (4*needBits);
00253
for( l=0; l<gridPoints; ++l){
00254 *cube++ = aI;
00255 *cube++ = aJ;
00256 *cube++ = aK;
00257 aL = (
UINT16)(l<< aShift);
00258 aL |= aL >> needBits;
00259 aL |= aL >> (2*needBits);
00260 aL |= aL >> (4*needBits);
00261 *cube++ = aL;
00262 }
00263 }
00264 }
00265 }
00266 }
00267
else
00268 {
00269
SetValues16( 0, inputDim, needBits, &cube );
00270 }
00271 *theBits = needBits;
00272
UNLOCK_DATA(tempCube);
00273 *theCube = tempCube;
00274 CleanupAndExit:
00275
LH_END_PROC(
"MakeCube16")
00276
return err;
00277 }
00278
00279 void SetValues16(
long current,
long inputDim,
long needBits, UINT16 **Poi )
00280 {
00281
register unsigned long lastPoint;
00282
register unsigned long aVal;
00283
register long i;
00284
register UINT16 u;
00285
00286
#ifdef DEBUG_OUTPUT
00287
OSErr err=
noErr;
00288
#endif
00289
00290 aVal = 0;
00291 lastPoint = (1<<needBits)-1;
00292
do{
00293 u = (
UINT16)(aVal<<(16-needBits));
00294 u |= u >> needBits;
00295 u |= u >> (2*needBits);
00296 u |= u >> (4*needBits);
00297 **Poi = u;
00298 (*Poi)++;
00299
if( current < inputDim-1 )
SetValues16( current+1, inputDim, needBits, Poi );
00300
if( aVal < lastPoint ){
00301
if( current > 0 )
for( i=0; i<current; ++i)*((*Poi) + i) = *((*Poi) - inputDim + i);
00302 (*Poi) += current;
00303 }
00304 aVal++;
00305 }
while( aVal <= lastPoint );
00306
00307 }
00308
00309
#endif
00310
00311
OSErr
00312 MakeCMColorCube(
long inputDim,
00313
long *theCubeSize,
00314 CUBE_DATA_TYPE *aHdlPtr,
00315
long *theBits )
00316 {
00317
long needBits,theSize;
00318
long i,gridPoints;
00319
OSErr err =
noErr;
00320
UINT16 *cube =
nil;
00321
register long aMaxVal;
00322
register long aShift;
00323
long aRound;
00324
00325
LH_START_PROC(
"MakeCMColorCube")
00326
00327 err =
CalcGridPoints4Cube(*theCubeSize, inputDim, &gridPoints, &needBits);
00328
if (err)
00329
goto CleanupAndExit;
00330
00331 theSize = gridPoints;
00332
for( i=1; i<inputDim; ++i)theSize *= gridPoints;
00333
00334
if( inputDim > 4 )
00335 {
00336 err =
cmparamErr;
00337
goto CleanupAndExit;
00338 }
00339 *theCubeSize = theSize * inputDim;
00340
if( inputDim != 4 )
00341 *theCubeSize = theSize * 4;
00342
00343 *aHdlPtr =
ALLOC_DATA(*theCubeSize*2, &err);
00344
if (err !=
noErr)
00345
goto CleanupAndExit;
00346
00347
LOCK_DATA( *aHdlPtr );
00348 cube = (
UINT16 *)
DATA_2_PTR( *aHdlPtr );
00349 aMaxVal = (1<<(30-needBits))-1;
00350 aMaxVal = aMaxVal / ( gridPoints -1 );
00351 aShift = 30 - needBits - 16;
00352 aRound = 0;
00353
if( inputDim == 1 )
00354 {
00355
for( i=0; i<gridPoints; ++i){
00356 *cube++ = (
UINT16)(( aMaxVal * i + aRound) >> aShift);
00357 *cube++ = 0;
00358 *cube++ = 0;
00359 *cube++ = 0;
00360 }
00361 }
00362
else if( inputDim == 2)
00363 {
00364
register long j;
00365
register UINT16 aI;
00366
for( i=0; i<gridPoints; ++i){
00367 aI = (
UINT16)(( aMaxVal * i + aRound ) >> aShift);
00368
for( j=0; j<gridPoints; ++j){
00369 *cube++ = aI;
00370 *cube++ = (
UINT16)(( aMaxVal * j + aRound ) >> aShift);
00371 *cube++ = 0;
00372 *cube++ = 0;
00373 }
00374 }
00375 }
00376
else if( inputDim == 3)
00377 {
00378
register long j,k;
00379
register UINT16 aI, aJ;
00380
for( i=0; i<gridPoints; ++i){
00381 aI = (
UINT16)(( aMaxVal * i + aRound ) >> aShift);
00382
for( j=0; j<gridPoints; ++j){
00383 aJ = (
UINT16)(( aMaxVal * j + aRound ) >> aShift);
00384
for( k=0; k<gridPoints; ++k){
00385 *cube++ = aI;
00386 *cube++ = aJ;
00387 *cube++ = (
UINT16)(( aMaxVal * k + aRound ) >> aShift);
00388 *cube++ = 0;
00389 }
00390 }
00391 }
00392 }
00393
else if( inputDim == 4){
00394
register long j,k,l;
00395
register UINT16 aI, aJ,aK;
00396
for( i=0; i<gridPoints; ++i){
00397 aI = (
UINT16)(( aMaxVal * i + aRound ) >> aShift);
00398
for( j=0; j<gridPoints; ++j){
00399 aJ = (
UINT16)(( aMaxVal * j + aRound ) >> aShift);
00400
for( k=0; k<gridPoints; ++k){
00401 aK = (
UINT16)(( aMaxVal * k + aRound ) >> aShift);
00402
for( l=0; l<gridPoints; ++l){
00403 *cube++ = aI;
00404 *cube++ = aJ;
00405 *cube++ = aK;
00406 *cube++ = (
UINT16)(( aMaxVal * l + aRound ) >> aShift);
00407 }
00408 }
00409 }
00410 }
00411 }
00412
else
00413 {
00414 *aHdlPtr =
DISPOSE_IF_DATA(*aHdlPtr);
00415 err =
cmparamErr;
00416
goto CleanupAndExit;
00417 }
00418
UNLOCK_DATA(*aHdlPtr);
00419 *theBits = needBits;
00420 CleanupAndExit:
00421
LH_END_PROC(
"MakeCMColorCube")
00422
return err;
00423 }
00424
00425 void SetValues(
long current,
long inputDim,
long needBits, UINT8 **Poi )
00426 {
00427
register UINT16 lastPoint;
00428
register UINT16 aVal;
00429
register long i;
00430
00431
#ifdef DEBUG_OUTPUT
00432
OSErr err=
noErr;
00433
#endif
00434
LH_START_PROC(
"SetValues")
00435 aVal = 0;
00436 lastPoint = (1<<needBits)-1;
00437
do{
00438 **Poi = (
UINT8)(aVal<<(8-needBits));
00439 (*Poi)++;
00440
if( current < inputDim-1 )
SetValues( current+1, inputDim, needBits, Poi );
00441
if( aVal < lastPoint ){
00442
if( current > 0 )
for( i=0; i<current; ++i)*((*Poi) + i) = *((*Poi) - inputDim + i);
00443 (*Poi) += current;
00444 }
00445 aVal++;
00446 }
while( aVal <= lastPoint );
00447
LH_END_PROC(
"SetValues")
00448 }
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478