static long laba_to_rgba (char *src, char *dst, long n) { while (n--) { double L = ((double *) src)[0]; double a = ((double *) src)[1]; double b = ((double *) src)[2]; double alpha = ((double *) src)[3]; double X, Y, Z, R, G, B; //convert Lab to XYZ LAB_to_XYZ (L, a, b, &X, &Y, &Z); //convert XYZ to RGB XYZ_to_RGB (X, Y, Z, &R, &G, &B); ((double *) dst)[0] = R; ((double *) dst)[1] = G; ((double *) dst)[2] = B; ((double *) dst)[3] = alpha; src += sizeof (double) * 4; dst += sizeof (double) * 4; } return n; }
static long lchaba_to_rgba (char *src, char *dst, long n) { while (n--) { double L = ((double *) src)[0]; double C = ((double *) src)[1]; double H = ((double *) src)[2]; double alpha = ((double *) src)[3]; double a, b, X, Y, Z, R, G, B; //Convert LCH(ab) to Lab CHab_to_ab (C, H, &a, &b); //Convert Lab to XYZ LAB_to_XYZ (L, a, b, &X, &Y, &Z); //Convert XYZ to RGB XYZ_to_RGB (X, Y, Z, &R, &G, &B); ((double *) dst)[0] = R; ((double *) dst)[1] = G; ((double *) dst)[2] = B; ((double *) dst)[3] = alpha; src += sizeof (double) * 4; dst += sizeof (double) * 4; } return n; }
void ImageRawData::SaveImage ( const string aFilename, bool need_XYZ_to_RGB , bool need_tonemap, bool need_gamma) const { FreeImageInterface output; output.AllocateMemory ( width , height ); for(size_t w = 0; w<width; w++ ) for (size_t h = 0; h < height; h++) { Spectrum pixel = rawData[ w ][ h ]; if (need_XYZ_to_RGB) pixel = XYZ_to_RGB ( pixel ); if (need_tonemap) pixel = tonemapACES ( pixel ) * 255; output.WritePixelValue ( w , h , pixel[0] , pixel[ 1 ], pixel[ 2 ] ); } if(need_gamma) output.ApplyGammaCorrection ( 2.2 ); output.SaveImage ( aFilename ); }
bool ShadingSystemImpl::set_colorspace (ustring colorspace) { for (int i = 0; colorSystems[i].name; ++i) { if (colorspace == colorSystems[i].name) { m_Red.setValue (colorSystems[i].xRed, colorSystems[i].yRed, 0.0f); m_Green.setValue (colorSystems[i].xGreen, colorSystems[i].yGreen, 0.0f); m_Blue.setValue (colorSystems[i].xBlue, colorSystems[i].yBlue, 0.0f); m_White.setValue (colorSystems[i].xWhite, colorSystems[i].yWhite, 0.0f); // set z values to normalize m_Red[2] = 1.0f - (m_Red[0] + m_Red[1]); m_Green[2] = 1.0f - (m_Green[0] + m_Green[1]); m_Blue[2] = 1.0f - (m_Blue[0] + m_Blue[1]); m_White[2] = 1.0f - (m_White[0] + m_White[1]); const Color3 &R(m_Red), &G(m_Green), &B(m_Blue), &W(m_White); // xyz -> rgb matrix, before scaling to white. Color3 r (G[1]*B[2] - B[1]*G[2], B[0]*G[2] - G[0]*B[2], G[0]*B[1] - B[0]*G[1]); Color3 g (B[1]*R[2] - R[1]*B[2], R[0]*B[2] - B[0]*R[2], B[0]*R[1] - R[0]*B[1]); Color3 b (R[1]*G[2] - G[1]*R[2], G[0]*R[2] - R[0]*G[2], R[0]*G[1] - G[0]*R[1]); Color3 w (r.dot(W), g.dot(W), b.dot(W)); // White scaling factor if (W[1] != 0.0f) // divide by W[1] to scale luminance to 1.0 w *= 1.0f/W[1]; // xyz -> rgb matrix, correctly scaled to white. r /= w[0]; g /= w[1]; b /= w[2]; m_XYZ2RGB = Matrix33 (r[0], g[0], b[0], r[1], g[1], b[1], r[2], g[2], b[2]); m_RGB2XYZ = m_XYZ2RGB.inverse(); m_luminance_scale = Color3 (m_RGB2XYZ[0][1], m_RGB2XYZ[1][1], m_RGB2XYZ[2][1]); // Precompute a table of blackbody values m_blackbody_table.clear (); float lastT = 0; for (int i = 0; lastT <= BB_MAX_TABLE_RANGE; ++i) { float T = powf (float(i), BB_TABLE_XPOWER) * BB_TABLE_SPACING + BB_DRAPER; lastT = T; bb_spectrum spec (T); Color3 rgb = XYZ_to_RGB (spectrum_to_XYZ (spec)); clamp_zero (rgb); rgb = colpow (rgb, 1.0f/BB_TABLE_YPOWER); m_blackbody_table.push_back (rgb); // std::cout << "Table[" << i << "; T=" << T << "] = " << rgb << "\n"; } // std::cout << "Made " << m_blackbody_table.size() << " table entries for blackbody\n"; #if 0 // Sanity checks std::cout << "m_XYZ2RGB = " << m_XYZ2RGB << "\n"; std::cout << "m_RGB2XYZ = " << m_RGB2XYZ << "\n"; std::cout << "m_luminance_scale = " << m_luminance_scale << "\n"; #endif return true; } } return false; }
Color3 ShadingSystemImpl::to_rgb (ustring fromspace, float a, float b, float c) { if (fromspace == Strings::RGB || fromspace == Strings::rgb) return Color3 (a, b, c); if (fromspace == Strings::hsv) return hsv_to_rgb (a, b, c); if (fromspace == Strings::hsl) return hsl_to_rgb (a, b, c); if (fromspace == Strings::YIQ) return YIQ_to_rgb (a, b, c); if (fromspace == Strings::XYZ) return XYZ_to_RGB (a, b, c); if (fromspace == Strings::xyY) return XYZ_to_RGB (xyY_to_XYZ (Color3(a,b,c))); error ("Unknown color space \"%s\"", fromspace.c_str()); return Color3 (a, b, c); }
Color3 ShadingSystemImpl::blackbody_rgb (float T) { if (T < BB_DRAPER) return Color3(1.0e-6f,0.0f,0.0f); // very very dim red if (T < BB_MAX_TABLE_RANGE) { float t = powf ((T - BB_DRAPER) / BB_TABLE_SPACING, 1.0f/BB_TABLE_XPOWER); int ti = (int)t; t -= ti; Color3 rgb = lerp (m_blackbody_table[ti], m_blackbody_table[ti+1], t); return colpow(rgb, BB_TABLE_YPOWER); } // Otherwise, compute for real bb_spectrum spec (T); Color3 rgb = XYZ_to_RGB (spectrum_to_XYZ (spec)); clamp_zero (rgb); return rgb; }
void __fastcall TPaletteForm::DrawPantone27Polar(TCanvas* cnv, int w, int h, int size) { TOutColor out_color("pantone_cube_27.txt"); float a_step = 13.3; float a_delta = 1; float dx,dy; float r0 = 10; float r1 = w > h ? h / 2 - 20: w / 2 - 20; int pw,ph; this->GetPaperSize(pw, ph); r0 = r0*(float)w/(float)pw; TPoint p; p.X = w / 2; p.Y= h / 2; TPoint pp[5]; float alfa = a_step; a_delta = 0; int entr[27];// ={428, 80, 286, 528, 452, 480, 81, 581,271, 48, 129, 368, 295, 677, 537, 294, 528, 452, 582, 572, 96, 135, 746, 404, 114, 282}; //int entr[27] ={0, 66, 398, 0, 398, 66, 580, 103,0, 76, 133, 473, 339, 108, 501, 63, 269, 28, 103, 431, 473, 391, 414, 256, 709, 58}; ConvertXYZ_1ToPantone(NULL, entr); for (int i = 0; i < 27; i++) { dx = 50*cos(3.14*alfa/180); dy = 50*sin(3.14*alfa/180); pp[0].X = p.X + r0*cos(3.14*(alfa + a_delta)/180) + dx; pp[0].Y = p.Y + r0*sin(3.14*(alfa + a_delta)/180) + dy; pp[1].X = p.X + r1*cos(3.14*(alfa + a_delta)/180) + dx; pp[1].Y = p.Y + r1*sin(3.14*(alfa + a_delta)/180) + dy; alfa += a_step; pp[3].X = p.X + r0*cos(3.14*(alfa - a_delta)/180) + dx; pp[3].Y = p.Y + r0*sin(3.14*(alfa - a_delta)/180) + dy; pp[2].X = p.X + r1*cos(3.14*(alfa - a_delta)/180) + dx; pp[2].Y = p.Y + r1*sin(3.14*(alfa - a_delta)/180) + dy; pp[4]= pp[0]; double r,g,b; XYZ_to_RGB(XYZCube27_1[i].x, XYZCube27_1[i].y, XYZCube27_1[i].z, r,g,b); TPantoneColorEntry* pce = MainForm->m_database.Get(entr[i]); cnv->Brush->Color = (TColor)RGB(pce->Red, pce->Green, pce->Blue); //cnv->Brush->Color = (TColor)RGB(255*r, 255*g, 255*b); cnv->Pen->Color = cnv->Brush->Color; //out_color.PrintColor(i +1 , pce->Red, pce->Green, pce->Blue); cnv->Polygon(pp, 4); cnv->FloodFill(pp[0].X, pp[0].Y, cnv->Brush->Color, fsSurface); } TRect r; r.init(p.X + r0 / 2 , p.Y - 10, p.X + r0 /2 + 20, p.Y + 10); cnv->Brush->Color = (TColor)RGB(255,0,0); cnv->Pen->Color = cnv->Brush->Color; cnv->Rectangle(r); r.init(p.X - r0 / 2 , p.Y - 10, p.X - r0 /2 - 20, p.Y + 10); cnv->Brush->Color = (TColor)RGB(0,0,255); cnv->Pen->Color = cnv->Brush->Color; cnv->Rectangle(r); r.init(p.X - r0/3 , p.Y - r0, p.X + r0/3, p.Y - r0 /3 ); cnv->Brush->Color = (TColor)RGB(255,255,255); cnv->Pen->Color = cnv->Brush->Color; cnv->Rectangle(r); r.init(p.X - r0/3 , p.Y + r0, p.X + r0/3, p.Y + r0 /3 ); cnv->Brush->Color = (TColor)RGB(255,255,255); cnv->Pen->Color = cnv->Brush->Color; cnv->Rectangle(r); }