Like Tree17Likes

How to use loadJPG function from library jpeg?

This is a discussion on How to use loadJPG function from library jpeg? within the C Programming forums, part of the General Programming Boards category; Originally Posted by TheReceptionist not sure, actually. im assuming its an int, part of the binary file info and that ...

  1. #61
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    Quote Originally Posted by TheReceptionist View Post
    not sure, actually. im assuming its an int, part of the binary file info and that its one byte (instructions say each value is a byte)
    Did you write the code that declares alpha? That should tell you. Given that you're trying to fread just one byte into each one, h_r, h_g, h_b and alpha should probably all be declared as chars.

  2. #62
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by TheReceptionist View Post
    not sure, actually. im assuming its an int, part of the binary file info and that its one byte (instructions say each value is a byte)
    Didn't you declare it in your program some place, or is this another one of those stupid library provided items?
    Quote Originally Posted by anduril462 View Post
    Did you write the code that declares alpha? That should tell you. Given that you're trying to fread just one byte into each one, h_r, h_g, h_b and alpha should probably all be declared as chars.
    Should probably be unsigned char as well, since you said something about values "not higher than 255".


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #63
    Normander the Barbarian TheReceptionist's Avatar
    Join Date
    Jul 2011
    Location
    Gainesville,FL
    Posts
    24
    thanks, anduril. that seems to have fixed it (for the moment atleast lol) =D

    i had it down in my notes that int=1 byte and char=4.


    **EDIT

    thanks also, quzah! that was def my problem
    Last edited by TheReceptionist; 07-28-2011 at 04:16 PM.

  4. #64
    Normander the Barbarian TheReceptionist's Avatar
    Join Date
    Jul 2011
    Location
    Gainesville,FL
    Posts
    24
    getting a segmentation fault error that i havent been able to fix in 7 hours. ive looked stuff up and all it says is that it has to do with a reference to a memory location that it cant find/is non-existent, etc.

    running gdb, i get that the problem occurs:

    Code:
    Program received signal SIGSEGV, Segmentation fault.
    0x0000000000400e0d in main () at histogram3.c:45
    45     printf("%d %d %d %d\n\n",mem[i][0],mem[i][1],mem[i][2],mem[i][3]);
    
    (gdb) backtrace
    #0  0x0000000000400e0d in main () at histogram3.c:45
    
    (gdb) frame 0
    #0  0x0000000000400e0d in main () at histogram3.c:45
    45      printf("%d %d %d %d\n\n",mem[i][0],mem[i][1],mem[i][2],mem[i][3]);
    
    (gdb) print i
    $1 = 4
    
    (gdb) print mem[i][0]
    Cannot access memory at address 0x7fff00e3898e
    i know the problem occurs on the 4th loop but dont know why! X O
    malloc doesnt come back null and it takes some values, so all i can think is that it has to do with my malloc.

    Code:
    int main()
    	{
    		FILE *fp;
    
    		int *p,width,height,size,i=0,*mem_loc;
    
    		unsigned char pix[4],mem[height][4];
    		char *filename;
    
    		filename="/cise/homes/melendez/Lenna.jpg";
    
    		size=loadJPG(filename,"/cise/homes/melendez/output.bin");
    
    		fp=fopen("/cise/homes/melendez/output.bin","rb");
    
    		if (fp==NULL)											//pg547 example
    			{
    				printf("CANNOT OPEN FILE\n");
    			}
    		else
    			{
    				fread(&width,sizeof(width),1,fp);	//(char* str,int n,FILE* file)
    				fread(&height,sizeof(height),1,fp);
    
    				printf("DIMENSIONS(in pixels): %d x %d\n",width,height);
    
    				mem_loc=malloc(sizeof(int)*mem[height][4]);
    
    				if(mem_loc==NULL)
    					printf("FAIL\n");
    				else
    					{
    				while(i<height)
    					{
    						fread(&pix,1,4,fp);
    						printf("%d %d %d %d\n",pix[0],pix[1],pix[2],pix[3]);
    
    						strcpy(mem[i],pix);
    						printf("%d %d %d %d\n\n",mem[i][0],mem[i][1],mem[i][2],mem[i][3]);
    						i++;
    					}
    				}
    			}
    
    		//return mem_loc;
    	}

  5. #65
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by TheReceptionist View Post
    Code:
    int main()
    	{
    		FILE *fp;
    
    		int *p,width,height,size,i=0,*mem_loc;
    
    		unsigned char pix[4],mem[height][4];
    				
    .......................................................
    
                             mem_loc=malloc(sizeof(int)*mem[height][4]);
    Where do you define height? Also what do you think your malloc is doing? What is at location mem[height][4]?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #66
    Normander the Barbarian TheReceptionist's Avatar
    Join Date
    Jul 2011
    Location
    Gainesville,FL
    Posts
    24
    Quote Originally Posted by AndrewHunter View Post
    Where do you define height?
    that was the problem ~_~ THANK YOU!! deadlines creeping up and thats when mistakes tend to happen ^^;

  7. #67
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Right now you are telling malloc to make enough memory (int *mem[height][4]) which is not how large your mem array is. The compiler evaluates this as to (int * <whatever is in the location of your array @[height][4]>) If you want it to allocate enough memory to hold your array you would need malloc(sizeof(int)*height*4).
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #68
    Normander the Barbarian TheReceptionist's Avatar
    Join Date
    Jul 2011
    Location
    Gainesville,FL
    Posts
    24
    ohhhh, i see what youre talking about! this is where we have to use the pointer as an array (dynamically allocated storage for arrays)?

  9. #69
    Registered User
    Join Date
    Mar 2009
    Posts
    343
    If you're reading unsigned chars into pix/mem[][], why is mem_loc a pointer to an integer (or integer array)? Those types should be consistent.

    Also, since you're reading an image that's of size width x height, I'd expect both of those terms to be needed to figure out how many pixels there are to be read in total. And since you need to store all of them, you'd probably need to include both terms in the malloc size calculation.

  10. #70
    Normander the Barbarian TheReceptionist's Avatar
    Join Date
    Jul 2011
    Location
    Gainesville,FL
    Posts
    24
    thanks, KCfromNC. i figured that out at 4am when i got tired of not knowing the pixel values. i opened adobe and literally looked what the first/last few pixels RGB values were and realized it was only alloc enough for 256.

  11. #71
    Registered User
    Join Date
    Mar 2009
    Posts
    343
    Good deal. Now a couple more pointers.

    You have an array which now has enough space to hold all of the pixel values. You can modify your code to read it in one fread() call if you like and have it end up directly in the array you malloc()ed. Should clean things up a bit.

    That will also get rid of the strcpy() call, which is not at all what you want in this case. Even if you don't do the step I mention above, look into the difference between strcpy() and the more useful memcpy() function. More useful in this case, anyway.

    Also, become familiar with the unix utility od. It stands for octal dump, but really it's a tool for dumping out binary files in a human readable format. It has lots of options, but one that may be useful for testing is "od -t x1 file.bin" to dump out each byte of file.bin as a hex value. You can use printf("%2.2x", array[i]) in your code to print the data being read there and compare it against what is really in the file. Should be helpful for debugging if you run in to problems.

  12. #72
    Normander the Barbarian TheReceptionist's Avatar
    Join Date
    Jul 2011
    Location
    Gainesville,FL
    Posts
    24
    ohhh... ok, i see what your saying about 1 fread for all of it. tweaked that part already and checked that the right information was being copied, etc. thanks!!

    still having issues with returning *width,*height in the function. this is what i know
    Code:
    int i,*p;
    p=&i;   //p points to LOCATION of i
    *p=i;   //*p now equals VALUE of i
    p=i;   //not valid unless both are same types
    *p=2   //VALUE of 2 is stored at LOCATION of where p points, which is i (in this case)
    so, in "get_pixels" function, to have the argument 2 (int *width) contain the value of int w(which we get from the function) i need to first point *width to the address of w, assign the value of w to *width, THEN i can printf the value that w has?

    i dont get why when i print *width in the MAIN func it doesnt match the *width i print in the get_pixel func

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "/cise/homes/melendez/jpeg.h"
    
    unsigned char *get_pixels(char *filename, int *width, int *height)
    
    	{
    		FILE *fp;
    
    		int i=0,size,w,h;
    		unsigned char pix[4], *mem_loc;
    
    		size=loadJPG(filename,"/cise/homes/melendez/output.bin");
    
    		fp=fopen("/cise/homes/melendez/output.bin","rb");
    
    		if (fp==NULL)											//pg547 example
    			{
    				printf("CANNOT OPEN FILE\n");
    			}
    		else
    			{
    				fread(&w,sizeof(w),1,fp);
    				fread(&h,sizeof(h),1,fp);
    
    				width=&w;
    				height=&h;
    				
    				*width=w;
    				*height=h;
    
    				printf("\n%d %d\n",*width,*height);
    				printf("DIMENSIONS: %d x %d\n",w,h);
    
    				unsigned char mem[h*w][4];
    
    				mem_loc=malloc(sizeof(int)*h*w*4);
    
    				printf("%d\n",*mem_loc);
    
    				if(mem_loc==NULL)
    					printf("FAIL\n");
    				else
    					{
    				while(i<(h*w))
    					{
    						fread(&pix,1,4,fp);
    						//printf("%d %d %d %d\n",pix[0],pix[1],pix[2],pix[3]);
    						memcpy(mem[i],pix,sizeof(int));
    						//printf("%d %d %d %d\n\n",mem[i][0],mem[i][1],mem[i][2],mem[i][3]);
    						//printf("%d\n",i);
    						i++;
    					}
    				       }
    			printf("%d\n",i);
    			}
    
    		return mem_loc;
    
    	}
    int main()
    	{
    		int *width,*height,w,h;
    		char *filename;
    		unsigned char *p;
    
    		filename="/cise/homes/melendez/Lenna.jpg";
    		p=get_pixels(filename,width,height);
    		printf("%d\n",*p);
    
    		
    		printf("%d\n",*width);
    	}
    Code:
    thunder:15% ./load
    
    256 256
    DIMENSIONS: 256 x 256
    0
    65536
    0
    1

  13. #73
    Normander the Barbarian TheReceptionist's Avatar
    Join Date
    Jul 2011
    Location
    Gainesville,FL
    Posts
    24
    ok, NEVERMIND. i fixed it. someone else told me about passing ints to int *s; all you need is to add a &infront of the int being passed to an int*

  14. #74
    Normander the Barbarian TheReceptionist's Avatar
    Join Date
    Jul 2011
    Location
    Gainesville,FL
    Posts
    24
    one last question (FTW)

    im having trouble with fwrite. im sure everything else works coz i printf-ed it every time, but i dont know why its not working. i know fwrite returns the number of successfully written ELEMENTS (if its in an array).

    im trying to print the array[64] in groups of 4, so first line of output file would be array[0], array[1], array[2],array[3]. and the second line starts from array[4], etc....

    Code:
    void write_histogram(int *histogram, int bins_per_color, char *filename)
    	{
    		FILE *fp;
    		int i=0;
    		fp=fopen(filename,"w");
    
    		if (fp==NULL)												{
    				printf("CANNOT OPEN FILE\n");
    			}
    		else
    			{
    				//while(i<64)
    
    				//	{
    						fwrite(histogram,sizeof(int),4,fp);
    
    				//	}
    
    			}
    		fclose(fp);
    	}

  15. #75
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What problem are you having? fwrite is really for binay data. If you tell it to print the int 123:
    Code:
    fwrite( &myint, sizeof( myint ), 1, file );
    ...and you open the file in a text editor, you aren't going to see "123". "Lines" are really irrelevant when using fwrite. Sure I suppose you could work that into what you are doing somehow, but you don't generally do that.

    If you want text, use fprintf.


    Quzah.
    Hope is the first step on the road to disappointment.

Page 5 of 6 FirstFirst 123456 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function library (from scratch)?
    By SirJiub in forum C Programming
    Replies: 7
    Last Post: 06-05-2011, 01:48 PM
  2. how to know which function belongs to which library?
    By gibsosmat in forum Linux Programming
    Replies: 8
    Last Post: 11-18-2007, 06:46 AM
  3. The mathematical function library in C++
    By turkertopal in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2005, 06:54 AM
  4. Library Function to verify Int
    By Mr_roboto in forum C++ Programming
    Replies: 24
    Last Post: 09-29-2005, 02:11 PM
  5. library function
    By darfader in forum C Programming
    Replies: 4
    Last Post: 09-10-2003, 09:38 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21