< prev index next >

src/java.desktop/share/native/liblcms/cmslut.c

Print this page




  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 // This file is available under and governed by the GNU General Public
  26 // License version 2 only, as published by the Free Software Foundation.
  27 // However, the following notice accompanied the original version of this
  28 // file:
  29 //
  30 //---------------------------------------------------------------------------------
  31 //
  32 //  Little Color Management System
  33 //  Copyright (c) 1998-2017 Marti Maria Saguer
  34 //
  35 // Permission is hereby granted, free of charge, to any person obtaining
  36 // a copy of this software and associated documentation files (the "Software"),
  37 // to deal in the Software without restriction, including without limitation
  38 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  39 // and/or sell copies of the Software, and to permit persons to whom the Software
  40 // is furnished to do so, subject to the following conditions:
  41 //
  42 // The above copyright notice and this permission notice shall be included in
  43 // all copies or substantial portions of the Software.
  44 //
  45 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  46 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  47 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  48 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  49 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  50 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  51 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  52 //
  53 //---------------------------------------------------------------------------------


 409                                      const cmsFloat64Number* Matrix, const cmsFloat64Number* Offset)
 410 {
 411     cmsUInt32Number i, n;
 412     _cmsStageMatrixData* NewElem;
 413     cmsStage* NewMPE;
 414 
 415     n = Rows * Cols;
 416 
 417     // Check for overflow
 418     if (n == 0) return NULL;
 419     if (n >= UINT_MAX / Cols) return NULL;
 420     if (n >= UINT_MAX / Rows) return NULL;
 421     if (n < Rows || n < Cols) return NULL;
 422 
 423     NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigMatrixElemType, Cols, Rows,
 424                                      EvaluateMatrix, MatrixElemDup, MatrixElemTypeFree, NULL );
 425     if (NewMPE == NULL) return NULL;
 426 
 427 
 428     NewElem = (_cmsStageMatrixData*) _cmsMallocZero(ContextID, sizeof(_cmsStageMatrixData));
 429     if (NewElem == NULL) return NULL;
 430 
 431 
 432     NewElem ->Double = (cmsFloat64Number*) _cmsCalloc(ContextID, n, sizeof(cmsFloat64Number));
 433 
 434     if (NewElem->Double == NULL) {
 435         MatrixElemTypeFree(NewMPE);
 436         return NULL;
 437     }
 438 
 439     for (i=0; i < n; i++) {
 440         NewElem ->Double[i] = Matrix[i];
 441     }
 442 
 443 
 444     if (Offset != NULL) {
 445 
 446         NewElem ->Offset = (cmsFloat64Number*) _cmsCalloc(ContextID, Rows, sizeof(cmsFloat64Number));
 447         if (NewElem->Offset == NULL) {
 448            MatrixElemTypeFree(NewMPE);
 449            return NULL;
 450         }
 451 
 452         for (i=0; i < Rows; i++) {
 453                 NewElem ->Offset[i] = Offset[i];
 454         }
 455 
 456     }
 457 
 458     NewMPE ->Data  = (void*) NewElem;
 459     return NewMPE;




 460 }
 461 
 462 
 463 // *************************************************************************************************
 464 // Type cmsSigCLutElemType
 465 // *************************************************************************************************
 466 
 467 
 468 // Evaluate in true floating point
 469 static
 470 void EvaluateCLUTfloat(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
 471 {
 472     _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data;
 473 
 474     Data -> Params ->Interpolation.LerpFloat(In, Out, Data->Params);
 475 }
 476 
 477 
 478 // Convert to 16 bits, evaluate, and back to floating point
 479 static


 712         return NULL;
 713     }
 714 
 715     if (Table != NULL) {
 716         for (i=0; i < n; i++) {
 717             NewElem ->Tab.TFloat[i] = Table[i];
 718         }
 719     }
 720 
 721     NewElem ->Params = _cmsComputeInterpParamsEx(ContextID, clutPoints,  inputChan, outputChan, NewElem ->Tab.TFloat, CMS_LERP_FLAGS_FLOAT);
 722     if (NewElem ->Params == NULL) {
 723         cmsStageFree(NewMPE);
 724         return NULL;
 725     }
 726 
 727     return NewMPE;
 728 }
 729 
 730 
 731 static
 732 int IdentitySampler(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void * Cargo)
 733 {
 734     int nChan = *(int*) Cargo;
 735     int i;
 736 
 737     for (i=0; i < nChan; i++)
 738         Out[i] = In[i];
 739 
 740     return 1;
 741 }
 742 
 743 // Creates an MPE that just copies input to output
 744 cmsStage* CMSEXPORT _cmsStageAllocIdentityCLut(cmsContext ContextID, cmsUInt32Number nChan)
 745 {
 746     cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS];
 747     cmsStage* mpe ;
 748     int i;
 749 
 750     for (i=0; i < MAX_INPUT_DIMENSIONS; i++)
 751         Dimensions[i] = 2;
 752 


