title: C++ create png/bitmap from array of numbers
body: <p>So i found this link regarding my question, but it is for c#</p>
  
  <p><a href="https://stackoverflow.com/questions/32203347/create-a-png-from-an-array-of-bytes">Create a PNG from an array of bytes</a></p>
  
  <p>I have a variable int array of numbers.
  i will call it  "pix[ ]"
  for now it can be any size from 3 to 256, later possibly bigger.</p>
  
  <p>What i want to do now, is to convert it into a pixel image.
  I am still a noobin c++ so pleas excuse me.
  I tried to download some libaries that make working with libpng easier, but they do not seem to be working (ubuntu, code::blocks)
  So i have questions in the following:</p>
  
  <p>1) how do you create a new bitmap (which libaries, which command)?</p>
  
  <p>2) how do i fill it with information from  "pix[ ]" ?</p>
  
  <p>3) how do i save it?</p>
  
  <p>if it is a repost of a question i am happy about a link also ;)</p>
  
  <p>Here is what i worked out so far, thanks for your help.</p>
  
  <pre><code>int main(){
   FILE *imageFile;
   int x,y,pixel,height=2,width=3;
  
   imageFile=fopen("image.pgm","wb");
   if(imageFile==NULL){
    perror("ERROR: Cannot open output file");
    exit(EXIT_FAILURE);
   }
  
   fprintf(imageFile,"P3\n");           // P3 filetype
   fprintf(imageFile,"%d %d\n",width,height);   // dimensions
   fprintf(imageFile,"255\n");          // Max pixel
    int pix[100] {200,200,200, 100,100,100, 0,0,0, 255,0,0, 0,255,0, 0,0,255};
  
  
         fwrite(pix,1,18,imageFile);
  
  
  fclose(imageFile);
  }
  </code></pre>
  
  <p>i have not fully understood what it does. i can open the output image, but it is not a correct representation of the Array.</p>
  
  <p>If i change things around, for example making a 2 dimensional array, then the image viewer tells me "expected an integer" and doesn't show me an image.</p>
  
  <p>So far so good.
  As i have the array before the image i created a function <code>aufrunden</code> to round up to the next int number because i want to create a square image.</p>
  
  <pre><code>int aufrunden (double h)
  {
  int i =h;
  if (h-i == 0)
    {
    return i;
    }
  else
    {
    i = h+1;
    return i;
    }
  }
  </code></pre>
  
  <p>This function is used in the creation of the image.
  If the image is bigger than the information the array provides like this (<code>a</code> is the length of th array)</p>
  
  <pre><code>double h;
  h= sqrt(a/3.0);
  int i = aufrunden(h);
  FILE *imageFile;                           
  int height=i,width=i;
  </code></pre>
  
  <p>It might happen now, that the array is <code>a=24</code> long. <code>aufrunden</code> makes the image 3x3 so it has 27 values...meaning it is missing the values for 1 pixel.
  Or worse it is only <code>a=23</code> long. also creating a 3x3 image.</p>
  
  <p>What will <code>fwrite(pix,1,18,imageFile);</code> write in those pixels for information? It would be best if the remaing values are just 0. </p>
  
  <p>*edit never mind, i will just add 0 to the end of the array until it is filling up the whole square...sorry</p>