473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Read .ppm into One Dimensional Arrays For Red Green and Blue Values

1
I am writing a program to store a ppm into one dimensional arrays in order to manipulate the data. The image that is being read is not correct. The code is as follows:
Expand|Select|Wrap|Line Numbers
  1. if (strcmp (str, "P6") == 0)
  2.  {
  3.  fscanf(fp, "%d %d ", &(I->NCols), &(I->NRows));
  4.   printf("\n\n******In the input image:******\n");
  5.  printf("\nColumns=%d and Rows=%d\n", (I->NCols),(I->NRows));
  6.  pixels =(I->NCols)*(I->NRows);
  7.  printf("There are %d pixels\n", pixels);
  8.  fscanf(fp, "%d ", &temp) ;
  9.  printf("%d\n", temp);
  10.  I->R = (int *) malloc(pixels*sizeof(int));
  11.  I->G = (int *) malloc(pixels*sizeof(int));
  12.  I->B = (int *) malloc(pixels*sizeof(int));
  13.  for (row=0; row < I->NRows; row++)
  14.  {
  15.      for (col=0; col < I->NCols; col++)
  16.      {
  17.          (I->R[row*I->NCols+col]) = (unsigned char) (getc(fp));
  18.          (I->G[row*I->NCols+col]) = (unsigned char) (getc(fp));
  19.          (I->B[row*I->NCols+col]) = (unsigned char) (getc(fp));
  20.  
  21.      }
  22.  }
  23.  
I think that the problem lies in this portion of the above code
Expand|Select|Wrap|Line Numbers
  1. (I->R[row*I->NCols+col]) = (unsigned char) (getc(fp));
  2.          (I->G[row*I->NCols+col]) = (unsigned char) (getc(fp));
  3.          (I->B[row*I->NCols+col]) = (unsigned char) (getc(fp));
  4.  
I'm not sure if I am using getc correctly. Now, I know that there are easier ways to do this. This method of one dimensional arrays is required by the assignment however. Essentially, there is a struct with items red, green and blue. Each of these is a pointer to a one dimensional array which contains only the red, green or blue values in the image. Anyway, let me know if I need to clarify and I thank anyone who can help in advance.

remync
Sep 22 '08 #1
2 2614
remync ,

Give more details on the structure of 'I' I can probably help.

Larry
Oct 2 '08 #2
manontheedge
175 100+
In your loop you have to adjust the COLUMN value, the value from the header, that you have in your variable is the number of PIXELS ... there are 3 values per pixel, so you have to multiply the column by 3 to properly get the data.
Oct 2 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: James | last post by:
I see that variations on this question have appeared before but I'm still completely stumped. I'm developing an application with a fairly robust graphics component for 3D rendering. I've written...
6
by: Ruben | last post by:
I'm trying to pass an array of string to a function without knowing how many strings I have beforehand. I've defined one functions as char * insert(char table,int cols, char values); out of...
9
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
8
by: masood.iqbal | last post by:
All this time I was under the illusion that I understand the concept of multi-dimensional arrays well ---- however the following code snippet belies my understanding. I had assumed all along...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
9
by: Dadi | last post by:
Hi, I can make a simple initialization work like this: Object ONE_ROW = {{"Vodafone", "5550160100197016"}}; But, now I want to create another array that consists of multiple copies of...
16
by: rguti | last post by:
Hi, How do I create a two dimensional array? I have created a one dimensional doing this: Dim laFields As ArrayList = New ArrayList How about to do a 2 dimensional?
22
by: spam.noam | last post by:
Hello, I discovered that I needed a small change to the Python grammar. I would like to hear what you think about it. In two lines: Currently, the expression "x" is a syntax error. I suggest...
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.