Thread: Image analyse

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    13

    Image analyse

    Hey, ok I recently started learning C++ but I have full knowledge in C, but I have a question:

    I want to make an app, that first asksyou to browse and you choose a specific image, in this image, there is a blue( or any color) pixel on it, it is example on x 52 and y 46 ( abscisse ordonne ( I learnt programming in French sorry) it's a png image.
    And i wantthe app to show a message or error if it finds the blue pixel...

    Can anybody help with the whole code? It's pretty urgent sorry

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This will be a different process for each image type, and also there are variations in the format possible in many types, meaning you need information from the header first. The "header" is file format information contained in the first few (say, 64) bytes of the file.

    However, I think SDL provides a transparent interface to all common image types. It also provides a lot of other stuff, so it is kind of a heavy handed solution (AFAIK you can't pick and choose components of SDL):

    Simple DirectMedia Layer

    Beyond that, look into the low level, type specific libraries such as libpng and libjpeg. You are lucky, SDL and all these are C API's.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    13
    Quote Originally Posted by MK27 View Post
    This will be a different process for each image type, and also there are variations in the format possible in many types, meaning you need information from the header first. The "header" is file format information contained in the first few (say, 64) bytes of the file.

    However, I think SDL provides a transparent interface to all common image types. It also provides a lot of other stuff, so it is kind of a heavy handed solution (AFAIK you can't pick and choose components of SDL):

    Simple DirectMedia Layer

    Beyond that, look into the low level, type specific libraries such as libpng and libjpeg. You are lucky, SDL and all these are C API's.
    cant you just be more specific? i mean can you give the code how to do that and the library. SDl isnt really my friend... i hate it.. but if its the only way ill do it but please just go straight to the point. The picture is in png.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by geniusisgenius View Post
    cant you just be more specific? i mean can you give the code how to do that and the library.
    I don't have it, but I know enough about it to tell you it is not trivial so it is unlikely anyone else would bother posting it, esp since there is a wealth of examples and information about this available on line. The point is, this is going to take you a while to do, there will be no quick solution.

    SDl isnt really my friend... i hate it.. but if its the only way ill do it but please just go straight to the point. The picture is in png.
    Then use libpng. But I'll guess if you find SDL hard to use, you'll find libpng even harder. If it's too hard, you just have to accept the fact that your programming and research skills are not up to the task, and wait until they get better. Of course, one way to improve them would be to grit your teeth and try with this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Seriously if you are time-impaired I would limit your image file choice to one common type (say jpg or png), get just the library for that image file type and learn it. While no one here will do your work for you if you are in a hurry and don't like learning things, realistically you can google for something like 'libpng example code' which will invariably yield C or C++ examples on doing all sorts of things. I am sure you will find the method to open the file simple and then accessing the raw data maybe a little less simple but still pretty easy. I used this method a few years ago when I had to write low-level jpg code to create thumbnails and full screen versions of any given image file. Of course if you choose this method and either have bugs now or need to explain your code later, you will be up a creek without a paddle if you don't take the time to learn the library.

    What MK said is 100% true: some of this code is non-trivial but worse for you, I have not seen a library that works equally well on all image types (that is free; I get the feeling you don't want to sink money into your solution..). The problem for you is that the only lib that might (libimagemagick) will require a lot of learning.....
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I don't use it much (except the GUI viewer, and convert), but Imagemagick always looks to me like a freaking great tool from the whole concept on up. Convert is a real handy timesaver for doing things like automating thumbnail creation.

    man convert
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    13
    ok thanks i found one, magick++ and i had the code source from a website, the only problem is i aint know how to install it....

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by geniusisgenius View Post
    ok thanks i found one, magick++ and i had the code source from a website, the only problem is i aint know how to install it....
    What operating system?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    13
    Windows vista and I have another pc on xp

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by geniusisgenius View Post
    Windows vista and I have another pc on xp
    Can't help you there, but there is a "Install from source -- Windows" link on the ImageMagick homepage:

    ImageMagick: Convert, Edit, and Compose Images

    You might not need the source to use the library, there is a binary download too.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    since i am working on a program that does something similar, i can just give you some code .. most of this is included in libpng documentation though , you should of checked

    WRITTEN IN C , by me and the help of various online sources! (including this forum)
    Code:
    png_uint_32 img_width;
    png_uint_32 img_height;
    png_uint_32 bit_depth;
    png_uint_32 channels;
    png_uint_32 color_type;
    unsigned long long stride;
    
    typedef struct color* colorp;
    struct color
    {
    	unsigned char r,g,b,a;
    };
    
    char color_compare(int r,int g,int b, int a, colorp color_comp)
    {
    	if( color_comp->r == r && color_comp->g == g && color_comp->b == b && color_comp->a == a )
    		return 0;
    	else
    		return 1;
    }
    char contains_blue(const char* filename, int r,int g,int b, int a)
    {
    
    	png_bytepp png_rows = read_png(filename); // png_rows is now a COLOR_BYTE ARRAY .. perfect for most work with images
    	assert(channels == 3 || channels == 4);
            
            //we will set r g and b to be BLUE .. just for your case
            r=0;g=0;b=255;a=255;
            struct color color_blue = { r,g,b,a};
    		int i,j;
    	for(i=0;i<img_height;++i)
    	{
    		for(j=0;j<stride;j+=channels)
            // the number of bytes in each row is divisible by the number of channels
    		{
    
    			if( !color_compare( png_rows[i][j], png_rows[i][j+1],png_rows[i][j+2], channels == 4 ? png_rows[i][j+3] : 255, &color_blue)  )
    			{
    				return 1;
    			}
    		}
    	}
    return 0;
    }
    
    
    png_bytepp read_png(const char* file_name)
    {
    	FILE *fp = fopen(file_name, "rb");
    	if(fp)
    	{
    		printf("-%s loaded\n\t",file_name);
    	}
    	else
    	{
    		printf("-Error loading file\n");
    		return;
    	}
    	
    	const int header_signature_size = 8;
    	assert( header_signature_size <= 8 );
    
    	void* header = malloc(header_signature_size);
    
    	int bytes_read;
    	if( (bytes_read = fread(header, 1, header_signature_size, fp)) && bytes_read == header_signature_size)
    	{
    		printf("-Header read (%d bytes read)\n\t", bytes_read);
    	}
    	else
    	{
    		printf("-Error reading Header data, %d bytes read\n",bytes_read);
    		return;
    	}
    	
    	int is_png = png_sig_cmp(header, 0, header_signature_size);
    	if( is_png )
    	{
    		printf("-File is not of PNG format\n\t");
    		return;
    	}
    	else
    		printf("-File is PNG format\n\t");
    		
    	free(header);
    
    	png_structp png_ptr = png_create_read_struct(
    							 PNG_LIBPNG_VER_STRING, NULL,NULL,NULL);
    	if(png_ptr == NULL)
    	{
    		printf("-Allocation of png_struct failed\n");
    		return;
    	}
    	else
    		printf("-Allocated png_struct\n\t");
    		
    	png_infop info_ptr = png_create_info_struct(png_ptr);
    	if(info_ptr == NULL)
    	{
    		png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
    		printf("-Allocation of info struct failed\n\t");
    		return;
    	}
    	else
    	{
    		printf("-Allocated png_info struct\n\t");
    	}
    	
    	png_infop end_ptr = png_create_info_struct(png_ptr);
    	if(info_ptr == NULL)
    	{
    		png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
    		printf("-Allocation of end_info struct failed\n");
    		return;
    	}
    	else
    	{
    		printf("-Allocated end_info struct\n\t");
    	}
    	
    	png_init_io(png_ptr, fp);
    	printf("-Initialized PNG I/O\n\t");
    	
    	png_set_sig_bytes(png_ptr, header_signature_size);
    	printf("-Set offset for header signature\n\t");
    	
    	png_read_info(png_ptr, info_ptr);
    	printf("-Read image data from header into info_ptr\n\t");
    	
    	img_width = png_get_image_width(png_ptr,info_ptr);
    	img_height = png_get_image_height(png_ptr,info_ptr);
    	bit_depth = png_get_bit_depth(png_ptr,info_ptr);
    	channels = png_get_channels(png_ptr,info_ptr);
    	color_type = png_get_color_type(png_ptr, info_ptr);
    	printf("-File info:\n\t\t-Image Width,Height: (%d,%d)\n\t\t-Bit Depth: %d\n\t\t-Channels: %d\n\t\t-Color Type: %d\n\t",
    					(int)img_width,(int)img_height,(int)bit_depth,(int)channels,(int)color_type);
    	
    	switch (color_type) {
            case PNG_COLOR_TYPE_PALETTE:
                png_set_palette_to_rgb(png_ptr);
                channels = 3; 
                printf("-Color type set to be converted from PALETTE to RGB\n\t");          
                break;
            case PNG_COLOR_TYPE_GRAY:
                if (bit_depth < 8)
                png_set_gray_1_2_4_to_8(png_ptr);
                bit_depth = 8;
    			printf("-Color type set to be expanded to low-bit GRAY to 8-bit\n\t");  
                break;
        }
    
    
    	if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) 
    	{
            png_set_tRNS_to_alpha(png_ptr);
            channels+=1;
    		printf("-All tRNS Chunks set to be expanded to alpha channels\n\t");
        }
    	if(bit_depth == 16)
    	{
    		png_set_strip_16(png_ptr);
    		printf("-File set to be stripped from 16-bit depth to 8-bit depth\n\t");
    	}
    	
    	png_bytepp rows = malloc(img_height * sizeof(png_bytep));
    	char* data = malloc(img_height * img_width * channels * bit_depth / 8);
    	printf("-Allocated memory for row bytes\n\t");
    	
    	stride = img_width * channels * bit_depth / 8 ;
    	printf("-Determined row size in bytes to be %llu\n\t",stride);
    
    	int i;
    	for(i=0;i<img_height;++i)
    	{
    		rows[i] = &data[i*stride];
    	}
    	printf("-Assigned row addresses in memory\n\t");
    
    	png_read_image(png_ptr, rows);
    	printf("-Saved image in memory\n");
    
    	fclose(fp);
    	fp = NULL;
    	png_destroy_read_struct(&png_ptr,&info_ptr,&end_ptr);
    
    	return rows;
    }
    obviously doesn't account for partial transparency (255 only) .. very crude method .. blue has to be BLUE (0,0,255,255)
    doesn't free image from memory
    Last edited by rodrigorules; 05-18-2010 at 03:19 PM.

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Can anybody help with the whole code?
    Um....nope. Do some research and code it yourself. When you have specific problems come back here and ask for assistance. We do not do anyone's work for them and you wouldn't learn anything by it if we did.

    It's pretty urgent sorry
    To you perhaps....but to us it is not. A lack of planning on your part does not constitute and emergency on ours. We are not on your project and we are assisting you in our free time so I suggest you do some footwork of your own and start working on the code instead of being lazy and trying to get us to do it for you.

    If this was for a work project, and I doubt it is, I would feel sorry for the people on your team to have such a dead weight dragging them down.

    Do some research, do some code, and then maybe we will help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 04:17 AM
  2. Replies: 1
    Last Post: 05-27-2009, 12:46 PM
  3. Simple Image Processing
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2009, 12:10 PM
  4. Replies: 6
    Last Post: 03-03-2005, 03:52 AM
  5. Replies: 4
    Last Post: 03-02-2003, 09:12 AM