首页 > 代码库 > ImageBasics
ImageBasics
1 //ImageBasics.cpp 2 3 /* 4 Copyright 2000-2004 The VCF Project. 5 Please see License.txt in the top level directory 6 where you installed the VCF. 7 */ 8 9 10 #include "vcf/ApplicationKit/ApplicationKit.h" 11 #include "vcf/GraphicsKit/Win32Image.h" 12 #include "vcf/GraphicsKit/Pixels.h" 13 14 15 16 using namespace VCF; 17 18 /** 19 This example will demonstrate some of the basics of 20 working with images 21 */ 22 23 24 class ImageBasicsWindow : public Window { 25 public: 26 ImageBasicsWindow() { 27 setCaption( "ImageBasics" ); 28 } 29 30 virtual ~ImageBasicsWindow(){}; 31 32 virtual void paint( GraphicsContext* ctx ) { 33 Window::paint( ctx ); 34 35 /** 36 This will create an image from a given file name 37 */ 38 Image* logoImage = GraphicsToolkit::createImage( "logo.bmp" ); 39 40 /** 41 Simplest way to draw an image 42 */ 43 44 double x = 20; 45 double y = 20; 46 47 ctx->drawImage( x, y, logoImage ); 48 49 y += logoImage->getHeight() + 20; 50 51 /** 52 Draw a portion of the image 53 */ 54 55 Rect r( 0, 0, logoImage->getWidth(), logoImage->getHeight() ); 56 r.offset( x, y ); 57 58 //original bounds 59 ctx->setColor( Color::getColor("black") ); 60 ctx->rectangle( &r ); 61 ctx->strokePath(); 62 63 r.inflate( -10, -10 ); 64 65 ctx->drawImageWithinBounds( &r, logoImage ); 66 67 y += logoImage->getHeight() + 20; 68 69 String imageInfo = Format( "Image width: %d, height: %d") 70 %logoImage->getWidth() % logoImage->getHeight(); 71 72 ctx->textAt( x, y, imageInfo ); 73 74 y += ctx->getTextHeight( imageInfo ) + 20; 75 76 /** 77 draw on the image 78 */ 79 /** 80 first lock the image 81 */ 82 logoImage->beginDrawing(); 83 84 GraphicsContext* imgCtx = logoImage->getImageContext(); 85 86 /** 87 draw a red rectangle on the image 88 */ 89 imgCtx->setColor( Color::getColor("red") ); 90 91 imgCtx->rectangle( 20, 20, 100, 75 ); 92 imgCtx->fillPath(); 93 94 /** 95 unlock the image makes sure the image pixels are uptodate 96 */ 97 logoImage->finishedDrawing(); 98 99 /**100 draw the image again101 */102 ctx->drawImage( x, y, logoImage );103 104 y += logoImage->getHeight() + 20;105 106 /**107 bitblt a portion of the image context to our current ctx108 */109 logoImage->beginDrawing();110 imgCtx = logoImage->getImageContext();111 112 Rect src(0,0,100,50);113 Rect dest(10, y, 10 + src.getWidth(), y + src.getHeight() );114 115 ctx->copyContext( src, dest, imgCtx );116 117 logoImage->finishedDrawing();118 119 y += dest.getHeight() + 20;120 121 /**122 Now draw the image transparently. Note that this does not use any sort123 of alpha channel transparency124 */125 126 logoImage->setIsTransparent( true );127 logoImage->setTransparencyColor( Color::getColor("red") );128 129 ctx->drawImage( x, y, logoImage );130 131 y += logoImage->getHeight() + 20;132 133 134 logoImage->setIsTransparent( false );135 /**136 Manipulate the pixel bits137 */138 139 /**140 retrieve the image bits - you‘ll get a pointer to141 a SysPixelType142 */143 {144 ColorPixels pix(logoImage);//->getImageBits()->pixels_;145 146 /**147 Calculate the size, width * height148 */149 int size = logoImage->getHeight() * logoImage->getWidth();150 /**151 manipulate the values of the green color channel152 */153 for ( int i=0;i<size;i++ ) {154 pix[i].g = minVal<>( 255, pix[i].g + 50 );155 }156 157 158 ctx->drawImage( x, y, logoImage );159 }160 161 /**162 delete the image cause we don‘t need it anymore163 */164 delete logoImage;165 166 }167 };168 169 170 171 172 class ImageBasicsApplication : public Application {173 public:174 175 ImageBasicsApplication( int argc, char** argv ) : Application(argc, argv) {176 177 }178 179 virtual bool initRunningApplication(){180 bool result = Application::initRunningApplication();181 182 Window* mainWindow = new ImageBasicsWindow();183 setMainWindow(mainWindow);184 185 mainWindow->setBounds( 100.0, 100.0, 400.0, 875.0 );186 mainWindow->show();187 188 189 return result;190 }191 192 };193 194 195 int main(int argc, char *argv[])196 {197 Application* app = new ImageBasicsApplication( argc, argv );198 199 Application::main();200 201 return 0;202 }203 204 205 /**206 $Id: ImageBasics.cpp 2647 2006-04-28 16:42:38Z kdmix $207 */
ImageBasics
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。