1329         // Check chain consistency
1330         prev = First;
1331         next = prev->Next;
1332 
1333         while (next != NULL)
1334         {
1335             if (next->InputChannels != prev->OutputChannels)
1336                 return FALSE;
1337 
1338             next = next->Next;
1339             prev = prev->Next;
1340     }
1341 }
1342 
1343     return TRUE;
1344 }
1345 
1346 
1347 // Default to evaluate the LUT on 16 bit-basis. Precision is retained.
1348 static
1349 void _LUTeval16(register const cmsUInt16Number In[], register cmsUInt16Number Out[],  register const void* D)
1350 {
1351     cmsPipeline* lut = (cmsPipeline*) D;
1352     cmsStage *mpe;
1353     cmsFloat32Number Storage[2][MAX_STAGE_CHANNELS];
1354     int Phase = 0, NextPhase;
1355 
1356     From16ToFloat(In, &Storage[Phase][0], lut ->InputChannels);
1357 
1358     for (mpe = lut ->Elements;
1359          mpe != NULL;
1360          mpe = mpe ->Next) {
1361 
1362              NextPhase = Phase ^ 1;
1363              mpe ->EvalPtr(&Storage[Phase][0], &Storage[NextPhase][0], mpe);
1364              Phase = NextPhase;
1365     }
1366 
1367 
1368     FromFloatTo16(&Storage[Phase][0], Out, lut ->OutputChannels);
1369 }
1370 
1371 
1372 
1373 // Does evaluate the LUT on cmsFloat32Number-basis.
1374 static
1375 void _LUTevalFloat(register const cmsFloat32Number In[], register cmsFloat32Number Out[], const void* D)
1376 {
1377     cmsPipeline* lut = (cmsPipeline*) D;
1378     cmsStage *mpe;
1379     cmsFloat32Number Storage[2][MAX_STAGE_CHANNELS];
1380     int Phase = 0, NextPhase;
1381 
1382     memmove(&Storage[Phase][0], In, lut ->InputChannels  * sizeof(cmsFloat32Number));
1383 
1384     for (mpe = lut ->Elements;
1385          mpe != NULL;
1386          mpe = mpe ->Next) {
1387 
1388               NextPhase = Phase ^ 1;
1389               mpe ->EvalPtr(&Storage[Phase][0], &Storage[NextPhase][0], mpe);
1390               Phase = NextPhase;
1391     }
1392 
1393     memmove(Out, &Storage[Phase][0], lut ->OutputChannels * sizeof(cmsFloat32Number));
1394 }
1395 




  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 // This file is available under and governed by the GNU General Public
  26 // License version 2 only, as published by the Free Software Foundation.
  27 // However, the following notice accompanied the original version of this
  28 // file:
  29 //
  30 //---------------------------------------------------------------------------------
  31 //
  32 //  Little Color Management System
  33 //  Copyright (c) 1998-2020 Marti Maria Saguer
  34 //
  35 // Permission is hereby granted, free of charge, to any person obtaining
  36 // a copy of this software and associated documentation files (the "Software"),
  37 // to deal in the Software without restriction, including without limitation
  38 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  39 // and/or sell copies of the Software, and to permit persons to whom the Software
  40 // is furnished to do so, subject to the following conditions:
  41 //
  42 // The above copyright notice and this permission notice shall be included in
  43 // all copies or substantial portions of the Software.
  44 //
  45 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  46 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  47 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  48 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  49 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  50 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  51 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  52 //
  53 //---------------------------------------------------------------------------------


 409                                      const cmsFloat64Number* Matrix, const cmsFloat64Number* Offset)
 410 {
 411     cmsUInt32Number i, n;
 412     _cmsStageMatrixData* NewElem;
 413     cmsStage* NewMPE;
 414 
 415     n = Rows * Cols;
 416 
 417     // Check for overflow
 418     if (n == 0) return NULL;
 419     if (n >= UINT_MAX / Cols) return NULL;
 420     if (n >= UINT_MAX / Rows) return NULL;
 421     if (n < Rows || n < Cols) return NULL;
 422 
 423     NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigMatrixElemType, Cols, Rows,
 424                                      EvaluateMatrix, MatrixElemDup, MatrixElemTypeFree, NULL );
 425     if (NewMPE == NULL) return NULL;
 426 
 427 
 428     NewElem = (_cmsStageMatrixData*) _cmsMallocZero(ContextID, sizeof(_cmsStageMatrixData));
 429     if (NewElem == NULL) goto Error;
 430     NewMPE->Data = (void*)NewElem;
 431 
 432     NewElem ->Double = (cmsFloat64Number*) _cmsCalloc(ContextID, n, sizeof(cmsFloat64Number));
 433     if (NewElem->Double == NULL) goto Error;




 434 
 435     for (i=0; i < n; i++) {
 436         NewElem ->Double[i] = Matrix[i];
 437     }
 438 

 439     if (Offset != NULL) {
 440 
 441         NewElem ->Offset = (cmsFloat64Number*) _cmsCalloc(ContextID, Rows, sizeof(cmsFloat64Number));
 442         if (NewElem->Offset == NULL) goto Error;



 443 
 444         for (i=0; i < Rows; i++) {
 445                 NewElem ->Offset[i] = Offset[i];
 446         }

 447     }
 448 

 449     return NewMPE;
 450 
 451 Error:
 452     cmsStageFree(NewMPE);
 453     return NULL;
 454 }
 455 
 456 
 457 // *************************************************************************************************
 458 // Type cmsSigCLutElemType
 459 // *************************************************************************************************
 460 
 461 
 462 // Evaluate in true floating point
 463 static
 464 void EvaluateCLUTfloat(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
 465 {
 466     _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data;
 467 
 468     Data -> Params ->Interpolation.LerpFloat(In, Out, Data->Params);
 469 }
 470 
 471 
 472 // Convert to 16 bits, evaluate, and back to floating point
 473 static


 706         return NULL;
 707     }
 708 
 709     if (Table != NULL) {
 710         for (i=0; i < n; i++) {
 711             NewElem ->Tab.TFloat[i] = Table[i];
 712         }
 713     }
 714 
 715     NewElem ->Params = _cmsComputeInterpParamsEx(ContextID, clutPoints,  inputChan, outputChan, NewElem ->Tab.TFloat, CMS_LERP_FLAGS_FLOAT);
 716     if (NewElem ->Params == NULL) {
 717         cmsStageFree(NewMPE);
 718         return NULL;
 719     }
 720 
 721     return NewMPE;
 722 }
 723 
 724 
 725 static
 726 int IdentitySampler(CMSREGISTER const cmsUInt16Number In[], CMSREGISTER cmsUInt16Number Out[], CMSREGISTER void * Cargo)
 727 {
 728     int nChan = *(int*) Cargo;
 729     int i;
 730 
 731     for (i=0; i < nChan; i++)
 732         Out[i] = In[i];
 733 
 734     return 1;
 735 }
 736 
 737 // Creates an MPE that just copies input to output
 738 cmsStage* CMSEXPORT _cmsStageAllocIdentityCLut(cmsContext ContextID, cmsUInt32Number nChan)
 739 {
 740     cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS];
 741     cmsStage* mpe ;
 742     int i;
 743 
 744     for (i=0; i < MAX_INPUT_DIMENSIONS; i++)
 745         Dimensions[i] = 2;
 746 


