//--------------------------------------------------------- bool CExercise_10::On_Execute(void) { bool bAlive; int x, y, i; CSG_Colors Colors; //----------------------------------------------------- // General initialisations... m_pLife = Parameters("RESULT")->asGrid(); m_nColors = Parameters("COLORS")->asInt(); Colors.Set_Count(m_nColors + 1); Colors.Set_Ramp(SG_GET_RGB(127, 127, 127), SG_GET_RGB(0, 0, 0)); Colors.Set_Color(0, SG_GET_RGB(255, 255, 255)); DataObject_Set_Colors(m_pLife, Colors); //----------------------------------------------------- // Initialise life's world... if( Parameters("REFRESH")->asBool() ) { srand((unsigned)time(NULL)); for(y=0; y<Get_NY(); y++) { for(x=0; x<Get_NX(); x++) { m_pLife->Set_Value(x, y, rand() > RAND_MAX / 2 ? 0 : 1); } } } //----------------------------------------------------- // Execution... m_pTemp = SG_Create_Grid(m_pLife, SG_DATATYPE_Byte); for(i=1, bAlive=true; bAlive && Process_Get_Okay(true); i++) { Process_Set_Text(CSG_String::Format(SG_T("%d %s"), i, _TL("Life Cycle"))); if( (bAlive = Next_Step()) == false ) { Message_Add(CSG_String::Format(SG_T("%s %d %s\n"), _TL("Dead after"), i, _TL("Life Cycles"))); } } delete(m_pTemp); //----------------------------------------------------- // Finish... return( true ); }
//--------------------------------------------------------- void CVIEW_ScatterPlot::_Draw_Image(wxDC &dc, wxRect r) { CSG_Colors *pColors = m_Options("DENSITY_PAL")->asColors(); wxImage Image(r.GetWidth(), r.GetHeight()); double dCount = (pColors->Get_Count() - 2.0) / log(1.0 + m_Count.Get_Max()); double dx = (m_Count.Get_NX() - 1.0) / (double)r.GetWidth (); double dy = (m_Count.Get_NY() - 1.0) / (double)r.GetHeight(); //----------------------------------------------------- #pragma omp parallel for for(int y=Image.GetHeight()-1; y>=0; y--) { double Count; double ix = 0.0; double iy = m_Count.Get_NY() - 1 - y * dy; for(int x=0; x<Image.GetWidth(); x++, ix+=dx) { int i = m_Count.Get_Value(ix, iy, Count) && Count > 0.0 ? (int)(log(1.0 + Count) * dCount) : 0; Image.SetRGB(x, y, pColors->Get_Red(i), pColors->Get_Green(i), pColors->Get_Blue(i)); } } dc.DrawBitmap(wxBitmap(Image), r.GetTopLeft()); }
//--------------------------------------------------------- bool CMine_Sweeper::MakeBoard(int level) { int i, x, y; CSG_Colors Colors; switch( level ) { case 0: Mine_NX = 8; Mine_NY = 8; N_Mines=10; break; case 1: Mine_NX = 16; Mine_NY = 16; N_Mines=40; break; case 2: Mine_NX = 30; Mine_NY = 16; N_Mines=99; break; } pInput = SG_Create_Grid(SG_DATATYPE_Int,SPRITE_SIZE*Mine_NX, SPRITE_SIZE*Mine_NY); pInput->Set_Name(_TL("Mine Sweeper")); Parameters("GRID")->Set_Value(pInput); //----------------------------------------------------- CSG_Parameter *pLUT = DataObject_Get_Parameter(pInput, "LUT"); if( pLUT && pLUT->asTable() ) { pLUT->asTable()->Del_Records(); for(i=0; i<16; i++) { CSG_Table_Record *pRecord = pLUT->asTable()->Add_Record(); pRecord->Set_Value(0, SG_GET_RGB(mine_res_color[i*3], mine_res_color[i*3+1], mine_res_color[i*3+2])); pRecord->Set_Value(3, i); } DataObject_Set_Parameter(pInput, pLUT); DataObject_Set_Parameter(pInput, "COLORS_TYPE", 1); // Color Classification Type: Lookup Table } Colors.Set_Count(16); for ( i=0;i<16; i++) { Colors.Set_Color(i, SG_GET_RGB(mine_res_color[i*3], mine_res_color[i*3+1], mine_res_color[i*3+2])); } DataObject_Set_Colors(pInput, Colors); DataObject_Update(pInput, 0.0, 15.0, true); //----------------------------------------------------- for( y = 0; y < Mine_NY; y++) for( x = 0; x < Mine_NX; x++) { SetSprite(x,y,SPRITE_CLOSE); } pInput->Set_Value(0, 0); return true; }
//--------------------------------------------------------- void CGrid_Colors_Fit::_Set_Colors(CSG_Colors &Colors, double pos_a, long color_a, double pos_b, long color_b) { int a, b; a = (int)(pos_a * Colors.Get_Count()); if( a < 0 ) a = 0; else if( a >= Colors.Get_Count() ) a = Colors.Get_Count() - 1; b = (int)(pos_b * Colors.Get_Count()); if( b < 0 ) b = 0; else if( b >= Colors.Get_Count() ) b = Colors.Get_Count() - 1; Colors.Set_Ramp(color_a, color_b, a, b); }
//--------------------------------------------------------- void CINFO_Messages::Add_Line(void) { CSG_Colors c; SetDefaultStyle(wxTextAttr(wxNullColour, wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW))); _Add_Text(wxT("\n\n")); c.Set_Ramp( Get_Color_asInt(wxSystemSettings::GetColour(wxSYS_COLOUR_ACTIVECAPTION)), Get_Color_asInt(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)) ); c.Set_Count(100); for(int i=0; i<c.Get_Count(); i++) { SetDefaultStyle(wxTextAttr( Get_Color_asWX(c.Get_Color(c.Get_Count() - 1 - i)), Get_Color_asWX(c.Get_Color(i)) )); _Add_Text(wxT(" ")); } SetDefaultStyle(wxTextAttr(wxNullColour, wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW))); }
//--------------------------------------------------------- void CVisibility_BASE::Initialize(CSG_Grid *pVisibility, int iMethod) { CSG_Colors Colors; switch( iMethod ) { case 0: // Visibility pVisibility->Assign(0.0); Colors.Set_Count(2); Colors.Set_Ramp(SG_GET_RGB(0, 0, 0), SG_GET_RGB(255, 255, 255)); break; case 1: // Shade pVisibility->Assign(M_PI_090); pVisibility->Set_Unit(_TL("radians")); Colors.Set_Ramp(SG_GET_RGB(255, 255, 255), SG_GET_RGB(0, 0, 0)); break; case 2: // Distance pVisibility->Assign_NoData(); Colors.Set_Ramp(SG_GET_RGB(255, 255, 191), SG_GET_RGB(0, 95, 0)); break; case 3: // Size pVisibility->Assign_NoData(); pVisibility->Set_Unit(_TL("radians")); Colors.Set_Ramp(SG_GET_RGB(0, 95, 0), SG_GET_RGB(255, 255, 191)); break; } SG_UI_DataObject_Colors_Set(pVisibility, &Colors); return; }
//--------------------------------------------------------- bool CGrid_Import::On_Execute(void) { bool bTransform; int x, y, yy, Method; double ax, ay, dx, dy, rx, ry, xMin, yMin, Cellsize; CSG_Colors Colors; CSG_String fImage, fWorld, Name; CSG_Grid *pImage; CSG_File Stream; wxImage Image; wxImageHistogram Histogram; //----------------------------------------------------- fImage = Parameters("FILE") ->asString(); Method = Parameters("METHOD") ->asInt(); Name = SG_File_Get_Name(fImage, false); //----------------------------------------------------- wxImageHandler *pImgHandler = NULL; if( !SG_UI_Get_Window_Main() ) { CSG_String fName = SG_File_Get_Name(fImage, true); if( SG_File_Cmp_Extension(fName, SG_T("jpg")) ) pImgHandler = new wxJPEGHandler; else if( SG_File_Cmp_Extension(fName, SG_T("pcx")) ) pImgHandler = new wxPCXHandler; else if( SG_File_Cmp_Extension(fName, SG_T("tif")) ) pImgHandler = new wxTIFFHandler; else if( SG_File_Cmp_Extension(fName, SG_T("gif")) ) pImgHandler = new wxGIFHandler; else if( SG_File_Cmp_Extension(fName, SG_T("pnm")) ) pImgHandler = new wxPNMHandler; else if( SG_File_Cmp_Extension(fName, SG_T("xpm")) ) pImgHandler = new wxXPMHandler; #ifdef _SAGA_MSW else if( SG_File_Cmp_Extension(fName, SG_T("bmp")) ) pImgHandler = new wxBMPHandler; #endif else // if( SG_File_Cmp_Extension(fName, SG_T("png")) ) pImgHandler = new wxPNGHandler; wxImage::AddHandler(pImgHandler); } if( !Image.LoadFile(fImage.c_str()) ) { return( false ); } //----------------------------------------------------- if( SG_File_Cmp_Extension(fImage, SG_T("bmp")) ) { fWorld = SG_File_Make_Path(NULL, fImage, SG_T("bpw")); } else if( SG_File_Cmp_Extension(fImage, SG_T("jpg")) ) { fWorld = SG_File_Make_Path(NULL, fImage, SG_T("jgw")); } else if( SG_File_Cmp_Extension(fImage, SG_T("png")) ) { fWorld = SG_File_Make_Path(NULL, fImage, SG_T("pgw")); } else if( SG_File_Cmp_Extension(fImage, SG_T("tif")) ) { fWorld = SG_File_Make_Path(NULL, fImage, SG_T("tfw")); } else { fWorld = SG_File_Make_Path(NULL, fImage, SG_T("world")); } bTransform = false; xMin = 0.0; yMin = 0.0; Cellsize = 1.0; if( Stream.Open(fWorld, SG_FILE_R, false) && fscanf(Stream.Get_Stream(), "%lf %lf %lf %lf %lf %lf ", &dx, &ry, &rx, &dy, &ax, &ay) == 6 ) { if( dx != -dy || rx != 0.0 || ry != 0.0 ) { bTransform = true; } else { xMin = ax; yMin = ay + dy * (Image.GetHeight() - 1); Cellsize = dx; } } //----------------------------------------------------- // color look-up table... if( Method == 0 && (yy = Image.ComputeHistogram(Histogram)) <= 256 ) { Colors.Set_Count(yy); for(wxImageHistogram::iterator i=Histogram.begin(); i!=Histogram.end(); ++i) { Colors.Set_Color(i->second.index, SG_GET_R(i->first), SG_GET_G(i->first), SG_GET_B(i->first)); } pImage = SG_Create_Grid(yy <= 2 ? SG_DATATYPE_Bit : SG_DATATYPE_Byte, Image.GetWidth(), Image.GetHeight(), Cellsize, xMin, yMin); for(y=0; y<pImage->Get_NY() && Set_Progress(y, pImage->Get_NY()); y++) { yy = bTransform ? y : pImage->Get_NY() - 1 - y; for(x=0; x<pImage->Get_NX(); x++) { pImage->Set_Value(x, y, Histogram[SG_GET_RGB(Image.GetRed(x, yy), Image.GetGreen(x, yy), Image.GetBlue(x, yy))].index); } } if( bTransform ) { Set_Transformation(&pImage, ax, ay, dx, dy, rx, ry); } pImage->Set_Name(Name); pImage->Get_Projection().Load(SG_File_Make_Path(NULL, fImage, SG_T("prj"))); Parameters("OUT_GRID")->Set_Value(pImage); DataObject_Set_Colors(pImage, Colors); DataObject_Update(pImage, 0, Colors.Get_Count() - 1); } //----------------------------------------------------- else // true color... { pImage = SG_Create_Grid(SG_DATATYPE_Int, Image.GetWidth(), Image.GetHeight(), Cellsize, xMin, yMin); pImage ->Set_Name(Name); for(y=0; y<pImage->Get_NY() && Set_Progress(y, pImage->Get_NY()); y++) { yy = bTransform ? y : pImage->Get_NY() - 1 - y; for(x=0; x<pImage->Get_NX(); x++) { pImage->Set_Value(x, y, SG_GET_RGB(Image.GetRed(x, yy), Image.GetGreen(x, yy), Image.GetBlue(x, yy))); } } if( bTransform ) { Set_Transformation(&pImage, ax, ay, dx, dy, rx, ry); } //------------------------------------------------- if( Method != 1 ) // true color... { pImage->Get_Projection().Load(fImage, SG_PROJ_FMT_WKT); pImage->Set_Name(Name); pImage->Get_Projection().Load(SG_File_Make_Path(NULL, fImage, SG_T("prj"))); Parameters("OUT_GRID")->Set_Value(pImage); DataObject_Set_Colors(pImage, 100, SG_COLORS_BLACK_WHITE); DataObject_Set_Parameter(pImage, "COLORS_TYPE", 6); // Color Classification Type: RGB } //------------------------------------------------- else // split channels... { CSG_Grid *pR, *pG, *pB; pR = SG_Create_Grid(pImage->Get_System(), SG_DATATYPE_Byte); pG = SG_Create_Grid(pImage->Get_System(), SG_DATATYPE_Byte); pB = SG_Create_Grid(pImage->Get_System(), SG_DATATYPE_Byte); for(y=0; y<pImage->Get_NY() && Set_Progress(y, pImage->Get_NY()); y++) { for(x=0; x<pImage->Get_NX(); x++) { pR->Set_Value(x, y, SG_GET_R(pImage->asInt(x, y))); pG->Set_Value(x, y, SG_GET_G(pImage->asInt(x, y))); pB->Set_Value(x, y, SG_GET_B(pImage->asInt(x, y))); } } pR->Get_Projection().Load(fImage, SG_PROJ_FMT_WKT); pG->Get_Projection().Load(fImage, SG_PROJ_FMT_WKT); pB->Get_Projection().Load(fImage, SG_PROJ_FMT_WKT); pR->Set_Name(CSG_String::Format(SG_T("%s [R]"), Name.c_str())); pG->Set_Name(CSG_String::Format(SG_T("%s [G]"), Name.c_str())); pB->Set_Name(CSG_String::Format(SG_T("%s [B]"), Name.c_str())); pR->Get_Projection().Load(SG_File_Make_Path(NULL, fImage, SG_T("prj"))); pG->Get_Projection().Load(SG_File_Make_Path(NULL, fImage, SG_T("prj"))); pB->Get_Projection().Load(SG_File_Make_Path(NULL, fImage, SG_T("prj"))); Parameters("OUT_RED") ->Set_Value(pR); Parameters("OUT_GREEN") ->Set_Value(pG); Parameters("OUT_BLUE") ->Set_Value(pB); DataObject_Set_Colors(pR, 100, SG_COLORS_BLACK_RED); DataObject_Set_Colors(pG, 100, SG_COLORS_BLACK_GREEN); DataObject_Set_Colors(pB, 100, SG_COLORS_BLACK_BLUE); } } //----------------------------------------------------- if( !SG_UI_Get_Window_Main() && pImgHandler != NULL) { wxImage::RemoveHandler(pImgHandler->GetName()); } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CPoints_View_Extent::_Draw_Image(void) { if( m_pPoints->Get_Count() <= 0 || m_pPoints->Get_Extent().Get_XRange() <= 0.0 || m_pPoints->Get_Extent().Get_YRange() <= 0.0 || m_cField < 0 || m_cField >= m_pPoints->Get_Field_Count() ) { return( false ); } //------------------------------------------------- wxSize Size; double dx, dy; Size = GetClientSize(); if (Size.x <= 0 || Size.y <= 0) // temporary hack for wxGTK to suppress 'assert "isOK()" failed in Get_Height(): invalid image' return( false ); // when dlg.ShowModal() is called; during construction everything is fine, but the first call of // the On_Size() event returns a client size of 0 m_Image .Create(Size.x, Size.y); m_Image_Value .Create(Size.x, Size.y); m_Image_Count .Create(Size.x, Size.y); //------------------------------------------------- m_Extent = m_pPoints->Get_Extent(); if( (dx = Size.y / (double)Size.x) < (m_Extent.Get_YRange() / m_Extent.Get_XRange()) ) { dx = 0.5 * (m_Extent.Get_XRange() - m_Extent.Get_YRange() / dx); m_Extent.m_rect.xMin += dx; m_Extent.m_rect.xMax -= dx; } else { dy = 0.5 * (m_Extent.Get_YRange() - m_Extent.Get_XRange() * dx); m_Extent.m_rect.yMin += dy; m_Extent.m_rect.yMax -= dy; } dx = Size.x / m_Extent.Get_XRange(); dy = Size.y / m_Extent.Get_YRange(); //------------------------------------------------- bool bColorAsRGB = m_Settings("C_AS_RGB")->asBool(); for(int i=0; i<m_pPoints->Get_Count(); i++) { TSG_Point_Z p = m_pPoints->Get_Point(i); p.z = m_pPoints->Get_Value(i, m_cField); int ix = (p.x - m_Extent.Get_XMin()) * dx; int iy = (p.y - m_Extent.Get_YMin()) * dy; if( ix >= 0 && ix <= m_Image.GetWidth() && iy >= 0 && iy < m_Image.GetHeight() ) { if( !bColorAsRGB ) { m_Image_Value[iy][ix] += p.z; } else { m_Image_Value[iy][ix] = p.z; } m_Image_Count[iy][ix]++; } } //------------------------------------------------- double zMin = m_pPoints->Get_Mean(m_cField) - 1.5 * m_pPoints->Get_StdDev(m_cField); double zRange = m_pPoints->Get_Mean(m_cField) + 1.5 * m_pPoints->Get_StdDev(m_cField) - zMin; CSG_Colors *pColors = m_Settings("COLORS")->asColors(); for(int iy=0; iy<m_Image.GetHeight(); iy++) { for(int ix=0; ix<m_Image.GetWidth(); ix++) { if( m_Image_Count[iy][ix] > 0 ) { if( !bColorAsRGB ) { int ic = (int)(pColors->Get_Count() * (m_Image_Value[iy][ix] / m_Image_Count[iy][ix] - zMin) / zRange); int c = pColors->Get_Color(ic < 0 ? 0 : ic >= pColors->Get_Count() ? pColors->Get_Count() - 1 : ic); _Draw_Pixel(ix, iy, c); } else { _Draw_Pixel(ix, iy, m_Image_Value[iy][ix]); } } else { _Draw_Pixel(ix, iy, 0); } } } return( true ); }
//--------------------------------------------------------- bool CExercise_14::Initialise(int Threshold) { int x, y, i, ix, iy, Dir; double z, dz, dzMax; CSG_Colors Colors; //----------------------------------------------------- m_pDir = new CSG_Grid(m_pDTM, SG_DATATYPE_Char); m_pChnl->Assign(); Colors.Set_Count(4); Colors.Set_Color(0, 192, 192, 192); // NOCHANNEL Colors.Set_Color(1, 0, 255, 0); // SPRING Colors.Set_Color(2, 0, 0, 255); // CHANNEL Colors.Set_Color(3, 255, 0, 0); // MOUTH DataObject_Set_Colors(m_pChnl, Colors); //----------------------------------------------------- for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { Dir = -1; if( is_InGrid(x, y) && !m_pDTM->is_NoData(x, y) ) { z = m_pDTM->asDouble(x, y); dzMax = 0.0; for(i=0; i<8; i++) { ix = Get_xTo(i, x); iy = Get_yTo(i, y); if( is_InGrid(ix, iy) && !m_pDTM->is_NoData(ix, iy) ) { dz = (z - m_pDTM->asDouble(ix, iy)) / Get_Length(i); if( dz > dzMax ) { dzMax = dz; Dir = i; } } } } //--------------------------------------------- m_pDir->Set_Value(x, y, Dir); if( Dir >= 0 ) { m_pChnl->Add_Value(Get_xTo(Dir, x), Get_yTo(Dir, y), 1); } } } //----------------------------------------------------- for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { m_pChnl->Set_Value(x, y, m_pChnl->asInt(x, y) >= Threshold ? SPRING : NOCHANNEL); } } return( true ); }
//--------------------------------------------------------- bool CGrid_CVA::On_Execute(void) { //----------------------------------------------------- CSG_Parameter_Grid_List *pA = Parameters("A")->asGridList(); CSG_Parameter_Grid_List *pB = Parameters("B")->asGridList(); CSG_Parameter_Grid_List *pC = Parameters("C")->asGridList(); if( pA->Get_Grid_Count() != pB->Get_Grid_Count() ) { Error_Set(_TL("number of initial and final state grids differs")); return( false ); } if( pA->Get_Grid_Count() == 0 ) { Error_Set(_TL("no grids in list")); return( false ); } //----------------------------------------------------- int n = pA->Get_Grid_Count(); bool bAngle = Parameters("ANGLE")->asBool() && n == 2; bool bC_Out = Parameters("C_OUT")->asBool(); CSG_Grid *pDist = Parameters("DIST")->asGrid(); CSG_Grid *pDir = Parameters("DIR" )->asGrid(); //----------------------------------------------------- pC->Del_Items(); if( bC_Out ) { for(int i=0; i<n; i++) { CSG_Grid *pGrid = SG_Create_Grid(Get_System()); pGrid->Fmt_Name("%s %01d", _TL("Change Vector"), i + 1); pC->Add_Item(pGrid); } } //----------------------------------------------------- CSG_Parameter *pLUT; CSG_Colors Colors; Colors.Set_Count(100); Colors.Set_Ramp(SG_GET_RGB(255, 255, 255), SG_GET_RGB( 0, 127, 127), 0, Colors.Get_Count() / 2); Colors.Set_Ramp(SG_GET_RGB( 0, 127, 127), SG_GET_RGB(255, 0, 0), Colors.Get_Count() / 2, Colors.Get_Count()); DataObject_Set_Colors(pDist, Colors); if( (pLUT = DataObject_Get_Parameter(pDir, "LUT")) == NULL || pLUT->asTable() == NULL || bAngle ) { Colors.Set_Default(100); Colors.Set_Ramp_Brighness(255, 0, 0, Colors.Get_Count() / 2); Colors.Set_Ramp_Brighness( 0, 255, Colors.Get_Count() / 2, Colors.Get_Count()); DataObject_Set_Colors(pDir, Colors); DataObject_Set_Parameter(pDir, "COLORS_TYPE", 2); } else { pLUT->asTable()->Del_Records(); for(int i=0, nClasses=(int)pow(2.0, n); i<nClasses; i++) { CSG_String s; for(int j=0; j<n; j++) { s += i & (int)pow(2.0, j) ? '+' : '-'; } CSG_Table_Record *pClass = pLUT->asTable()->Add_Record(); pClass->Set_Value(1, s); pClass->Set_Value(3, i); pClass->Set_Value(4, i); } Colors.Set_Count(pLUT->asTable()->Get_Count()); Colors.Random(); for(int c=0; c<pLUT->asTable()->Get_Count(); c++) { pLUT->asTable()->Get_Record(c)->Set_Value(0, Colors.Get_Color(c)); } DataObject_Set_Parameter(pDir, pLUT); DataObject_Set_Parameter(pDir, "COLORS_TYPE", 1); // Color Classification Type: Lookup Table } //----------------------------------------------------- for(int y=0; y<Get_NY() && Set_Progress(y); y++) { #pragma omp parallel for for(int x=0; x<Get_NX(); x++) { bool bOkay; int i, j; double d; CSG_Vector v(n); for(i=0, bOkay=true; i<n && bOkay; i++) { if( pA->Get_Grid(i)->is_NoData(x, y) || pB->Get_Grid(i)->is_NoData(x, y) ) { bOkay = false; } else { v[i] = pB->Get_Grid(i)->asDouble(x, y) - pA->Get_Grid(i)->asDouble(x, y); } } //--------------------------------------------- if( bOkay ) { if( bAngle ) { d = atan2(v[0], v[1]); } else for(i=0, j=1, d=0.0; i<n; i++, j*=2) { if( v[i] >= 0.0 ) { d += j; } } pDist->Set_Value(x, y, v.Get_Length()); pDir ->Set_Value(x, y, d); for(i=0; i<n && bC_Out; i++) { pC->Get_Grid(i)->Set_Value(x, y, v[i]); } } //--------------------------------------------- else { pDist->Set_NoData(x, y); pDir ->Set_NoData(x, y); for(i=0; i<n && bC_Out; i++) { pC->Get_Grid(i)->Set_NoData(x, y); } } } } return( true ); }
//--------------------------------------------------------- bool CGrid_Color_Rotate::On_Execute(void) { bool bDown; int i; long c; CSG_Grid *pGrid; CSG_Colors *pColors; pGrid = Parameters("GRID")->asGrid(); pColors = Parameters("COLORS")->asColors(); bDown = Parameters("DIR")->asBool(); if( pColors->Get_Count() > 1 ) { do { if( bDown ) { for(i=0, c=pColors->Get_Color(0); i<pColors->Get_Count() - 1; i++) { pColors->Set_Color(i, pColors->Get_Color(i + 1)); } pColors->Set_Color(pColors->Get_Count() - 1, c); } else { for(i=pColors->Get_Count()-1, c=pColors->Get_Color(pColors->Get_Count()-1); i>0; i--) { pColors->Set_Color(i, pColors->Get_Color(i - 1)); } pColors->Set_Color(0, c); } DataObject_Set_Colors(pGrid, *pColors); DataObject_Update(pGrid, true); } while( Process_Get_Okay(true) ); return( true ); } return( false ); }
//--------------------------------------------------------- bool CGrid_Export::On_Execute(void) { //----------------------------------------------------- int y, iy, Method; double dTrans; CSG_Grid *pGrid, *pShade, Grid, Shade; //----------------------------------------------------- pGrid = Parameters("GRID" )->asGrid(); pShade = Parameters("SHADE" )->asGrid(); Method = Parameters("COLOURING" )->asInt (); dTrans = Parameters("SHADE_TRANS" )->asDouble() / 100.0; if( !pGrid ) { return( false ); } //----------------------------------------------------- if( Method == 5 ) // same as in graphical user interface { if( !SG_UI_DataObject_asImage(pGrid, &Grid) ) { Error_Set("could not retrieve colour coding from graphical user interface."); return( false ); } } else { double zMin, zScale; CSG_Colors Colors; CSG_Table LUT; if( SG_UI_Get_Window_Main() ) { Colors.Assign(Parameters("COL_PALETTE")->asColors()); } else { Colors.Set_Palette( Parameters("COL_PALETTE")->asInt (), Parameters("COL_REVERT" )->asBool(), Parameters("COL_COUNT" )->asInt () ); } switch( Method ) { case 0: // stretch to grid's standard deviation zMin = pGrid->Get_Mean() - Parameters("STDDEV")->asDouble() * pGrid->Get_StdDev(); zScale = Colors.Get_Count() / (2 * Parameters("STDDEV")->asDouble() * pGrid->Get_StdDev()); break; case 1: // stretch to grid's value range zMin = pGrid->Get_ZMin(); zScale = Colors.Get_Count() / pGrid->Get_ZRange(); break; case 2: // stretch to specified value range zMin = Parameters("STRETCH")->asRange()->Get_LoVal(); if( zMin >= (zScale = Parameters("STRETCH")->asRange()->Get_HiVal()) ) { Error_Set(_TL("invalid user specified value range.")); return( false ); } zScale = Colors.Get_Count() / (zScale - zMin); break; case 3: // lookup table if( !Parameters("LUT")->asTable() || Parameters("LUT")->asTable()->Get_Field_Count() < 5 ) { Error_Set(_TL("invalid lookup table.")); return( false ); } LUT.Create(*Parameters("LUT")->asTable()); break; case 4: // rgb coded values break; } //------------------------------------------------- Grid.Create(*Get_System(), SG_DATATYPE_Int); for(y=0, iy=Get_NY()-1; y<Get_NY() && Set_Progress(y); y++, iy--) { #pragma omp parallel for for(int x=0; x<Get_NX(); x++) { double z = pGrid->asDouble(x, y); if( Method == 3 ) // lookup table { int i, iColor = -1; for(i=0; iColor<0 && i<LUT.Get_Count(); i++) { if( z == LUT[i][3] ) { Grid.Set_Value(x, iy, LUT[iColor = i].asInt(0)); } } for(i=0; iColor<0 && i<LUT.Get_Count(); i++) { if( z >= LUT[i][3] && z <= LUT[i][4] ) { Grid.Set_Value(x, iy, LUT[iColor = i].asInt(0)); } } if( iColor < 0 ) { Grid.Set_NoData(x, iy); } } else if( pGrid->is_NoData(x, y) ) { Grid.Set_NoData(x, iy); } else if( Method == 4 ) // rgb coded values { Grid.Set_Value(x, iy, z); } else { int i = (int)(zScale * (z - zMin)); Grid.Set_Value(x, iy, Colors[i < 0 ? 0 : i >= Colors.Get_Count() ? Colors.Get_Count() - 1 : i]); } } } } //----------------------------------------------------- if( !pShade || pShade->Get_ZRange() <= 0.0 ) { pShade = NULL; } else { double dMinBright, dMaxBright; dMinBright = Parameters("SHADE_BRIGHT")->asRange()->Get_LoVal() / 100.0; dMaxBright = Parameters("SHADE_BRIGHT")->asRange()->Get_HiVal() / 100.0; if( dMinBright >= dMaxBright ) { SG_UI_Msg_Add_Error(_TL("Minimum shade brightness must be lower than maximum shade brightness!")); return( false ); } int nColors = 100; CSG_Colors Colors(nColors, SG_COLORS_BLACK_WHITE, true); //------------------------------------------------- Shade.Create(*Get_System(), SG_DATATYPE_Int); for(y=0, iy=Get_NY()-1; y<Get_NY() && Set_Progress(y); y++, iy--) { #pragma omp parallel for for(int x=0; x<Get_NX(); x++) { if( pShade->is_NoData(x, y) ) { Shade.Set_NoData(x, iy); } else { Shade.Set_Value (x, iy, Colors[(int)(nColors * (dMaxBright - dMinBright) * (pShade->asDouble(x, y) - pShade->Get_ZMin()) / pShade->Get_ZRange() + dMinBright)]); } } } } //----------------------------------------------------- wxImage Image(Get_NX(), Get_NY()); if( Grid.Get_NoData_Count() > 0 ) { Image.SetAlpha(); } for(y=0; y<Get_NY() && Set_Progress(y); y++) { #pragma omp parallel for for(int x=0; x<Get_NX(); x++) { if( Grid.is_NoData(x, y) || (pShade != NULL && Shade.is_NoData(x, y)) ) { if( Image.HasAlpha() ) { Image.SetAlpha(x, y, wxIMAGE_ALPHA_TRANSPARENT); } Image.SetRGB(x, y, 255, 255, 255); } else { if( Image.HasAlpha() ) { Image.SetAlpha(x, y, wxIMAGE_ALPHA_OPAQUE); } int r, g, b, c = Grid.asInt(x, y); r = SG_GET_R(c); g = SG_GET_G(c); b = SG_GET_B(c); if( pShade ) { c = Shade.asInt(x, y); r = dTrans * r + SG_GET_R(c) * (1.0 - dTrans); g = dTrans * g + SG_GET_G(c) * (1.0 - dTrans); b = dTrans * b + SG_GET_B(c) * (1.0 - dTrans); } Image.SetRGB(x, y, r, g, b); } } } //------------------------------------------------- CSG_String fName(Parameters("FILE")->asString()); if( !SG_File_Cmp_Extension(fName, SG_T("bmp")) && !SG_File_Cmp_Extension(fName, SG_T("jpg")) && !SG_File_Cmp_Extension(fName, SG_T("pcx")) && !SG_File_Cmp_Extension(fName, SG_T("png")) && !SG_File_Cmp_Extension(fName, SG_T("tif")) ) { fName = SG_File_Make_Path(NULL, fName, SG_T("png")); Parameters("FILE")->Set_Value(fName); } //----------------------------------------------------- wxImageHandler *pImgHandler = NULL; if( !SG_UI_Get_Window_Main() ) { if( SG_File_Cmp_Extension(fName, SG_T("jpg")) ) pImgHandler = new wxJPEGHandler; else if( SG_File_Cmp_Extension(fName, SG_T("pcx")) ) pImgHandler = new wxPCXHandler; else if( SG_File_Cmp_Extension(fName, SG_T("tif")) ) pImgHandler = new wxTIFFHandler; #ifdef _SAGA_MSW else if( SG_File_Cmp_Extension(fName, SG_T("bmp")) ) pImgHandler = new wxBMPHandler; #endif else // if( SG_File_Cmp_Extension(fName, SG_T("png")) ) pImgHandler = new wxPNGHandler; wxImage::AddHandler(pImgHandler); } if( !Image.SaveFile(fName.c_str()) ) { Error_Set(CSG_String::Format(SG_T("%s [%s]"), _TL("could not save image file"), fName.c_str())); return( false ); } pGrid->Get_Projection().Save(SG_File_Make_Path(NULL, fName, SG_T("prj")), SG_PROJ_FMT_WKT); //----------------------------------------------------- CSG_File Stream; if( SG_File_Cmp_Extension(fName, SG_T("bmp")) ) Stream.Open(SG_File_Make_Path(NULL, fName, SG_T("bpw")), SG_FILE_W, false); else if( SG_File_Cmp_Extension(fName, SG_T("jpg")) ) Stream.Open(SG_File_Make_Path(NULL, fName, SG_T("jgw")), SG_FILE_W, false); else if( SG_File_Cmp_Extension(fName, SG_T("pcx")) ) Stream.Open(SG_File_Make_Path(NULL, fName, SG_T("pxw")), SG_FILE_W, false); else if( SG_File_Cmp_Extension(fName, SG_T("png")) ) Stream.Open(SG_File_Make_Path(NULL, fName, SG_T("pgw")), SG_FILE_W, false); else if( SG_File_Cmp_Extension(fName, SG_T("tif")) ) Stream.Open(SG_File_Make_Path(NULL, fName, SG_T("tfw")), SG_FILE_W, false); if( Stream.is_Open() ) { Stream.Printf(SG_T("%.10f\n%f\n%f\n%.10f\n%.10f\n%.10f\n"), pGrid->Get_Cellsize(), 0.0, 0.0, -pGrid->Get_Cellsize(), pGrid->Get_XMin(), pGrid->Get_YMax() ); } //----------------------------------------------------- if( Parameters("FILE_KML")->asBool() ) { CSG_MetaData KML; KML.Set_Name("kml"); KML.Add_Property("xmlns", "http://www.opengis.net/kml/2.2"); // CSG_MetaData *pFolder = KML.Add_Child("Folder"); // pFolder->Add_Child("name" , "Raster exported from SAGA"); // pFolder->Add_Child("description", "System for Automated Geoscientific Analyses - www.saga-gis.org"); // CSG_MetaData *pOverlay = pFolder->Add_Child("GroundOverlay"); CSG_MetaData *pOverlay = KML.Add_Child("GroundOverlay"); pOverlay->Add_Child("name" , pGrid->Get_Name()); pOverlay->Add_Child("description", pGrid->Get_Description()); pOverlay->Add_Child("Icon" )->Add_Child("href", SG_File_Get_Name(fName, true)); pOverlay->Add_Child("LatLonBox" ); pOverlay->Get_Child("LatLonBox" )->Add_Child("north", pGrid->Get_YMax()); pOverlay->Get_Child("LatLonBox" )->Add_Child("south", pGrid->Get_YMin()); pOverlay->Get_Child("LatLonBox" )->Add_Child("east" , pGrid->Get_XMax()); pOverlay->Get_Child("LatLonBox" )->Add_Child("west" , pGrid->Get_XMin()); KML.Save(fName, SG_T("kml")); } //----------------------------------------------------- if( !SG_UI_Get_Window_Main() && pImgHandler != NULL) { wxImage::RemoveHandler(pImgHandler->GetName()); } //----------------------------------------------------- return( true ); }