void Renderer::SaveState(std::string name) { Document doc; doc.Parse("{}"); Value state(kObjectType); state.AddMember("Volume", Value().SetString("volname", doc.GetAllocator()), doc.GetAllocator()); getCamera().saveState(doc, state); getRenderProperties().saveState(doc, state); getTransferFunction().saveState(doc, state); getColorMap().saveState(doc, state["TransferFunction"]); getSlices().saveState(doc, state); getIsos().saveState(doc, state); doc.AddMember("State", state, doc.GetAllocator()); StringBuffer sbuf; PrettyWriter<StringBuffer> writer(sbuf); doc.Accept(writer); std::ofstream out; out.open(name.c_str(), std::ofstream::out); out << sbuf.GetString() << "\n"; out.close(); }
bool GifTranscoder::renderImage(GifFileType* gifIn, GifByteType* rasterBits, int imageIndex, int transparentColorIndex, ColorARGB* renderBuffer, ColorARGB bgColor, GifImageDesc prevImageDimens, int prevImageDisposalMode) { ASSERT(imageIndex < gifIn->ImageCount, "Image index %d is out of bounds (count=%d)", imageIndex, gifIn->ImageCount); ColorMapObject* colorMap = getColorMap(gifIn); if (colorMap == NULL) { LOGE("No GIF color map found"); return false; } // Clear all or part of the background, before drawing the first image and maybe before drawing // subsequent images (depending on the DisposalMode). if (imageIndex == 0) { fillRect(renderBuffer, gifIn->SWidth, gifIn->SHeight, 0, 0, gifIn->SWidth, gifIn->SHeight, bgColor); } else if (prevImageDisposalMode == DISPOSE_BACKGROUND) { fillRect(renderBuffer, gifIn->SWidth, gifIn->SHeight, prevImageDimens.Left, prevImageDimens.Top, prevImageDimens.Width, prevImageDimens.Height, TRANSPARENT); } // Paint this image onto the canvas for (int y = 0; y < gifIn->Image.Height; y++) { for (int x = 0; x < gifIn->Image.Width; x++) { GifByteType colorIndex = *getPixel(rasterBits, gifIn->Image.Width, x, y); // This image may be smaller than the GIF's "logical screen" int renderX = x + gifIn->Image.Left; int renderY = y + gifIn->Image.Top; // Skip drawing transparent pixels if this image renders on top of the last one if (imageIndex > 0 && prevImageDisposalMode == DISPOSE_DO_NOT && colorIndex == transparentColorIndex) { continue; } ColorARGB* renderPixel = getPixel(renderBuffer, gifIn->SWidth, renderX, renderY); *renderPixel = getColorARGB(colorMap, transparentColorIndex, colorIndex); } } return true; }
GifByteType GifTranscoder::computeNewColorIndex(GifFileType* gifIn, int transparentColorIndex, ColorARGB* renderBuffer, int x, int y) { ColorMapObject* colorMap = getColorMap(gifIn); // Compute the average color of 4 adjacent pixels from the input image. ColorARGB c1 = *getPixel(renderBuffer, gifIn->SWidth, x * 2, y * 2); ColorARGB c2 = *getPixel(renderBuffer, gifIn->SWidth, x * 2 + 1, y * 2); ColorARGB c3 = *getPixel(renderBuffer, gifIn->SWidth, x * 2, y * 2 + 1); ColorARGB c4 = *getPixel(renderBuffer, gifIn->SWidth, x * 2 + 1, y * 2 + 1); ColorARGB avgColor = computeAverage(c1, c2, c3, c4); // Search the color map for the best match. return findBestColor(colorMap, transparentColorIndex, avgColor); }
//----------------------------------------------------------------------------- CGColorRef getCGColor (const CColor& color) { CGColorMap& colorMap = getColorMap (); CGColorMap::const_iterator it = colorMap.find (color); if (it != colorMap.end ()) { CGColorRef result = it->second; return result; } const CGFloat components[] = { static_cast<CGFloat> (color.red / 255.), static_cast<CGFloat> (color.green / 255.), static_cast<CGFloat> (color.blue / 255.), static_cast<CGFloat> (color.alpha / 255.) }; CGColorRef result = CGColorCreate (GetCGColorSpace (), components); colorMap.insert (std::make_pair (color, result)); return result; }
void Renderer::LoadState(std::string statefile, bool with_data) { Document doc; std::ifstream in; in.open(statefile.c_str(), std::istream::in); in.seekg(0, std::ios::end); std::streamsize size = in.tellg(); in.seekg(0, std::ios::beg); in.read(xyzzy, size); doc.Parse(xyzzy); in.close(); if (! doc.IsObject() || ! doc.HasMember("State")) { std::cerr << "invalud state file\n"; return; } if (doc["State"].HasMember("Camera")) { getCamera().loadState(doc["State"]["Camera"]); getCamera().commit(); } if (doc["State"].HasMember("Lights")) { getLights().loadState(doc["State"]["Lights"]); getLights().commit(getRenderer()); } if (doc["State"].HasMember("TransferFunction")) { getTransferFunction().loadState(doc["State"]["TransferFunction"]); if (doc["State"]["TransferFunction"].HasMember("Colormap")) { getColorMap().loadState(doc["State"]["TransferFunction"]["Colormap"]); getColorMap().commit(getTransferFunction()); } getTransferFunction().commit(getRenderer()); } if (doc["State"].HasMember("Slices")) { getSlices().loadState(doc["State"]["Slices"]); getSlices().commit(getRenderer(), &volume); } if (doc["State"].HasMember("Isosurfaces")) { getIsos().loadState(doc["State"]["Isosurfaces"]); getIsos().commit(&volume); } if (with_data) LoadDataFromFile(doc["State"]["Volume"].GetString()); in.close(); }
/*---------------------------------------------------------------------------*/ void start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) { bmp_source_ptr source = (bmp_source_ptr) sinfo; INT32 headerSize; INT32 biWidth = 0; INT32 biHeight = 0; unsigned int biPlanes; INT32 biCompression; INT32 biXPelsPerMeter,biYPelsPerMeter; INT32 biClrUsed = 0; int mapentrysize = 0; /* 0 indicates no colormap */ JDIMENSION row_width; PBITMAPINFO2 pbmi; /* is infoheader2 + pointer to rgb table */ PRGB2 pRGB2; pbmi = (PBITMAPINFO2)source->pSaveImg->pbmp2; biWidth = pbmi->cx; biHeight = pbmi->cy; biPlanes = pbmi->cPlanes; biXPelsPerMeter = 0; biYPelsPerMeter = 0; biClrUsed = 1 << ( pbmi->cBitCount * pbmi->cPlanes ); source->bits_per_pixel = pbmi->cBitCount; /* Read the colormap, if any */ if (biClrUsed > 0 && biClrUsed <= 256) { if (biClrUsed <= 0) biClrUsed = 256; /* assume it's 256 */ pRGB2 = &pbmi->argbColor[0]; /* Color definition record */ /* ** Allocate space to store the colormap */ source->colormap = (*cinfo->mem->alloc_sarray) ((j_common_ptr)cinfo, JPOOL_IMAGE, (JDIMENSION)biClrUsed, /* # of palette entries */ (JDIMENSION)4); /* # bytes per color */ /* ** and read it from memory. */ getColorMap(source,(int)biClrUsed,pRGB2); } /* ** Compute row width in file, including padding to 4-byte boundary */ if (source->bits_per_pixel == 24) row_width = (JDIMENSION) (biWidth * 3); else row_width = (JDIMENSION) biWidth; while ((row_width & 3) != 0) row_width++; source->row_width = row_width; /* ** Allocate space for inversion array, prepare for preload pass */ source->whole_image = (*cinfo->mem->request_virt_sarray) ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, row_width, (JDIMENSION) biHeight, (JDIMENSION) 1); source->pub.get_pixel_rows = preload_image; /* ** Allocate one-row buffer for returned data */ source->pub.buffer = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo, JPOOL_IMAGE, (JDIMENSION) (biWidth * 3), (JDIMENSION) 1); source->pub.buffer_height = 1; cinfo->in_color_space = JCS_RGB; cinfo->input_components = 3; cinfo->data_precision = 8; cinfo->image_width = (JDIMENSION) biWidth; cinfo->image_height = (JDIMENSION) biHeight; }