RGB DepthOfField::sampleCircle(int x, int y, double z, double radius, double tolerance, Image* image) const { int int_rad = int(radius); double rad2 = radius*radius; int x_min = MAX(x - int_rad,0); int x_max = MIN(x + int_rad,image->getWidth() - 1); int y_min = MAX(y - int_rad,0); int y_max = MIN(y + int_rad,image->getHeight() - 1); int samples = 0; RGB result = RGB(0.0,0.0,0.0); for (int b = y_min; b < y_max; b++) { for (int a = x_min; a < x_max; a++) { if ((x-a)*(x-a) + (y-b)*(y-b) <= rad2) { RGB depth = depth_buffer->getRGBA(a,b); if (fabs(depth.r() - z) < tolerance) { result += image->getRGBA(a,b); samples++; } } } } if (samples > 0) { return result / double(samples); } return result; }
svg_writer::style svg_writer::get_color_style( const RGB& color) const { #define rgb_value(value) (255.0 * value) string text = msprintf("rgb(%s, %s, %s)", rgb_value(color.get_red()), rgb_value(color.get_green()), rgb_value(color.get_blue())); return {"stroke", text}; #undef rgb_value }
void GraphicsEdge::setColor(const RGB &rgb) { _overriddenColor.setRed(rgb.getRed()); _overriddenColor.setGreen(rgb.getGreen()); _overriddenColor.setBlue(rgb.getBlue()); _overrideColor = true; setZValue(-1); scene()->update(boundingRect()); }
std::string toHtml(const RGB &rgb) { char buf[32]; // Microsoft doesn't define round(double) in <cmath> unsigned r = boost::numeric::converter<unsigned, double>::convert(clip(rgb.r())*255); unsigned g = boost::numeric::converter<unsigned, double>::convert(clip(rgb.g())*255); unsigned b = boost::numeric::converter<unsigned, double>::convert(clip(rgb.b())*255); sprintf(buf, "#%02x%02x%02x", r, g, b); return buf; }
void SelfOrganizingMaps::displayUsingNeuronColor(){ vector<double> weigths; for(int row=0; row<_size; row++){ for(int col=0; col<_size; col++){ RGB *neuronColor = _matrix->getNeuron(row, col)->getNeuronColor(); glColor3f(neuronColor->getRed(), neuronColor->getGreen(), neuronColor->getBlue()); glBegin(GL_QUADS); glVertex3f(row, col, 0); // upper left glVertex3f(row, col-1, 0); // lower left glVertex3f(row+1, col-1, 0); // lower right glVertex3f(row+1, col, 0); // upper right glEnd(); } } }
bool Image::get(int a_nWidth, int a_nHeight, RGB &a_rgbColor) { if(a_nWidth < 0 || a_nWidth > m_nWidth) return false; if(a_nHeight < 0 || a_nHeight > m_nHeight) return false; a_rgbColor.set(m_rgbImage[a_nWidth][a_nHeight]); return true; }
// Transform pixel to grey color // @input: // - RGB - unnull color // @output: void TargetPixel::ToGrey(const RGB &t_color) { bool pixelIsGrey = t_color.IsGreyColor(); if ( true == pixelIsGrey ) { SetRGB(t_color); } else { int red = t_color.GetRed(); int green = t_color.GetGreen(); int blue = t_color.GetBlue(); RGB greyColor(red, green, blue); greyColor.ToGrey(); SetRGB(greyColor); } }
/* virtual */ std::string svg_writer::get_label_formatted( const rna_label& label, const RGB& color) const { properties out; out << get_point_formatted(label.p, "", "") << property("class", color.get_name()); return create_element("text", out, label.label); }
// TODO: handle wrong input bool SteganoHide::hideNumberInMagickColorRGB(const unsigned short &smallNumber, Magick::ColorRGB &color) { RGB pixelRGB(convert16BitTo8BitRGB(color.redQuantum()), convert16BitTo8BitRGB(color.greenQuantum()), convert16BitTo8BitRGB(color.blueQuantum())); RGB roundedRGB; roundedRGB.red = pixelRGB.red - (pixelRGB.red % 10); roundedRGB.green = pixelRGB.green - (pixelRGB.green % 10); roundedRGB.blue = pixelRGB.blue - (pixelRGB.blue % 10); unsigned char mostSignificantBit = std::floor(smallNumber / 100); unsigned char middleSigificantBit = std::floor(smallNumber % 100 / 10); unsigned char leastSignificantBit = smallNumber % 10; /* if(mostSignificantBit > 5 || middleSigificantBit > 5 || leastSignificantBit > 5) { throw hideNumber2Big; }*/ // there was an overflow: eg. roundedRGB.blue = 250 and leastSignificantLetterBit = 8 => 258 => 2 // This way to check overflow is possible, because *SignificantLetterBit >= 0 if(roundedRGB.green + middleSigificantBit > 255) { return false; } if(roundedRGB.blue + leastSignificantBit > 255) { return false; } // std::cout << (int)roundedRGB.blue << "+" << (int)leastSignificantBit << "="; roundedRGB.red += mostSignificantBit; roundedRGB.green += middleSigificantBit; roundedRGB.blue += leastSignificantBit; //std::cout << (int)roundedRGB.blue << std::endl; color = Magick::ColorRGB(roundedRGB.toString()); return true; }
/* virtual */ std::string svg_writer::get_line_formatted( point from, point to, const RGB& color) const { properties out; out << get_point_formatted(from, "", "1") << get_point_formatted(to, "", "2") << property("class", color.get_name()); return create_element("line", out); }
void Image::gammaCorrect(float dGamma) { RGB rgbTemp; float fPower = 1.0 / dGamma; for(int x = 0; x < m_nWidth; x++) { for(int y = 0; y < m_nHeight; y++) { rgbTemp.set(m_rgbImage[x][y]); rgbTemp.clamp(0.0, 1.0); m_rgbImage[x][y].R = pow(rgbTemp.r(), fPower); m_rgbImage[x][y].G = pow(rgbTemp.g(), fPower); m_rgbImage[x][y].B = pow(rgbTemp.b(), fPower); } } }
const bool operator<(const RGB b) const { return this->value() < b.value(); }
void debugMostSimilarSamples(MFCC* templat, MFCC* palette, Waveform paletteAudio) { assert(palette->numMfccs > 0); int32_t* closestMatches = new int32_t[templat->numWindows]; for(int64_t windowIndex = 0; windowIndex < templat->numWindows; ++ windowIndex) { double bestSim = similarityIndex(palette, 0, templat, windowIndex); int32_t bestPaletteIndex = 0; for(int32_t i = 1; i < palette->numWindows; ++ i) { double thisSim = similarityIndex(palette, i, templat, windowIndex); if(thisSim < bestSim) { bestSim = thisSim; bestPaletteIndex = i; } } closestMatches[windowIndex] = bestPaletteIndex; } { assert(palette->params.frameStepMilliseconds < palette->params.frameLengthMilliseconds); Waveform mixture; int32_t windowLength = (palette->params.frameLengthMilliseconds * paletteAudio.mSampleRate) / 1000; int32_t windowStep = (palette->params.frameStepMilliseconds * paletteAudio.mSampleRate) / 1000; mixture.mNumSamples = templat->numWindows * windowStep + windowLength; mixture.mFloatSamples = new double[mixture.mNumSamples]; for(int64_t i = 0; i < mixture.mNumSamples; ++ i) { mixture.mFloatSamples[i] = 0; } mixture.mSampleRate = paletteAudio.mSampleRate; for(int64_t windowIndex = 0; windowIndex < templat->numWindows; ++ windowIndex) { for(int64_t windowSample = 0; windowSample < windowLength; ++ windowSample) { // Apply hanning window // Hooray for compiler optimizations double tau = 6.28318530717958647692528677; double numerator = tau * windowSample; double denominator = windowLength - 1; double hanning = 0.5 * (1.0 - std::cos(numerator / denominator)); mixture.mFloatSamples[windowIndex * windowStep + windowSample] += hanning * paletteAudio.mFloatSamples[closestMatches[windowIndex] * windowStep + windowSample]; } } writeWaveform("closest_matches.ogg", mixture); } { int width = templat->numWindows; int height = templat->numMfccs; if(width > 2048) width = 2048; char imageData[width * height * 3]; for(int pixelY = 0; pixelY < height; ++ pixelY) { for(int pixelX = 0; pixelX < width; ++ pixelX) { int frame = pixelX; int spectrum = (height - pixelY) - 1; { double power = normalized(palette->data[closestMatches[frame]][spectrum], -4, 18); RGB heat = colorrampSevenHeat(power); imageData[(pixelY * width + pixelX) * 3 ] = heat.RU8(); imageData[(pixelY * width + pixelX) * 3 + 1] = heat.GU8(); imageData[(pixelY * width + pixelX) * 3 + 2] = heat.BU8(); } } } stbi_write_png("closest_matches.png", width, height, 3, imageData, width * 3); } }
int main() { freopen("in.txt","r",stdin); char to[10],now[10]; RGB rgb; HSV hsv; HSL hsl; while(~scanf("%s",to)) { scanf("%s",now); if(!strcmp(to,"RGB")) { if(!strcmp(now,"HSV")) { hsv.read(); hsv.toRGB().print(); } if(!strcmp(now,"HSL")) { hsl.read(); hsl.toRGB().print(); } if(!strcmp(now,"RGB")) { rgb.read(); rgb.print(); } } if(!strcmp(to,"HSL")) { if(!strcmp(now,"HSV")) { hsv.read(); hsv.toRGB().toHSL().print(); } if(!strcmp(now,"HSL")) { hsl.read(); hsl.print(); } if(!strcmp(now,"RGB")) { rgb.read(); rgb.toHSL().print(); } } if(!strcmp(to,"HSV")) { if(!strcmp(now,"HSV")) { hsv.read(); hsv.print(); } if(!strcmp(now,"HSL")) { hsl.read(); hsl.toRGB().toHSV().print(); } if(!strcmp(now,"RGB")) { rgb.read(); rgb.toHSV().print(); } } } return 0; }
bool Shade(Path* path, Geometry *geometry, BVHAccel *bvh, const RayHit& rayHit) { uint tracedShadowRayCount; if (rayHit.index == 0xffffffffu) { return false; } // Something was hit unsigned int currentTriangleIndex = rayHit.index; RGB triInterpCol = geometry->triangles[currentTriangleIndex].InterpolateColor( geometry->vertColors, rayHit.b1, rayHit.b2); Normal shadeN = geometry->triangles[currentTriangleIndex].InterpolateNormal( geometry->vertNormals, rayHit.b1, rayHit.b2); // Calculate next step path->depth++; // Check if I have to stop if (path->depth >= MAX_PATH_DEPTH) { // Too depth, terminate the path return false; } else if (path->depth > 2) { // Russian Rulette, maximize cos const float p = min(1.f, triInterpCol.filter() * AbsDot(shadeN, path->pathRay.d)); if (p > getFloatRNG(&path->seed)) path->throughput /= p; else { // Terminate the path return false; } } //-------------------------------------------------------------------------- // Build the shadow ray //-------------------------------------------------------------------------- // Check if it is a light source float RdotShadeN = Dot(path->pathRay.d, shadeN); if (geometry->IsLight(currentTriangleIndex)) { // Check if we are on the right side of the light source if ((path->depth == 1) && (RdotShadeN < 0.f)) path->radiance += triInterpCol * path->throughput; // Terminate the path return false; } if (RdotShadeN > 0.f) { // Flip shade normal shadeN = -shadeN; } else RdotShadeN = -RdotShadeN; path->throughput *= RdotShadeN * triInterpCol; // Trace shadow rays const Point hitPoint = path->pathRay(rayHit.t); tracedShadowRayCount = 0; const float lightStrategyPdf = static_cast<float> (SHADOWRAY) / static_cast<float> (geometry->nLights); float lightPdf[SHADOWRAY]; RGB lightColor[SHADOWRAY]; Ray shadowRay[SHADOWRAY]; for (unsigned int i = 0; i < SHADOWRAY; ++i) { // Select the light to sample const unsigned int currentLightIndex = geometry->SampleLights(getFloatRNG(&path->seed)); // const TriangleLight &light = scene->lights[currentLightIndex]; // Select a point on the surface lightColor[tracedShadowRayCount] = Sample_L(currentLightIndex, geometry, hitPoint, shadeN, getFloatRNG(&path->seed), getFloatRNG(&path->seed), &lightPdf[tracedShadowRayCount], &shadowRay[tracedShadowRayCount]); // Scale light pdf for ONE_UNIFORM strategy lightPdf[tracedShadowRayCount] *= lightStrategyPdf; // Using 0.1 instead of 0.0 to cut down fireflies if (lightPdf[tracedShadowRayCount] > 0.1f) tracedShadowRayCount++; } RayHit* rh = new RayHit[tracedShadowRayCount]; for (unsigned int i = 0; i < tracedShadowRayCount; ++i) Intersect(shadowRay[i], rh[i], bvh->bvhTree, geometry->triangles, geometry->vertices); if ((tracedShadowRayCount > 0)) { for (unsigned int i = 0; i < tracedShadowRayCount; ++i) { const RayHit *shadowRayHit = &rh[i]; if (shadowRayHit->index == 0xffffffffu) { // Nothing was hit, light is visible path->radiance += path->throughput * lightColor[i] / lightPdf[i]; } } } //-------------------------------------------------------------------------- // Build the next vertex path ray //-------------------------------------------------------------------------- // Calculate exit direction float r1 = 2.f * M_PI * getFloatRNG(&path->seed); float r2 = getFloatRNG(&path->seed); float r2s = sqrt(r2); const Vector w(shadeN); Vector u; if (fabsf(shadeN.x) > .1f) { const Vector a(0.f, 1.f, 0.f); u = Cross(a, w); } else { const Vector a(1.f, 0.f, 0.f); u = Cross(a, w); } u = Normalize(u); Vector v = Cross(w, u); Vector newDir = u * (cosf(r1) * r2s) + v * (sinf(r1) * r2s) + w * sqrtf(1.f - r2); newDir = Normalize(newDir); path->pathRay.o = hitPoint; path->pathRay.d = newDir; return true; }