/* scale here is 1/rate */ static int cdf_dgamma (lua_State *L) { /* stack should contain x, shape and opt. scale */ lua_Number x = luaL_checknumber(L, 1); lua_Number shape = luaL_checknumber(L, 2); lua_Number scale = luaL_optnumber(L, 3, 1); lua_Number d; check_gamma(L, 1, x, shape, scale); d = x * scale; d = exp(shape*log(d)-d-dlngam(&shape))/x; lua_pushnumber(L, d); return 1; }
static int cdf_pgamma (lua_State *L) { /* stack should contain x, shape and opt. scale */ lua_Number x = luaL_checknumber(L, 1); lua_Number shape = luaL_checknumber(L, 2); lua_Number scale = luaL_optnumber(L, 3, 1); lua_Number p, q, bound; int which = 1; int status; check_gamma(L, 1, x, shape, scale); cdfgam(&which, &p, &q, &x, &shape, &scale, &status, &bound); check_status(status, bound); lua_pushnumber(L, p); return 1; }
static int cdf_qgamma (lua_State *L) { /* stack should contain p, shape and opt. scale */ lua_Number p = luaL_checknumber(L, 1); lua_Number shape = luaL_checknumber(L, 2); lua_Number scale = luaL_optnumber(L, 3, 1); lua_Number x; check_gamma(L, 2, p, shape, scale); if (p==0 || p==1) x = (p==0) ? 0 : HUGE_VAL; else { lua_Number q = 1-p; lua_Number bound; int which = 2; int status; cdfgam(&which, &p, &q, &x, &shape, &scale, &status, &bound); check_status(status, bound); } lua_pushnumber(L, x); return 1; }
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ApplyGamma, reporter, ctxInfo) { GrContext* context = ctxInfo.grContext(); static const int kW = 256; static const int kH = 256; static const size_t kRowBytes = sizeof(uint32_t) * kW; GrSurfaceDesc baseDesc; baseDesc.fConfig = kRGBA_8888_GrPixelConfig; baseDesc.fWidth = kW; baseDesc.fHeight = kH; const SkImageInfo ii = SkImageInfo::MakeN32Premul(kW, kH); SkAutoTMalloc<uint32_t> srcPixels(kW * kH); for (int y = 0; y < kH; ++y) { for (int x = 0; x < kW; ++x) { srcPixels.get()[y*kW+x] = SkPreMultiplyARGB(x, y, x, 0xFF); } } SkBitmap bm; bm.installPixels(ii, srcPixels.get(), kRowBytes); SkAutoTMalloc<uint32_t> read(kW * kH); // We allow more error on GPUs with lower precision shader variables. float error = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.2f : 0.5f; for (auto toSRGB : { false, true }) { sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii)); if (!dst) { ERRORF(reporter, "Could not create surfaces for copy surface test."); continue; } SkCanvas* dstCanvas = dst->getCanvas(); dstCanvas->clear(SK_ColorRED); dstCanvas->flush(); SkPaint gammaPaint; gammaPaint.setBlendMode(SkBlendMode::kSrc); gammaPaint.setColorFilter(toSRGB ? SkColorFilter::MakeLinearToSRGBGamma() : SkColorFilter::MakeSRGBToLinearGamma()); dstCanvas->drawBitmap(bm, 0, 0, &gammaPaint); dstCanvas->flush(); sk_memset32(read.get(), 0, kW * kH); if (!dstCanvas->readPixels(ii, read.get(), kRowBytes, 0, 0)) { ERRORF(reporter, "Error calling readPixels"); continue; } bool abort = false; // Validate that pixels were copied/transformed correctly. for (int y = 0; y < kH && !abort; ++y) { for (int x = 0; x < kW && !abort; ++x) { uint32_t r = read.get()[y * kW + x]; uint32_t s = srcPixels.get()[y * kW + x]; uint32_t expected; if (!check_gamma(s, r, toSRGB, error, &expected)) { ERRORF(reporter, "Expected dst %d,%d to contain 0x%08x " "from src 0x%08x and mode %s. Got %08x", x, y, expected, s, toSRGB ? "ToSRGB" : "ToLinear", r); abort = true; break; } } } } }