title: Reading output of a USB webcam in Linux
body: <p>I was experimenting with a little bit with fread and fwrite in C. So i wrote this little program in C to get data from a webcam and dump it into a file. The following is the source:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #define SIZE 307200 // number of pixels (640x480 for my webcam)
  int main() {
      FILE *camera, *grab;
      camera=fopen("/dev/video0", "rb");
      grab=fopen("grab.raw", "wb");
      float data[SIZE];
      fread(data, sizeof(data[0]), SIZE, camera);
      fwrite(data, sizeof(data[0]), SIZE, grab);
      fclose(camera);
      fclose(grab); 
      return 0;
  }
  </code></pre>
  
  <p>The program works when compiled (<code>gcc -o snap camera.c</code>). What took me by surprise was that the output file was not a raw data dump but a JPEG file. Output of the <code>file</code> command on linux on the programs output file showed it was a JPEG image data: JFIF Standard 1.01. The file was viewable on an image viewer, although a little saturated.</p>
  
  <p>How or why does this happen? I did not use any JPEG encoding libraries in the source or the program. Does the camera output JPEG natively? The webcam is a Sony Playstation 2 EyeToy which was manufactured by Logitech. The system is Debian Linux.</p>