ICCProfile JP2Instance::ReadICCProfile() { CheckOpenStream( !m_path.IsEmpty() && s_jasperInitialized, "ReadICCProfile" ); if ( jas_image_cmprof( m_jp2Image ) == nullptr ) return ICCProfile(); jas_stream_t* iccStream = nullptr; try { // Create a growable memory stream for output iccStream = jas_stream_memopen( 0, 0 ); if ( iccStream == nullptr ) JP2KERROR( "Extracting ICC profile from JPEG2000 image: Unable to create JasPer stream" ); if ( jas_iccprof_save( jas_image_cmprof( m_jp2Image )->iccprof, iccStream ) < 0 ) JP2KERROR( "Extracting ICC profile from JPEG2000 image: Error saving profile to JasPer stream" ); long iccSize = jas_stream_tell( iccStream ); if ( iccSize <= 0 ) JP2KERROR( "Extracting ICC profile from JPEG2000 image: Invalid JasPer stream position" ); ByteArray iccData( iccSize ); jas_stream_rewind( iccStream ); if ( jas_stream_read( iccStream, iccData.Begin(), iccSize ) != iccSize ) JP2KERROR( "Extracting ICC profile from JPEG2000 image: Error reading JasPer stream" ); jas_stream_close( iccStream ), iccStream = nullptr; ICCProfile icc( iccData ); if ( icc.IsProfile() ) Console().WriteLn( "<end><cbr>ICC profile extracted: \'" + icc.Description() + "\', " + String( icc.ProfileSize() ) + " bytes." ); return icc; } catch ( ... ) { if ( iccStream != nullptr ) jas_stream_close( iccStream ); Close(); throw; } }
int AssignICCProfileProcess::ProcessCommandLine( const StringList& argv ) const { ArgumentList arguments = ExtractArguments( argv, ArgumentItemMode::AsViews, ArgumentOption::AllowWildcards|ArgumentOption::NoPreviews ); AssignICCProfileInstance instance( this ); bool launchInterface = false; int count = 0; for ( ArgumentList::const_iterator i = arguments.Begin(); i != arguments.End(); ++i ) { const Argument& arg = *i; if ( arg.IsNumeric() ) throw Error( "Unknown numeric argument: " + arg.Token() ); else if ( arg.IsString() ) { if ( arg.Id() == "profile" ) { instance.targetProfile = arg.StringValue(); instance.targetProfile.Trim(); if ( instance.targetProfile.IsEmpty() ) throw Error( "Empty profile identifier: " + arg.Token() ); } else if ( arg.Id() == "filename" ) { String filename = arg.StringValue(); filename.Trim(); if ( filename.IsEmpty() ) throw Error( "Empty file name: " + arg.Token() ); instance.targetProfile.Clear(); StringList dirs = ICCProfile::ProfileDirectories(); for ( StringList::const_iterator i = dirs.Begin(); i != dirs.End(); ++i ) { String path = *i + '/' + filename; if ( File::Exists( path ) ) { ICCProfile icc( path ); if ( icc.IsProfile() ) { instance.targetProfile = icc.Description(); break; } } } if ( instance.targetProfile.IsEmpty() ) throw Error( "The specified file name does not correspond to a valid ICC profile: " + filename ); } else throw Error( "Unknown string argument: " + arg.Token() ); } else if ( arg.IsSwitch() ) { if ( arg.Id() == "default" ) instance.mode = arg.SwitchState() ? AssignMode::AssignDefaultProfile : AssignMode::AssignNewProfile; else if ( arg.Id() == "untag" ) instance.mode = arg.SwitchState() ? AssignMode::LeaveUntagged : AssignMode::AssignNewProfile; else throw Error( "Unknown switch argument: " + arg.Token() ); } else if ( arg.IsLiteral() ) { if ( arg.Id() == "default" ) instance.mode = AssignMode::AssignDefaultProfile; else if ( arg.Id() == "untag" ) instance.mode = AssignMode::LeaveUntagged; else if ( arg.Id() == "-interface" ) launchInterface = true; else if ( arg.Id() == "-help" ) { ShowHelp(); return 0; } else throw Error( "Unknown argument: " + arg.Token() ); } else if ( arg.IsItemList() ) { ++count; if ( arg.Items().IsEmpty() ) { Console().WriteLn( "No view(s) found: " + arg.Token() ); continue; } for ( StringList::const_iterator j = arg.Items().Begin(); j != arg.Items().End(); ++j ) { View v = View::ViewById( *j ); if ( v.IsNull() ) throw Error( "No such view: " + *j ); instance.LaunchOn( v ); } } } if ( launchInterface ) instance.LaunchInterface(); else if ( count == 0 ) { if ( ImageWindow::ActiveWindow().IsNull() ) throw Error( "There is no active image window." ); instance.LaunchOnCurrentWindow(); } return 0; }
// // * - type ... calgray, calrgb, cielab, icc, [palette] // * - white = x, z // * - black = x, y, z // * - range = amin, amax, bmin, bmax (cielab) // * - gamma = val [,val, val] (calgray [,calrgb]) // * - matrix = 9 vals (calrgb) // * - profile = file-path (icc based) // * - name = sRGB .. some predefined name IColorSpaceMan::cs_handle_pair_t ColorSpaceManImpl::color_space_load(Char const* spec_str) { ParseArgs pargs(&g_cs_kwds, &g_cs_names); ParsedResult pres(parse_options(spec_str, pargs)); unsigned name = pres.explicit_value(); if (name == ParsedResult::NO_EXPL_VALUE) throw exception_invalid_value(msg_invalid_cs_spec()) << JAGLOC; cs_str_spec_t spec(name, pres); cs_handle_pair_t result; switch (name) { case CSN_SRGB: result.first = register_icc_from_memory(binres::icc_srgb, binres::icc_srgb_size, 3); break; case CSN_ADOBE_RGB: result.first = register_icc_from_memory(binres::icc_adobe_rgb, binres::icc_adobe_rgb_size, 3); break; case CSN_BYID: if (!spec.id) throw exception_invalid_value(msg_invalid_cs_spec()) << JAGLOC; result.first = ColorSpaceHandle( handle_from_id<RESOURCE_COLOR_SPACE>(spec.id)); break; case CSN_DEVICE_RGB: result.first = ColorSpaceHandle(CS_DEVICE_RGB); break; case CSN_DEVICE_GRAY: result.first = ColorSpaceHandle(CS_DEVICE_GRAY); break; case CSN_DEVICE_CMYK: result.first = ColorSpaceHandle(CS_DEVICE_CMYK); break; case CSN_CALGRAY: { intrusive_ptr<ICIECalGray> gray(define_calgray()); set_cie_base(spec, *gray); if (!spec.gamma.empty()) gray->gamma(spec.gamma[0]); result.first = color_space_load(gray); break; } case CSN_CALRGB: { intrusive_ptr<ICIECalRGB> rgb(define_calrgb()); set_cie_base(spec, *rgb); if (!spec.gamma.empty()) { JAG_ASSERT(spec.gamma.size()==3); rgb->gamma(spec.gamma[0], spec.gamma[1], spec.gamma[2]); } if (!spec.matrix.empty()) { JAG_ASSERT(spec.matrix.size()==9); rgb->matrix(spec.matrix[0], spec.matrix[1], spec.matrix[2], spec.matrix[3], spec.matrix[4], spec.matrix[5], spec.matrix[6], spec.matrix[7], spec.matrix[8]); } result.first = color_space_load(rgb); break; } case CSN_CIELAB: { intrusive_ptr<ICIELab> lab(define_cielab()); set_cie_base(spec, *lab); if (!spec.range.empty()) { JAG_ASSERT(4 == spec.range.size()); lab->range(spec.range[0], spec.range[1], spec.range[2], spec.range[3]); } result.first = color_space_load(lab); break; } case CSN_ICC: { if (-1==spec.components || spec.icc_profile.empty()) throw exception_invalid_value(msg_invalid_cs_spec()) << JAGLOC; intrusive_ptr<IICCBased> icc(define_iccbased()); icc->num_components(spec.components); icc->icc_profile(spec.icc_profile.c_str()); result.first = color_space_load(icc); break; } default: ; } // switch JAG_ASSERT(is_valid(result.first)); // is it palette? if (!spec.palette.empty()) { intrusive_ptr<IPalette> indexed(define_indexed()); indexed->set( id_from_handle<ColorSpace>(result.first), &spec.palette[0], static_cast<UInt>(spec.palette.size())); result.second = result.first; result.first = color_space_load(indexed); } return result; }
int ICCProfileTransformationProcess::ProcessCommandLine( const StringList& argv ) const { ArgumentList arguments = ExtractArguments( argv, ArgumentItemMode::AsViews, ArgumentOption::AllowWildcards|ArgumentOption::NoPreviews ); ICCProfileTransformationInstance instance( this ); bool launchInterface = false; int count = 0; for ( ArgumentList::const_iterator i = arguments.Begin(); i != arguments.End(); ++i ) { const Argument& arg = *i; if ( arg.IsNumeric() ) { throw Error( "Unknown numeric argument: " + arg.Token() ); } else if ( arg.IsString() ) { if ( arg.Id() == "profile" ) { instance.targetProfile = arg.StringValue(); instance.targetProfile.Trim(); if ( instance.targetProfile.IsEmpty() ) throw Error( "Empty profile identifier: " + arg.Token() ); } else if ( arg.Id() == "filename" ) { String filename = arg.StringValue(); filename.Trim(); if ( filename.IsEmpty() ) throw Error( "Empty file name: " + arg.Token() ); instance.targetProfile.Clear(); StringList dirs = ICCProfile::ProfileDirectories(); for ( StringList::const_iterator i = dirs.Begin(); i != dirs.End(); ++i ) { String path = *i + '/' + filename; if ( File::Exists( path ) ) { ICCProfile icc( path ); if ( icc.IsProfile() ) { instance.targetProfile = icc.Description(); break; } } } if ( instance.targetProfile.IsEmpty() ) throw Error( "The specified file name does not correspond to a valid ICC profile: " + filename ); } else if ( arg.Id() == "rendering-intent" ) { if ( arg.StringValue() == "perceptual" ) instance.renderingIntent = ICCTRenderingIntent::Perceptual; else if ( arg.StringValue() == "saturation" ) instance.renderingIntent = ICCTRenderingIntent::Saturation; else if ( arg.StringValue() == "relative" || arg.StringValue() == "relative-colorimetric" ) instance.renderingIntent = ICCTRenderingIntent::RelativeColorimetric; else if ( arg.StringValue() == "absolute" || arg.StringValue() == "absolute-colorimetric" ) instance.renderingIntent = ICCTRenderingIntent::AbsoluteColorimetric; else throw Error( "Invalid rendering intent: " + arg.Token() ); } else throw Error( "Unknown string argument: " + arg.Token() ); } else if ( arg.IsSwitch() ) { if ( arg.Id() == "to-default-profile" ) instance.toDefaultProfile = arg.SwitchState(); else if ( arg.Id() == "black-point-compensation" ) instance.useBlackPointCompensation = arg.SwitchState(); else if ( arg.Id() == "floating-point-transform" ) instance.useFloatingPointTransformation = arg.SwitchState(); else throw Error( "Unknown switch argument: " + arg.Token() ); } else if ( arg.IsLiteral() ) { if ( arg.Id() == "to-default-profile" ) instance.toDefaultProfile = true; else if ( arg.Id() == "black-point-compensation" ) instance.useBlackPointCompensation = true; else if ( arg.Id() == "floating-point-transform" ) instance.useFloatingPointTransformation = true; else if ( arg.Id() == "-interface" ) launchInterface = true; else if ( arg.Id() == "-help" ) { ShowHelp(); return 0; } else throw Error( "Unknown argument: " + arg.Token() ); } else if ( arg.IsItemList() ) { ++count; if ( arg.Items().IsEmpty() ) { Console().WriteLn( "No view(s) found: " + arg.Token() ); continue; } for ( StringList::const_iterator j = arg.Items().Begin(); j != arg.Items().End(); ++j ) { View v = View::ViewById( *j ); if ( v.IsNull() ) throw Error( "No such view: " + *j ); instance.LaunchOn( v ); } } } if ( launchInterface ) instance.LaunchInterface(); else if ( count == 0 ) { if ( ImageWindow::ActiveWindow().IsNull() ) throw Error( "There is no active image window." ); instance.LaunchOnCurrentWindow(); } return 0; }
void WorldGenerator::generateChunk(Chunk *chunk) { vec3i64 cc = chunk->getCC(); const ElevationChunk elevation = elevationGenerator.getChunk(vec2i64(cc[0], cc[1])); bool underground = cc[2] * Chunk::WIDTH <= std::ceil(elevation.max); if (underground) { tunnelSwitchPerlin.noise3( cc.cast<double>() * Chunk::WIDTH / wp.tunnelSwitchScale / wp.overall_scale, vec3d(1 / wp.tunnelSwitchScale / wp.overall_scale), vec3ui(Chunk::WIDTH, Chunk::WIDTH, Chunk::WIDTH + 9), wp.tunnelSwitchOctaves, wp.tunnelSwitchAmplGain, wp.tunnelSwitchFreqGain, tunnelSwitchBuffer ); cavenessPerlin.noise3( cc.cast<double>() * Chunk::WIDTH / wp.cavenessScale / wp.overall_scale, vec3d(1 / wp.cavenessScale / wp.overall_scale), vec3ui(Chunk::WIDTH, Chunk::WIDTH, Chunk::WIDTH + 9), wp.cavenessOctaves, wp.cavenessAmplGain, wp.cavenessFreqGain, cavenessBuffer ); } for (uint iccx = 0; iccx < Chunk::WIDTH; iccx++) for (uint iccy = 0; iccy < Chunk::WIDTH; iccy++) { int64 bcx = cc[0] * Chunk::WIDTH + iccx; int64 bcy = cc[1] * Chunk::WIDTH + iccy; double h = elevation.heights[iccy * Chunk::WIDTH + iccx]; double base_vegetation = 0; double base_temperature = 0; bool solid = false; int realDepth = 0; for (int iccz = Chunk::WIDTH + 8; iccz >= 0; iccz--) { int64 bcz = iccz + cc[2] * Chunk::WIDTH; vec3d dbc = vec3i64(bcx, bcy, bcz).cast<double>(); const double depth = h - bcz; if (depth < 0) { solid = false; } else if (depth > (h * wp.surfaceRelDepth)) { solid = true; } else { const double xScale = wp.surfaceThresholdXScale; double funPos = 1.0 - depth / (h * wp.surfaceRelDepth) * 2; double threshold = funPos * (xScale * xScale + funPos * funPos) / (xScale * xScale + 1) * 2.0; double v = surfacePerlin.noise3( dbc / wp.surfaceScale, wp.surfaceOctaves, wp.surfaceAmplGain, wp.surfaceFreqGain ); if (v > threshold) solid = true; else solid = false; } if (solid) realDepth++; else realDepth = 0; uint8 block; if (iccz < (int) Chunk::WIDTH) { if (solid) { if (base_temperature > wp.desert_threshold && realDepth < 5) block = 4; // sand else if (base_vegetation > wp.grasland_threshold && realDepth == 1) block = 2; // gras else if (realDepth >= 5) block = 3; // dirt else block = 1; // stone } else { if (realDepth <= 0 && bcz <= 0) block = 62; // water else block = 0; // air } // TODO cave rooms // TODO height dependent // caves if (block != 0) { if (!underground) LOG_ERROR(logger) << "Trying to generate caves, but not underground"; uint index = ((iccz * Chunk::WIDTH) + iccy) * Chunk::WIDTH + iccx; double depthValue1 = wp.cavenessDepthGainFac1 * (1 - 1 / (depth / wp.cavenessDepthGain1 + 1)); double depthValue2 = wp.cavenessDepthGainFac2 * (1 - 1 / (depth / wp.cavenessDepthGain2 + 1)); double caveness = (cavenessBuffer[index] + 0.5) * (depthValue1 + depthValue2); double tunnelSwitch = tunnelSwitchBuffer[index]; double overLap = wp.tunnelSwitchOverlap; double tunnelValue1 = 0; double tunnelValue2 = 0; vec3d tunnelCoords(dbc / wp.tunnelScale); if (tunnelSwitch > -overLap) { const double v1 = std::abs(tunnelPerlin1a.noise3( tunnelCoords, wp.tunnelOctaves, wp.tunnelAmplGain, wp.tunnelFreqGain)); const double v2 = std::abs(tunnelPerlin2a.noise3( tunnelCoords, wp.tunnelOctaves, wp.tunnelAmplGain, wp.tunnelFreqGain)); const double v3 = std::abs(tunnelPerlin3a.noise3( tunnelCoords, wp.tunnelOctaves, wp.tunnelAmplGain, wp.tunnelFreqGain)); double v12 = 1 / ((v1 * v1 + 1) * (v2 * v2 + 1) - 1); double v23 = 1 / ((v2 * v2 + 1) * (v3 * v3 + 1) - 1); double ramp = 1; if (tunnelSwitch < 0) ramp = (overLap + tunnelSwitch) / overLap; tunnelValue1 = (v12 + v23) * ramp; } if (block != 0 && tunnelSwitch < overLap) { const double v1 = std::abs(tunnelPerlin1b.noise3( tunnelCoords, wp.tunnelOctaves, wp.tunnelAmplGain, wp.tunnelFreqGain)); const double v2 = std::abs(tunnelPerlin2b.noise3( tunnelCoords, wp.tunnelOctaves, wp.tunnelAmplGain, wp.tunnelFreqGain)); const double v3 = std::abs(tunnelPerlin3b.noise3( tunnelCoords, wp.tunnelOctaves, wp.tunnelAmplGain, wp.tunnelFreqGain)); double v12 = 1 / ((v1 * v1 + 1) * (v2 * v2 + 1) - 1); double v23 = 1 / ((v2 * v2 + 1) * (v3 * v3 + 1) - 1); double ramp = 1; if (tunnelSwitch > 0) ramp = (overLap - tunnelSwitch) / overLap; tunnelValue2 = (v12 + v23) * ramp; } if (caveness * caveness * (wp.caveRoomValue + tunnelValue1 + tunnelValue2) > wp.caveThreshold) { block = 0; } } vec3ui8 icc(iccx, iccy, iccz); chunk->initBlock(icc, block); } } } chunk->finishInitialization(); }