1323         // Check chain consistency
1324         prev = First;
1325         next = prev->Next;
1326 
1327         while (next != NULL)
1328         {
1329             if (next->InputChannels != prev->OutputChannels)
1330                 return FALSE;
1331 
1332             next = next->Next;
1333             prev = prev->Next;
1334     }
1335 }
1336 
1337     return TRUE;
1338 }
1339 
1340 
1341 // Default to evaluate the LUT on 16 bit-basis. Precision is retained.
1342 static
1343 void _LUTeval16(CMSREGISTER const cmsUInt16Number In[], CMSREGISTER cmsUInt16Number Out[],  CMSREGISTER const void* D)
1344 {
1345     cmsPipeline* lut = (cmsPipeline*) D;
1346     cmsStage *mpe;
1347     cmsFloat32Number Storage[2][MAX_STAGE_CHANNELS];
1348     int Phase = 0, NextPhase;
1349 
1350     From16ToFloat(In, &Storage[Phase][0], lut ->InputChannels);
1351 
1352     for (mpe = lut ->Elements;
1353          mpe != NULL;
1354          mpe = mpe ->Next) {
1355 
1356              NextPhase = Phase ^ 1;
1357              mpe ->EvalPtr(&Storage[Phase][0], &Storage[NextPhase][0], mpe);
1358              Phase = NextPhase;
1359     }
1360 
1361 
1362     FromFloatTo16(&Storage[Phase][0], Out, lut ->OutputChannels);
1363 }
1364 
1365 
1366 
1367 // Does evaluate the LUT on cmsFloat32Number-basis.
1368 static
1369 void _LUTevalFloat(CMSREGISTER const cmsFloat32Number In[], CMSREGISTER cmsFloat32Number Out[], const void* D)
1370 {
1371     cmsPipeline* lut = (cmsPipeline*) D;
1372     cmsStage *mpe;
1373     cmsFloat32Number Storage[2][MAX_STAGE_CHANNELS];
1374     int Phase = 0, NextPhase;
1375 
1376     memmove(&Storage[Phase][0], In, lut ->InputChannels  * sizeof(cmsFloat32Number));
1377 
1378     for (mpe = lut ->Elements;
1379          mpe != NULL;
1380          mpe = mpe ->Next) {
1381 
1382               NextPhase = Phase ^ 1;
1383               mpe ->EvalPtr(&Storage[Phase][0], &Storage[NextPhase][0], mpe);
1384               Phase = NextPhase;
1385     }
1386 
1387     memmove(Out, &Storage[Phase][0], lut ->OutputChannels * sizeof(cmsFloat32Number));
1388 }
1389 


< prev index next >