📄 matrixbasedtransformtosrgb.java
字号:
* @param ncols number of columns in the input * @param nrows number of rows in the input * @param inb input data block * @param outb output data block * @exception MatrixBasedTransformException */ public void apply (DataBlkInt inb [], DataBlkInt outb []) throws MatrixBasedTransformException { int [][] in = new int [3][], out = new int [3][]; // data references. int nrows = inb[0].h, ncols = inb[0].w; if ((fBuf == null) || (fBuf[0].length < ncols*nrows)) { fBuf = new float [3][ncols*nrows];} // for each component (rgb) for (int c=0; c<3; ++c) { // Reference the input and output samples. in[c] = (int []) inb[c].getData(); out[c] = (int []) outb[c].getData(); // Assure a properly sized output buffer. if (out[c]==null || out[c].length < in[c].length) { out[c] = new int [in[c].length]; outb[c].setData(out[c]); } // The first thing to do is to process the input into a standard form // and through the first input LUT, producing floating point output values standardizeMatrixLineThroughLut(inb[c], fBuf[c], dwMaxValue[c], fLut[c]); } // For each row and column float [] ra = fBuf[RED]; float [] ga = fBuf[GREEN]; float [] ba = fBuf[BLUE]; int [] ro = out [RED]; int [] go = out [GREEN]; int [] bo = out [BLUE]; int [] lut32 = lut.lut; double r, g, b; int val, index=0; for (int y = 0; y < inb[0].h; ++y) { int end = index+inb[0].w; while (index < end) { // Calculate the rgb pixel indices for this row / column r = ra[index]; g = ga[index]; b = ba[index]; // Apply the matrix to the intermediate floating point data in order to index the // final LUT. val = (int)(matrix[M00] * r + matrix[M01] * g + matrix[M02] * b + 0.5); // Clip the calculated value if necessary.. if (val < 0) ro[index] = lut32[0]; else if (val >= lut32.length ) ro[index] = lut32[lut32.length-1]; else ro[index] = lut32[val]; val = (int)(matrix[M10] * r + matrix[M11] * g + matrix[M12] * b + 0.5); // Clip the calculated value if necessary.. if (val < 0) go[index] = lut32[0]; else if (val >= lut32.length ) go[index] = lut32[lut32.length-1]; else go[index] = lut32[val]; val = (int)(matrix[M20] * r + matrix[M21] * g + matrix[M22] * b + 0.5); // Clip the calculated value if necessary.. if (val < 0) bo[index] = lut32[0]; else if (val >= lut32.length ) bo[index] = lut32[lut32.length-1]; else bo[index] = lut32[val]; index++; }}} /** * Performs the transform. Pass the input throught the LookUpTableFP, apply the * matrix to the output and finally pass the intermediate buffer through the * LookUpTable8. This operation is designated (LFP*M*L8) * Data are already * constructed. Although the data for only one component is returned, the * transformation must be done for all components, because the matrix application * involves a linear combination of component input to produce the output. * @param ncols number of columns in the input * @param nrows number of rows in the input * @param inb input data block * @param outb output data block * @exception MatrixBasedTransformException */ public void apply (DataBlkFloat inb [], DataBlkFloat outb []) throws MatrixBasedTransformException { float [][] in = new float [3][], out = new float [3][]; // data references. int nrows = inb[0].h, ncols = inb[0].w; if ((fBuf == null) || (fBuf[0].length < ncols*nrows)) { fBuf = new float [3][ncols*nrows]; } // for each component (rgb) for (int c=0; c<3; ++c) { // Reference the input and output pixels. in[c] = (float []) inb[c].getData(); out[c] = (float []) outb[c].getData(); // Assure a properly sized output buffer. if (out[c]==null || out[c].length < in[c].length) { out[c] = new float [in[c].length]; outb[c].setData(out[c]); } // The first thing to do is to process the input into a standard form // and through the first input LUT, producing floating point output values standardizeMatrixLineThroughLut(inb[c], fBuf[c], dwMaxValue[c], fLut[c]); } int [] lut32 = lut.lut; // For each row and column int index=0, val; for (int y = 0; y < inb[0].h; ++y) { int end = index+inb[0].w; while (index < end) { // Calculate the rgb pixel indices for this row / column // Apply the matrix to the intermediate floating point data inorder to index the // final LUT. val = (int)(matrix[M00] * fBuf[RED] [index] + matrix[M01] * fBuf[GREEN][index] + matrix[M02] * fBuf[BLUE] [index] + 0.5); // Clip the calculated value if necessary.. if (val < 0) out[0][index] = lut32[0]; else if (val >= lut32.length ) out[0][index] = lut32[lut32.length-1]; else out[0][index] = lut32[val]; val = (int)(matrix[M10] * fBuf[RED] [index] + matrix[M11] * fBuf[GREEN][index] + matrix[M12] * fBuf[BLUE] [index] + 0.5); // Clip the calculated value if necessary.. if (val < 0) out[1][index] = lut32[0]; else if (val >= lut32.length ) out[1][index] = lut32[lut32.length-1]; else out[1][index] = lut32[val]; val = (int)(matrix[M20] * fBuf[RED] [index] + matrix[M21] * fBuf[GREEN][index] + matrix[M22] * fBuf[BLUE] [index] + 0.5); // Clip the calculated value if necessary.. if (val < 0) out[2][index] = lut32[0]; else if (val >= lut32.length ) out[2][index] = lut32[lut32.length-1]; else out[2][index] = lut32[val]; index++; }} } private static void standardizeMatrixLineThroughLut ( DataBlkInt inb, // input datablock float [] out, // output data reference int dwInputMaxValue, // Maximum value of the input for clipping LookUpTableFP lut // Inital input LUT ) { int wTemp, j=0; int [] in = (int []) inb.getData(); // input pixel reference float [] lutFP = lut.lut; for (int y = inb.uly; y < inb.uly+inb.h; ++y) { for (int x = inb.ulx; x < inb.ulx+inb.w; ++x) { int i = inb.offset+(y-inb.uly)*inb.scanw+(x-inb.ulx); // pixel index. if (in[i] > dwInputMaxValue) wTemp = dwInputMaxValue; else if (in[i] < 0) wTemp = 0; else wTemp = in[i]; out[j++] = lutFP[wTemp]; }}} private static void standardizeMatrixLineThroughLut ( DataBlkFloat inb, // input datablock float [] out, // output data reference float dwInputMaxValue, // Maximum value of the input for clipping LookUpTableFP lut // Inital input LUT ) { int j=0; float wTemp; float [] in = (float []) inb.getData(); // input pixel reference float [] lutFP = lut.lut; for (int y = inb.uly; y < inb.uly+inb.h; ++y) { for (int x = inb.ulx; x < inb.ulx+inb.w; ++x) { int i = inb.offset+(y-inb.uly)*inb.scanw+(x-inb.ulx); // pixel index. if (in[i] > dwInputMaxValue) wTemp = dwInputMaxValue; else if (in[i] < 0) wTemp = 0; else wTemp = in[i]; out[j++] = lutFP[(int)wTemp]; }}} /* end class MatrixBasedTransformTosRGB */ }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -