jdk/src/share/native/sun/java2d/cmm/lcms/cmsmatsh.c
changeset 6570 6fb864c9fe0b
parent 6569 eb3c76a898eb
parent 6563 7e0bcb97526a
child 6604 657f846b9848
equal deleted inserted replaced
6569:eb3c76a898eb 6570:6fb864c9fe0b
     1 /*
       
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     3  *
       
     4  * This code is free software; you can redistribute it and/or modify it
       
     5  * under the terms of the GNU General Public License version 2 only, as
       
     6  * published by the Free Software Foundation.  Oracle designates this
       
     7  * particular file as subject to the "Classpath" exception as provided
       
     8  * by Oracle in the LICENSE file that accompanied this code.
       
     9  *
       
    10  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    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 //  Little cms
       
    32 //  Copyright (C) 1998-2007 Marti Maria
       
    33 //
       
    34 // Permission is hereby granted, free of charge, to any person obtaining
       
    35 // a copy of this software and associated documentation files (the "Software"),
       
    36 // to deal in the Software without restriction, including without limitation
       
    37 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
       
    38 // and/or sell copies of the Software, and to permit persons to whom the Software
       
    39 // is furnished to do so, subject to the following conditions:
       
    40 //
       
    41 // The above copyright notice and this permission notice shall be included in
       
    42 // all copies or substantial portions of the Software.
       
    43 //
       
    44 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    45 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
       
    46 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
       
    47 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
       
    48 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
       
    49 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
       
    50 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
       
    51 
       
    52 
       
    53 #include "lcms.h"
       
    54 
       
    55 
       
    56 // Shaper/Matrix handling
       
    57 // This routines handles the matrix-shaper method. A note about domain
       
    58 // is here required. If the shaper-matrix is invoked on INPUT profiles,
       
    59 // after the shaper process, we have a value between 0 and 0xFFFF. Thus,
       
    60 // for proper matrix handling, we must convert it to 15fix16, so
       
    61 // ToFixedDomain might be called. But cmsLinearInterpFixed() returns
       
    62 // data yet in fixed point, so no additional process is required.
       
    63 // Then, we obtain data on 15.16, so we need to shift >> by 1 to
       
    64 // obtain 1.15 PCS format.
       
    65 
       
    66 // On OUTPUT profiles, things are inverse, we must first expand 1 bit
       
    67 // by shifting left, and then convert result between 0 and 1.000 to
       
    68 // RGB, so FromFixedDomain() must be called before pass values to
       
    69 // shaper. Trickly, there is a situation where this shifts works
       
    70 // little different. Sometimes, lcms smelts input/output
       
    71 // matrices into a single, one shaper, process. In such cases, since
       
    72 // input is encoded from 0 to 0xffff, we must first use the shaper and
       
    73 // then the matrix, an additional FromFixedDomain() must be used to
       
    74 // accomodate output values.
       
    75 
       
    76 // For a sake of simplicity, I will handle this three behaviours
       
    77 // with different routines, so the flags MATSHAPER_INPUT and MATSHAPER_OUTPUT
       
    78 // can be conbined to signal smelted matrix-shapers
       
    79 
       
    80 
       
    81 
       
    82 static
       
    83 int ComputeTables(LPGAMMATABLE Table[3], LPWORD Out[3], LPL16PARAMS p16)
       
    84 {
       
    85     int i, AllLinear;
       
    86 
       
    87        cmsCalcL16Params(Table[0] -> nEntries, p16);
       
    88 
       
    89        AllLinear = 0;
       
    90        for (i=0; i < 3; i++)
       
    91        {
       
    92         LPWORD PtrW;
       
    93 
       
    94         PtrW = (LPWORD) _cmsMalloc(sizeof(WORD) * p16 -> nSamples);
       
    95 
       
    96         if (PtrW == NULL) return -1;  // Signal error
       
    97 
       
    98         CopyMemory(PtrW, Table[i] -> GammaTable, sizeof(WORD) * Table[i] -> nEntries);
       
    99 
       
   100         Out[i] = PtrW;      // Set table pointer
       
   101 
       
   102         // Linear after all?
       
   103 
       
   104         AllLinear   += cmsIsLinear(PtrW, p16 -> nSamples);
       
   105        }
       
   106 
       
   107        // If is all linear, then supress table interpolation (this
       
   108        // will speed greately some trivial operations.
       
   109        // Return 1 if present, 0 if all linear
       
   110 
       
   111 
       
   112        if (AllLinear != 3) return 1;
       
   113 
       
   114        return 0;
       
   115 
       
   116 }
       
   117 
       
   118 
       
   119 LPMATSHAPER cmsAllocMatShaper2(LPMAT3 Matrix, LPGAMMATABLE In[], LPGAMMATABLE Out[], DWORD Behaviour)
       
   120 {
       
   121        LPMATSHAPER NewMatShaper;
       
   122        int rc;
       
   123 
       
   124        NewMatShaper = (LPMATSHAPER) _cmsMalloc(sizeof(MATSHAPER));
       
   125        if (NewMatShaper)
       
   126               ZeroMemory(NewMatShaper, sizeof(MATSHAPER));
       
   127 
       
   128        NewMatShaper->dwFlags = Behaviour & (MATSHAPER_ALLSMELTED);
       
   129 
       
   130        // Fill matrix part
       
   131 
       
   132        MAT3toFix(&NewMatShaper -> Matrix, Matrix);
       
   133 
       
   134        // Reality check
       
   135 
       
   136        if (!MAT3isIdentity(&NewMatShaper -> Matrix, 0.00001))
       
   137                      NewMatShaper -> dwFlags |= MATSHAPER_HASMATRIX;
       
   138 
       
   139        // Now, on the table characteristics
       
   140 
       
   141        if (Out) {
       
   142 
       
   143             rc = ComputeTables(Out, NewMatShaper ->L, &NewMatShaper ->p16);
       
   144             if (rc < 0) {
       
   145                  cmsFreeMatShaper(NewMatShaper);
       
   146                  return NULL;
       
   147             }
       
   148             if (rc == 1) NewMatShaper -> dwFlags |= MATSHAPER_HASSHAPER;
       
   149        }
       
   150 
       
   151 
       
   152        if (In) {
       
   153 
       
   154             rc = ComputeTables(In, NewMatShaper ->L2, &NewMatShaper ->p2_16);
       
   155             if (rc < 0) {
       
   156                 cmsFreeMatShaper(NewMatShaper);
       
   157                 return NULL;
       
   158             }
       
   159             if (rc == 1) NewMatShaper -> dwFlags |= MATSHAPER_HASINPSHAPER;
       
   160        }
       
   161 
       
   162 
       
   163        return NewMatShaper;
       
   164 
       
   165 }
       
   166 
       
   167 
       
   168 
       
   169 // Creation & Destruction
       
   170 
       
   171 LPMATSHAPER cmsAllocMatShaper(LPMAT3 Matrix, LPGAMMATABLE Tables[], DWORD Behaviour)
       
   172 {
       
   173        LPMATSHAPER NewMatShaper;
       
   174        int i, AllLinear;
       
   175 
       
   176            if (Matrix == NULL) return NULL;
       
   177            for (i=0; i < 3; i++) {
       
   178 
       
   179                    if (Tables[i] == NULL) return NULL;
       
   180            }
       
   181 
       
   182        NewMatShaper = (LPMATSHAPER) _cmsMalloc(sizeof(MATSHAPER));
       
   183        if (NewMatShaper)
       
   184               ZeroMemory(NewMatShaper, sizeof(MATSHAPER));
       
   185 
       
   186        NewMatShaper->dwFlags = Behaviour & (MATSHAPER_ALLSMELTED);
       
   187 
       
   188        // Fill matrix part
       
   189 
       
   190        MAT3toFix(&NewMatShaper -> Matrix, Matrix);
       
   191 
       
   192        // Reality check
       
   193 
       
   194        if (!MAT3isIdentity(&NewMatShaper -> Matrix, 0.00001))
       
   195                      NewMatShaper -> dwFlags |= MATSHAPER_HASMATRIX;
       
   196 
       
   197        // Now, on the table characteristics
       
   198        cmsCalcL16Params(Tables[0] -> nEntries, &NewMatShaper -> p16);
       
   199 
       
   200        // Copy tables
       
   201 
       
   202        AllLinear = 0;
       
   203        for (i=0; i < 3; i++) {
       
   204 
       
   205         LPWORD PtrW;
       
   206 
       
   207         PtrW = (LPWORD) _cmsMalloc(sizeof(WORD) * NewMatShaper -> p16.nSamples);
       
   208 
       
   209         if (PtrW == NULL) {
       
   210               cmsFreeMatShaper(NewMatShaper);
       
   211               return NULL;
       
   212         }
       
   213 
       
   214         CopyMemory(PtrW, Tables[i] -> GammaTable,
       
   215                             sizeof(WORD) * Tables[i] -> nEntries);
       
   216 
       
   217         NewMatShaper -> L[i] = PtrW;      // Set table pointer
       
   218 
       
   219         // Linear after all?
       
   220 
       
   221         AllLinear   += cmsIsLinear(PtrW, NewMatShaper -> p16.nSamples);
       
   222        }
       
   223 
       
   224        // If is all linear, then supress table interpolation (this
       
   225        // will speed greately some trivial operations
       
   226 
       
   227        if (AllLinear != 3)
       
   228               NewMatShaper -> dwFlags |= MATSHAPER_HASSHAPER;
       
   229 
       
   230        return NewMatShaper;
       
   231 }
       
   232 
       
   233 
       
   234 
       
   235 // Free associated memory
       
   236 
       
   237 void cmsFreeMatShaper(LPMATSHAPER MatShaper)
       
   238 {
       
   239        int i;
       
   240 
       
   241        if (!MatShaper) return;
       
   242 
       
   243        for (i=0; i < 3; i++)
       
   244        {
       
   245               if (MatShaper -> L[i]) _cmsFree(MatShaper ->L[i]);
       
   246               if (MatShaper -> L2[i]) _cmsFree(MatShaper ->L2[i]);
       
   247        }
       
   248 
       
   249        _cmsFree(MatShaper);
       
   250 }
       
   251 
       
   252 
       
   253 // All smelted must postpose gamma to last stage.
       
   254 
       
   255 static
       
   256 void AllSmeltedBehaviour(LPMATSHAPER MatShaper, WORD In[], WORD Out[])
       
   257 {
       
   258 
       
   259        WORD tmp[3];
       
   260        WVEC3 InVect, OutVect;
       
   261 
       
   262        if (MatShaper -> dwFlags & MATSHAPER_HASINPSHAPER)
       
   263        {
       
   264        InVect.n[VX] = cmsLinearInterpFixed(In[0], MatShaper -> L2[0], &MatShaper -> p2_16);
       
   265        InVect.n[VY] = cmsLinearInterpFixed(In[1], MatShaper -> L2[1], &MatShaper -> p2_16);
       
   266        InVect.n[VZ] = cmsLinearInterpFixed(In[2], MatShaper -> L2[2], &MatShaper -> p2_16);
       
   267        }
       
   268        else
       
   269        {
       
   270             InVect.n[VX] = ToFixedDomain(In[0]);
       
   271             InVect.n[VY] = ToFixedDomain(In[1]);
       
   272             InVect.n[VZ] = ToFixedDomain(In[2]);
       
   273        }
       
   274 
       
   275 
       
   276        if (MatShaper -> dwFlags & MATSHAPER_HASMATRIX)
       
   277        {
       
   278 
       
   279              MAT3evalW(&OutVect, &MatShaper -> Matrix, &InVect);
       
   280        }
       
   281        else {
       
   282 
       
   283            OutVect.n[VX] = InVect.n[VX];
       
   284            OutVect.n[VY] = InVect.n[VY];
       
   285            OutVect.n[VZ] = InVect.n[VZ];
       
   286        }
       
   287 
       
   288 
       
   289        tmp[0] = _cmsClampWord(FromFixedDomain(OutVect.n[VX]));
       
   290        tmp[1] = _cmsClampWord(FromFixedDomain(OutVect.n[VY]));
       
   291        tmp[2] = _cmsClampWord(FromFixedDomain(OutVect.n[VZ]));
       
   292 
       
   293 
       
   294 
       
   295        if (MatShaper -> dwFlags & MATSHAPER_HASSHAPER)
       
   296        {
       
   297        Out[0] = cmsLinearInterpLUT16(tmp[0], MatShaper -> L[0], &MatShaper -> p16);
       
   298        Out[1] = cmsLinearInterpLUT16(tmp[1], MatShaper -> L[1], &MatShaper -> p16);
       
   299        Out[2] = cmsLinearInterpLUT16(tmp[2], MatShaper -> L[2], &MatShaper -> p16);
       
   300        }
       
   301        else
       
   302        {
       
   303            Out[0] = tmp[0];
       
   304            Out[1] = tmp[1];
       
   305            Out[2] = tmp[2];
       
   306        }
       
   307 
       
   308 }
       
   309 
       
   310 
       
   311 static
       
   312 void InputBehaviour(LPMATSHAPER MatShaper, WORD In[], WORD Out[])
       
   313 {
       
   314        WVEC3 InVect, OutVect;
       
   315 
       
   316 
       
   317        if (MatShaper -> dwFlags & MATSHAPER_HASSHAPER)
       
   318        {
       
   319        InVect.n[VX] = cmsLinearInterpFixed(In[0], MatShaper -> L[0], &MatShaper -> p16);
       
   320        InVect.n[VY] = cmsLinearInterpFixed(In[1], MatShaper -> L[1], &MatShaper -> p16);
       
   321        InVect.n[VZ] = cmsLinearInterpFixed(In[2], MatShaper -> L[2], &MatShaper -> p16);
       
   322        }
       
   323        else
       
   324        {
       
   325        InVect.n[VX] = ToFixedDomain(In[0]);
       
   326        InVect.n[VY] = ToFixedDomain(In[1]);
       
   327        InVect.n[VZ] = ToFixedDomain(In[2]);
       
   328        }
       
   329 
       
   330        if (MatShaper -> dwFlags & MATSHAPER_HASMATRIX)
       
   331        {
       
   332               MAT3evalW(&OutVect, &MatShaper -> Matrix, &InVect);
       
   333        }
       
   334        else
       
   335        {
       
   336        OutVect =  InVect;
       
   337        }
       
   338 
       
   339        // PCS in 1Fixed15 format, adjusting
       
   340 
       
   341        Out[0] = _cmsClampWord((OutVect.n[VX]) >> 1);
       
   342        Out[1] = _cmsClampWord((OutVect.n[VY]) >> 1);
       
   343        Out[2] = _cmsClampWord((OutVect.n[VZ]) >> 1);
       
   344 
       
   345 }
       
   346 
       
   347 
       
   348 static
       
   349 void OutputBehaviour(LPMATSHAPER MatShaper, WORD In[], WORD Out[])
       
   350 {
       
   351        WVEC3 InVect, OutVect;
       
   352        int i;
       
   353 
       
   354        // We need to convert from XYZ to RGB, here we must
       
   355        // shift << 1 to pass between 1.15 to 15.16 formats
       
   356 
       
   357        InVect.n[VX] = (Fixed32) In[0] << 1;
       
   358        InVect.n[VY] = (Fixed32) In[1] << 1;
       
   359        InVect.n[VZ] = (Fixed32) In[2] << 1;
       
   360 
       
   361        if (MatShaper -> dwFlags & MATSHAPER_HASMATRIX)
       
   362        {
       
   363               MAT3evalW(&OutVect, &MatShaper -> Matrix, &InVect);
       
   364        }
       
   365        else
       
   366        {
       
   367        OutVect = InVect;
       
   368        }
       
   369 
       
   370 
       
   371        if (MatShaper -> dwFlags & MATSHAPER_HASSHAPER)
       
   372        {
       
   373               for (i=0; i < 3; i++)
       
   374               {
       
   375 
       
   376               Out[i] = cmsLinearInterpLUT16(
       
   377                      _cmsClampWord(FromFixedDomain(OutVect.n[i])),
       
   378                      MatShaper -> L[i],
       
   379                      &MatShaper ->p16);
       
   380               }
       
   381        }
       
   382        else
       
   383        {
       
   384        // Result from fixed domain to RGB
       
   385 
       
   386        Out[0] = _cmsClampWord(FromFixedDomain(OutVect.n[VX]));
       
   387        Out[1] = _cmsClampWord(FromFixedDomain(OutVect.n[VY]));
       
   388        Out[2] = _cmsClampWord(FromFixedDomain(OutVect.n[VZ]));
       
   389        }
       
   390 
       
   391 }
       
   392 
       
   393 
       
   394 // Master on evaluating shapers, 3 different behaviours
       
   395 
       
   396 void cmsEvalMatShaper(LPMATSHAPER MatShaper, WORD In[], WORD Out[])
       
   397 {
       
   398 
       
   399        if ((MatShaper -> dwFlags & MATSHAPER_ALLSMELTED) == MATSHAPER_ALLSMELTED)
       
   400        {
       
   401               AllSmeltedBehaviour(MatShaper, In, Out);
       
   402               return;
       
   403        }
       
   404        if (MatShaper -> dwFlags & MATSHAPER_INPUT)
       
   405        {
       
   406               InputBehaviour(MatShaper, In, Out);
       
   407               return;
       
   408        }
       
   409 
       
   410        OutputBehaviour(MatShaper, In, Out);
       
   411 }