Path: blob/master/src/java.desktop/share/native/common/java2d/opengl/OGLBufImgOps.c
41159 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.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 it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* 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 WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 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 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#ifndef HEADLESS2627#include <jlong.h>2829#include "OGLBufImgOps.h"30#include "OGLContext.h"31#include "OGLRenderQueue.h"32#include "OGLSurfaceData.h"33#include "GraphicsPrimitiveMgr.h"3435/** Evaluates to true if the given bit is set on the local flags variable. */36#define IS_SET(flagbit) \37(((flags) & (flagbit)) != 0)3839/**************************** ConvolveOp support ****************************/4041/**42* The ConvolveOp shader is fairly straightforward. For each texel in43* the source texture, the shader samples the MxN texels in the surrounding44* area, multiplies each by its corresponding kernel value, and then sums45* them all together to produce a single color result. Finally, the46* resulting value is multiplied by the current OpenGL color, which contains47* the extra alpha value.48*49* Note that this shader source code includes some "holes" marked by "%s".50* This allows us to build different shader programs (e.g. one for51* 3x3, one for 5x5, and so on) simply by filling in these "holes" with52* a call to sprintf(). See the OGLBufImgOps_CreateConvolveProgram() method53* for more details.54*55* REMIND: Currently this shader (and the supporting code in the56* EnableConvolveOp() method) only supports 3x3 and 5x5 filters.57* Early shader-level hardware did not support non-constant sized58* arrays but modern hardware should support them (although I59* don't know of any simple way to find out, other than to compile60* the shader at runtime and see if the drivers complain).61*/62static const char *convolveShaderSource =63// maximum size supported by this shader64"const int MAX_KERNEL_SIZE = %s;"65// image to be convolved66"uniform sampler%s baseImage;"67// image edge limits:68// imgEdge.xy = imgMin.xy (anything < will be treated as edge case)69// imgEdge.zw = imgMax.xy (anything > will be treated as edge case)70"uniform vec4 imgEdge;"71// value for each location in the convolution kernel:72// kernelVals[i].x = offsetX[i]73// kernelVals[i].y = offsetY[i]74// kernelVals[i].z = kernel[i]75"uniform vec3 kernelVals[MAX_KERNEL_SIZE];"76""77"void main(void)"78"{"79" int i;"80" vec4 sum;"81""82" if (any(lessThan(gl_TexCoord[0].st, imgEdge.xy)) ||"83" any(greaterThan(gl_TexCoord[0].st, imgEdge.zw)))"84" {"85// (placeholder for edge condition code)86" %s"87" } else {"88" sum = vec4(0.0);"89" for (i = 0; i < MAX_KERNEL_SIZE; i++) {"90" sum +="91" kernelVals[i].z *"92" texture%s(baseImage,"93" gl_TexCoord[0].st + kernelVals[i].xy);"94" }"95" }"96""97// modulate with gl_Color in order to apply extra alpha98" gl_FragColor = sum * gl_Color;"99"}";100101/**102* Flags that can be bitwise-or'ed together to control how the shader103* source code is generated.104*/105#define CONVOLVE_RECT (1 << 0)106#define CONVOLVE_EDGE_ZERO_FILL (1 << 1)107#define CONVOLVE_5X5 (1 << 2)108109/**110* The handles to the ConvolveOp fragment program objects. The index to111* the array should be a bitwise-or'ing of the CONVOLVE_* flags defined112* above. Note that most applications will likely need to initialize one113* or two of these elements, so the array is usually sparsely populated.114*/115static GLhandleARB convolvePrograms[8];116117/**118* The maximum kernel size supported by the ConvolveOp shader.119*/120#define MAX_KERNEL_SIZE 25121122/**123* Compiles and links the ConvolveOp shader program. If successful, this124* function returns a handle to the newly created shader program; otherwise125* returns 0.126*/127static GLhandleARB128OGLBufImgOps_CreateConvolveProgram(jint flags)129{130GLhandleARB convolveProgram;131GLint loc;132char *kernelMax = IS_SET(CONVOLVE_5X5) ? "25" : "9";133char *target = IS_SET(CONVOLVE_RECT) ? "2DRect" : "2D";134char edge[100];135char finalSource[2000];136137J2dTraceLn1(J2D_TRACE_INFO,138"OGLBufImgOps_CreateConvolveProgram: flags=%d",139flags);140141if (IS_SET(CONVOLVE_EDGE_ZERO_FILL)) {142// EDGE_ZERO_FILL: fill in zero at the edges143sprintf(edge, "sum = vec4(0.0);");144} else {145// EDGE_NO_OP: use the source pixel color at the edges146sprintf(edge,147"sum = texture%s(baseImage, gl_TexCoord[0].st);",148target);149}150151// compose the final source code string from the various pieces152sprintf(finalSource, convolveShaderSource,153kernelMax, target, edge, target);154155convolveProgram = OGLContext_CreateFragmentProgram(finalSource);156if (convolveProgram == 0) {157J2dRlsTraceLn(J2D_TRACE_ERROR,158"OGLBufImgOps_CreateConvolveProgram: error creating program");159return 0;160}161162// "use" the program object temporarily so that we can set the uniforms163j2d_glUseProgramObjectARB(convolveProgram);164165// set the "uniform" texture unit binding166loc = j2d_glGetUniformLocationARB(convolveProgram, "baseImage");167j2d_glUniform1iARB(loc, 0); // texture unit 0168169// "unuse" the program object; it will be re-bound later as needed170j2d_glUseProgramObjectARB(0);171172return convolveProgram;173}174175void176OGLBufImgOps_EnableConvolveOp(OGLContext *oglc, jlong pSrcOps,177jboolean edgeZeroFill,178jint kernelWidth, jint kernelHeight,179unsigned char *kernel)180{181OGLSDOps *srcOps = (OGLSDOps *)jlong_to_ptr(pSrcOps);182jint kernelSize = kernelWidth * kernelHeight;183GLhandleARB convolveProgram;184GLfloat xoff, yoff;185GLfloat edgeX, edgeY, minX, minY, maxX, maxY;186GLfloat kernelVals[MAX_KERNEL_SIZE*3];187jint i, j, kIndex;188GLint loc;189jint flags = 0;190191J2dTraceLn2(J2D_TRACE_INFO,192"OGLBufImgOps_EnableConvolveOp: kernelW=%d kernelH=%d",193kernelWidth, kernelHeight);194195RETURN_IF_NULL(oglc);196RETURN_IF_NULL(srcOps);197RESET_PREVIOUS_OP();198199if (srcOps->textureTarget == GL_TEXTURE_RECTANGLE_ARB) {200flags |= CONVOLVE_RECT;201202// for GL_TEXTURE_RECTANGLE_ARB, texcoords are specified in the203// range [0,srcw] and [0,srch], so to achieve an x/y offset of204// exactly one pixel we simply use the value 1 here205xoff = 1.0f;206yoff = 1.0f;207} else {208// for GL_TEXTURE_2D, texcoords are specified in the range [0,1],209// so to achieve an x/y offset of approximately one pixel we have210// to normalize to that range here211xoff = 1.0f / srcOps->textureWidth;212yoff = 1.0f / srcOps->textureHeight;213}214if (edgeZeroFill) {215flags |= CONVOLVE_EDGE_ZERO_FILL;216}217if (kernelWidth == 5 && kernelHeight == 5) {218flags |= CONVOLVE_5X5;219}220221// locate/initialize the shader program for the given flags222if (convolvePrograms[flags] == 0) {223convolvePrograms[flags] = OGLBufImgOps_CreateConvolveProgram(flags);224if (convolvePrograms[flags] == 0) {225// shouldn't happen, but just in case...226return;227}228}229convolveProgram = convolvePrograms[flags];230231// enable the convolve shader232j2d_glUseProgramObjectARB(convolveProgram);233234// update the "uniform" image min/max values235edgeX = (kernelWidth/2) * xoff;236edgeY = (kernelHeight/2) * yoff;237minX = edgeX;238minY = edgeY;239if (srcOps->textureTarget == GL_TEXTURE_RECTANGLE_ARB) {240// texcoords are in the range [0,srcw] and [0,srch]241maxX = ((GLfloat)srcOps->width) - edgeX;242maxY = ((GLfloat)srcOps->height) - edgeY;243} else {244// texcoords are in the range [0,1]245maxX = (((GLfloat)srcOps->width) / srcOps->textureWidth) - edgeX;246maxY = (((GLfloat)srcOps->height) / srcOps->textureHeight) - edgeY;247}248loc = j2d_glGetUniformLocationARB(convolveProgram, "imgEdge");249j2d_glUniform4fARB(loc, minX, minY, maxX, maxY);250251// update the "uniform" kernel offsets and values252loc = j2d_glGetUniformLocationARB(convolveProgram, "kernelVals");253kIndex = 0;254for (i = -kernelHeight/2; i < kernelHeight/2+1; i++) {255for (j = -kernelWidth/2; j < kernelWidth/2+1; j++) {256kernelVals[kIndex+0] = j*xoff;257kernelVals[kIndex+1] = i*yoff;258kernelVals[kIndex+2] = NEXT_FLOAT(kernel);259kIndex += 3;260}261}262j2d_glUniform3fvARB(loc, kernelSize, kernelVals);263}264265void266OGLBufImgOps_DisableConvolveOp(OGLContext *oglc)267{268J2dTraceLn(J2D_TRACE_INFO, "OGLBufImgOps_DisableConvolveOp");269270RETURN_IF_NULL(oglc);271272// disable the ConvolveOp shader273j2d_glUseProgramObjectARB(0);274}275276/**************************** RescaleOp support *****************************/277278/**279* The RescaleOp shader is one of the simplest possible. Each fragment280* from the source image is multiplied by the user's scale factor and added281* to the user's offset value (these are component-wise operations).282* Finally, the resulting value is multiplied by the current OpenGL color,283* which contains the extra alpha value.284*285* The RescaleOp spec says that the operation is performed regardless of286* whether the source data is premultiplied or non-premultiplied. This is287* a problem for the OpenGL pipeline in that a non-premultiplied288* BufferedImage will have already been converted into premultiplied289* when uploaded to an OpenGL texture. Therefore, we have a special mode290* called RESCALE_NON_PREMULT (used only for source images that were291* originally non-premultiplied) that un-premultiplies the source color292* prior to the rescale operation, then re-premultiplies the resulting293* color before returning from the fragment shader.294*295* Note that this shader source code includes some "holes" marked by "%s".296* This allows us to build different shader programs (e.g. one for297* GL_TEXTURE_2D targets, one for GL_TEXTURE_RECTANGLE_ARB targets, and so on)298* simply by filling in these "holes" with a call to sprintf(). See the299* OGLBufImgOps_CreateRescaleProgram() method for more details.300*/301static const char *rescaleShaderSource =302// image to be rescaled303"uniform sampler%s baseImage;"304// vector containing scale factors305"uniform vec4 scaleFactors;"306// vector containing offsets307"uniform vec4 offsets;"308""309"void main(void)"310"{"311" vec4 srcColor = texture%s(baseImage, gl_TexCoord[0].st);"312// (placeholder for un-premult code)313" %s"314// rescale source value315" vec4 result = (srcColor * scaleFactors) + offsets;"316// (placeholder for re-premult code)317" %s"318// modulate with gl_Color in order to apply extra alpha319" gl_FragColor = result * gl_Color;"320"}";321322/**323* Flags that can be bitwise-or'ed together to control how the shader324* source code is generated.325*/326#define RESCALE_RECT (1 << 0)327#define RESCALE_NON_PREMULT (1 << 1)328329/**330* The handles to the RescaleOp fragment program objects. The index to331* the array should be a bitwise-or'ing of the RESCALE_* flags defined332* above. Note that most applications will likely need to initialize one333* or two of these elements, so the array is usually sparsely populated.334*/335static GLhandleARB rescalePrograms[4];336337/**338* Compiles and links the RescaleOp shader program. If successful, this339* function returns a handle to the newly created shader program; otherwise340* returns 0.341*/342static GLhandleARB343OGLBufImgOps_CreateRescaleProgram(jint flags)344{345GLhandleARB rescaleProgram;346GLint loc;347char *target = IS_SET(RESCALE_RECT) ? "2DRect" : "2D";348char *preRescale = "";349char *postRescale = "";350char finalSource[2000];351352J2dTraceLn1(J2D_TRACE_INFO,353"OGLBufImgOps_CreateRescaleProgram: flags=%d",354flags);355356if (IS_SET(RESCALE_NON_PREMULT)) {357preRescale = "srcColor.rgb /= srcColor.a;";358postRescale = "result.rgb *= result.a;";359}360361// compose the final source code string from the various pieces362sprintf(finalSource, rescaleShaderSource,363target, target, preRescale, postRescale);364365rescaleProgram = OGLContext_CreateFragmentProgram(finalSource);366if (rescaleProgram == 0) {367J2dRlsTraceLn(J2D_TRACE_ERROR,368"OGLBufImgOps_CreateRescaleProgram: error creating program");369return 0;370}371372// "use" the program object temporarily so that we can set the uniforms373j2d_glUseProgramObjectARB(rescaleProgram);374375// set the "uniform" values376loc = j2d_glGetUniformLocationARB(rescaleProgram, "baseImage");377j2d_glUniform1iARB(loc, 0); // texture unit 0378379// "unuse" the program object; it will be re-bound later as needed380j2d_glUseProgramObjectARB(0);381382return rescaleProgram;383}384385void386OGLBufImgOps_EnableRescaleOp(OGLContext *oglc, jlong pSrcOps,387jboolean nonPremult,388unsigned char *scaleFactors,389unsigned char *offsets)390{391OGLSDOps *srcOps = (OGLSDOps *)jlong_to_ptr(pSrcOps);392GLhandleARB rescaleProgram;393GLint loc;394jint flags = 0;395396J2dTraceLn(J2D_TRACE_INFO, "OGLBufImgOps_EnableRescaleOp");397398RETURN_IF_NULL(oglc);399RETURN_IF_NULL(srcOps);400RESET_PREVIOUS_OP();401402// choose the appropriate shader, depending on the source texture target403if (srcOps->textureTarget == GL_TEXTURE_RECTANGLE_ARB) {404flags |= RESCALE_RECT;405}406if (nonPremult) {407flags |= RESCALE_NON_PREMULT;408}409410// locate/initialize the shader program for the given flags411if (rescalePrograms[flags] == 0) {412rescalePrograms[flags] = OGLBufImgOps_CreateRescaleProgram(flags);413if (rescalePrograms[flags] == 0) {414// shouldn't happen, but just in case...415return;416}417}418rescaleProgram = rescalePrograms[flags];419420// enable the rescale shader421j2d_glUseProgramObjectARB(rescaleProgram);422423// update the "uniform" scale factor values (note that the Java-level424// dispatching code always passes down 4 values here, regardless of425// the original source image type)426loc = j2d_glGetUniformLocationARB(rescaleProgram, "scaleFactors");427{428GLfloat sf1 = NEXT_FLOAT(scaleFactors);429GLfloat sf2 = NEXT_FLOAT(scaleFactors);430GLfloat sf3 = NEXT_FLOAT(scaleFactors);431GLfloat sf4 = NEXT_FLOAT(scaleFactors);432j2d_glUniform4fARB(loc, sf1, sf2, sf3, sf4);433}434435// update the "uniform" offset values (note that the Java-level436// dispatching code always passes down 4 values here, and that the437// offsets will have already been normalized to the range [0,1])438loc = j2d_glGetUniformLocationARB(rescaleProgram, "offsets");439{440GLfloat off1 = NEXT_FLOAT(offsets);441GLfloat off2 = NEXT_FLOAT(offsets);442GLfloat off3 = NEXT_FLOAT(offsets);443GLfloat off4 = NEXT_FLOAT(offsets);444j2d_glUniform4fARB(loc, off1, off2, off3, off4);445}446}447448void449OGLBufImgOps_DisableRescaleOp(OGLContext *oglc)450{451J2dTraceLn(J2D_TRACE_INFO, "OGLBufImgOps_DisableRescaleOp");452453RETURN_IF_NULL(oglc);454455// disable the RescaleOp shader456j2d_glUseProgramObjectARB(0);457}458459/**************************** LookupOp support ******************************/460461/**462* The LookupOp shader takes a fragment color (from the source texture) as463* input, subtracts the optional user offset value, and then uses the464* resulting value to index into the lookup table texture to provide465* a new color result. Finally, the resulting value is multiplied by466* the current OpenGL color, which contains the extra alpha value.467*468* The lookup step requires 3 texture accesses (or 4, when alpha is included),469* which is somewhat unfortunate because it's not ideal from a performance470* standpoint, but that sort of thing is getting faster with newer hardware.471* In the 3-band case, we could consider using a three-dimensional texture472* and performing the lookup with a single texture access step. We already473* use this approach in the LCD text shader, and it works well, but for the474* purposes of this LookupOp shader, it's probably overkill. Also, there's475* a difference in that the LCD text shader only needs to populate the 3D LUT476* once, but here we would need to populate it on every invocation, which477* would likely be a waste of VRAM and CPU/GPU cycles.478*479* The LUT texture is currently hardcoded as 4 rows/bands, each containing480* 256 elements. This means that we currently only support user-provided481* tables with no more than 256 elements in each band (this is checked at482* at the Java level). If the user provides a table with less than 256483* elements per band, our shader will still work fine, but if elements are484* accessed with an index >= the size of the LUT, then the shader will simply485* produce undefined values. Typically the user would provide an offset486* value that would prevent this from happening, but it's worth pointing out487* this fact because the software LookupOp implementation would usually488* throw an ArrayIndexOutOfBoundsException in this scenario (although it is489* not something demanded by the spec).490*491* The LookupOp spec says that the operation is performed regardless of492* whether the source data is premultiplied or non-premultiplied. This is493* a problem for the OpenGL pipeline in that a non-premultiplied494* BufferedImage will have already been converted into premultiplied495* when uploaded to an OpenGL texture. Therefore, we have a special mode496* called LOOKUP_NON_PREMULT (used only for source images that were497* originally non-premultiplied) that un-premultiplies the source color498* prior to the lookup operation, then re-premultiplies the resulting499* color before returning from the fragment shader.500*501* Note that this shader source code includes some "holes" marked by "%s".502* This allows us to build different shader programs (e.g. one for503* GL_TEXTURE_2D targets, one for GL_TEXTURE_RECTANGLE_ARB targets, and so on)504* simply by filling in these "holes" with a call to sprintf(). See the505* OGLBufImgOps_CreateLookupProgram() method for more details.506*/507static const char *lookupShaderSource =508// source image (bound to texture unit 0)509"uniform sampler%s baseImage;"510// lookup table (bound to texture unit 1)511"uniform sampler2D lookupTable;"512// offset subtracted from source index prior to lookup step513"uniform vec4 offset;"514""515"void main(void)"516"{"517" vec4 srcColor = texture%s(baseImage, gl_TexCoord[0].st);"518// (placeholder for un-premult code)519" %s"520// subtract offset from original index521" vec4 srcIndex = srcColor - offset;"522// use source value as input to lookup table (note that523// "v" texcoords are hardcoded to hit texel centers of524// each row/band in texture)525" vec4 result;"526" result.r = texture2D(lookupTable, vec2(srcIndex.r, 0.125)).r;"527" result.g = texture2D(lookupTable, vec2(srcIndex.g, 0.375)).r;"528" result.b = texture2D(lookupTable, vec2(srcIndex.b, 0.625)).r;"529// (placeholder for alpha store code)530" %s"531// (placeholder for re-premult code)532" %s"533// modulate with gl_Color in order to apply extra alpha534" gl_FragColor = result * gl_Color;"535"}";536537/**538* Flags that can be bitwise-or'ed together to control how the shader539* source code is generated.540*/541#define LOOKUP_RECT (1 << 0)542#define LOOKUP_USE_SRC_ALPHA (1 << 1)543#define LOOKUP_NON_PREMULT (1 << 2)544545/**546* The handles to the LookupOp fragment program objects. The index to547* the array should be a bitwise-or'ing of the LOOKUP_* flags defined548* above. Note that most applications will likely need to initialize one549* or two of these elements, so the array is usually sparsely populated.550*/551static GLhandleARB lookupPrograms[8];552553/**554* The handle to the lookup table texture object used by the shader.555*/556static GLuint lutTextureID = 0;557558/**559* Compiles and links the LookupOp shader program. If successful, this560* function returns a handle to the newly created shader program; otherwise561* returns 0.562*/563static GLhandleARB564OGLBufImgOps_CreateLookupProgram(jint flags)565{566GLhandleARB lookupProgram;567GLint loc;568char *target = IS_SET(LOOKUP_RECT) ? "2DRect" : "2D";569char *alpha;570char *preLookup = "";571char *postLookup = "";572char finalSource[2000];573574J2dTraceLn1(J2D_TRACE_INFO,575"OGLBufImgOps_CreateLookupProgram: flags=%d",576flags);577578if (IS_SET(LOOKUP_USE_SRC_ALPHA)) {579// when numComps is 1 or 3, the alpha is not looked up in the table;580// just keep the alpha from the source fragment581alpha = "result.a = srcColor.a;";582} else {583// when numComps is 4, the alpha is looked up in the table, just584// like the other color components from the source fragment585alpha =586"result.a = texture2D(lookupTable, vec2(srcIndex.a, 0.875)).r;";587}588if (IS_SET(LOOKUP_NON_PREMULT)) {589preLookup = "srcColor.rgb /= srcColor.a;";590postLookup = "result.rgb *= result.a;";591}592593// compose the final source code string from the various pieces594sprintf(finalSource, lookupShaderSource,595target, target, preLookup, alpha, postLookup);596597lookupProgram = OGLContext_CreateFragmentProgram(finalSource);598if (lookupProgram == 0) {599J2dRlsTraceLn(J2D_TRACE_ERROR,600"OGLBufImgOps_CreateLookupProgram: error creating program");601return 0;602}603604// "use" the program object temporarily so that we can set the uniforms605j2d_glUseProgramObjectARB(lookupProgram);606607// set the "uniform" values608loc = j2d_glGetUniformLocationARB(lookupProgram, "baseImage");609j2d_glUniform1iARB(loc, 0); // texture unit 0610loc = j2d_glGetUniformLocationARB(lookupProgram, "lookupTable");611j2d_glUniform1iARB(loc, 1); // texture unit 1612613// "unuse" the program object; it will be re-bound later as needed614j2d_glUseProgramObjectARB(0);615616return lookupProgram;617}618619void620OGLBufImgOps_EnableLookupOp(OGLContext *oglc, jlong pSrcOps,621jboolean nonPremult, jboolean shortData,622jint numBands, jint bandLength, jint offset,623void *tableValues)624{625OGLSDOps *srcOps = (OGLSDOps *)jlong_to_ptr(pSrcOps);626int bytesPerElem = (shortData ? 2 : 1);627GLhandleARB lookupProgram;628GLfloat foff;629GLint loc;630void *bands[4];631int i;632jint flags = 0;633634J2dTraceLn4(J2D_TRACE_INFO,635"OGLBufImgOps_EnableLookupOp: short=%d num=%d len=%d off=%d",636shortData, numBands, bandLength, offset);637638for (i = 0; i < 4; i++) {639bands[i] = NULL;640}641RETURN_IF_NULL(oglc);642RETURN_IF_NULL(srcOps);643RESET_PREVIOUS_OP();644645// choose the appropriate shader, depending on the source texture target646// and the number of bands involved647if (srcOps->textureTarget == GL_TEXTURE_RECTANGLE_ARB) {648flags |= LOOKUP_RECT;649}650if (numBands != 4) {651flags |= LOOKUP_USE_SRC_ALPHA;652}653if (nonPremult) {654flags |= LOOKUP_NON_PREMULT;655}656657// locate/initialize the shader program for the given flags658if (lookupPrograms[flags] == 0) {659lookupPrograms[flags] = OGLBufImgOps_CreateLookupProgram(flags);660if (lookupPrograms[flags] == 0) {661// shouldn't happen, but just in case...662return;663}664}665lookupProgram = lookupPrograms[flags];666667// enable the lookup shader668j2d_glUseProgramObjectARB(lookupProgram);669670// update the "uniform" offset value671loc = j2d_glGetUniformLocationARB(lookupProgram, "offset");672foff = offset / 255.0f;673j2d_glUniform4fARB(loc, foff, foff, foff, foff);674675// bind the lookup table to texture unit 1 and enable texturing676j2d_glActiveTextureARB(GL_TEXTURE1_ARB);677if (lutTextureID == 0) {678/*679* Create the lookup table texture with 4 rows (one band per row)680* and 256 columns (one LUT band element per column) and with an681* internal format of 16-bit luminance values, which will be682* sufficient for either byte or short LUT data. Note that the683* texture wrap mode will be set to the default of GL_CLAMP_TO_EDGE,684* which means that out-of-range index value will be clamped685* appropriately.686*/687lutTextureID =688OGLContext_CreateBlitTexture(GL_LUMINANCE16, GL_LUMINANCE,689256, 4);690if (lutTextureID == 0) {691// should never happen, but just to be safe...692return;693}694}695j2d_glBindTexture(GL_TEXTURE_2D, lutTextureID);696j2d_glEnable(GL_TEXTURE_2D);697698// update the lookup table with the user-provided values699if (numBands == 1) {700// replicate the single band for R/G/B; alpha band is unused701for (i = 0; i < 3; i++) {702bands[i] = tableValues;703}704bands[3] = NULL;705} else if (numBands == 3) {706// user supplied band for each of R/G/B; alpha band is unused707for (i = 0; i < 3; i++) {708bands[i] = PtrAddBytes(tableValues, i*bandLength*bytesPerElem);709}710bands[3] = NULL;711} else if (numBands == 4) {712// user supplied band for each of R/G/B/A713for (i = 0; i < 4; i++) {714bands[i] = PtrAddBytes(tableValues, i*bandLength*bytesPerElem);715}716}717718// upload the bands one row at a time into our lookup table texture719for (i = 0; i < 4; i++) {720if (bands[i] == NULL) {721continue;722}723j2d_glTexSubImage2D(GL_TEXTURE_2D, 0,7240, i, bandLength, 1,725GL_LUMINANCE,726shortData ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE,727bands[i]);728}729730// restore texture unit 0 (the default) as the active one since731// the OGLBlitTextureToSurface() method is responsible for binding the732// source image texture, which will happen later733j2d_glActiveTextureARB(GL_TEXTURE0_ARB);734}735736void737OGLBufImgOps_DisableLookupOp(OGLContext *oglc)738{739J2dTraceLn(J2D_TRACE_INFO, "OGLBufImgOps_DisableLookupOp");740741RETURN_IF_NULL(oglc);742743// disable the LookupOp shader744j2d_glUseProgramObjectARB(0);745746// disable the lookup table on texture unit 1747j2d_glActiveTextureARB(GL_TEXTURE1_ARB);748j2d_glDisable(GL_TEXTURE_2D);749j2d_glActiveTextureARB(GL_TEXTURE0_ARB);750}751752#endif /* !HEADLESS */753754755