linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* reading the camera device
@ 2002-11-29 10:30 Mat Harris
  0 siblings, 0 replies; only message in thread
From: Mat Harris @ 2002-11-29 10:30 UTC (permalink / raw)
  To: linux-c-programming


[-- Attachment #1.1: Type: text/plain, Size: 434 bytes --]

ok, i have this little bit of code to read the camera device and return the
buffer to another function for writing to a ppm image. the problem is that
when it gets to the read function, it hangs and never resumes. obviously
there is a problem somewhere in the way of length of reading the bytes in.

the program is attached.

-- 
Mat Harris			OpenGPG Public Key ID: C37D57D9
mat.harris@genestate.com	matthewh.genestate.com	

[-- Attachment #1.2: grab.c --]
[-- Type: text/plain, Size: 1601 bytes --]

/*Spiros Ioannou 2/2000 */
/*sivann@softlab.ece.ntua.gr*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <linux/types.h> 
#include <errno.h>
/*#include <linux/videodev.h>*/
#include "../conf.h"

void save_ppm(char *filename, unsigned char *pic);

int main()
{
	int dev;				/* the device we will use */
	int len;
	char device[FILENAME_SIZE] = "/dev/video0";
	unsigned char pic[MAX_RGBA_IMAGE_SIZE];
  	char filename[FILENAME_SIZE] = "./test.ppm";

	dev = open (device, O_RDWR);
	if (dev < 0)
	{
		perror(device);
		exit(1);
	}

	fprintf(stderr, "about to read\n");
	len = read(dev, pic, MAX_RGBA_IMAGE_SIZE);
	if (len == 0 || len == -1)
	{
		fprintf(stderr, "error reading camera device\n");
		exit(0);
	}
	fprintf(stderr, "read\n");

	/*while((len = read(dev, pic, MAX_RGBA_IMAGE_SIZE)) <= 0)
	{
		if(errno != 0 && errno != EINTR)
		{
			fprintf(stderr, "Error read image\n");
			exit(0);
		}
	}*/

	save_ppm(filename, pic);
	
	exit(0);	
}

void save_ppm(char *filename, unsigned char *pic)
{
  FILE *outfile;
  unsigned char buff[3];
  int i;

    if (!(outfile=fopen(filename,"w"))) {
      perror(filename);
      exit(errno);
    };
    fprintf (outfile, "P6\n%d %d\n%d\n", 640, 480, 255);
    for (i = 0; i < 640*480*3; i+=3) {
      buff[0] = pic[i];   /* r */
      buff[1] = pic[i+1]; /* g */
      buff[2] = pic[i+2]; /* b */
      fwrite (buff, 1, 3, outfile);
    }
    fclose(outfile);
}

[-- Attachment #1.3: conf.h --]
[-- Type: text/plain, Size: 771 bytes --]

/************************************************
 * this is the config file containing defaults	*
 * for the camera program. all defaults are	*
 * controlled with preprocessor macros		*
 ************************************************/

#define FILENAME_SIZE 20			/* char size for holding filenames */
#define DEFAULT_NOISE_THREASHOLD 60		/* level of noise */
#define DEFAULT_PIXEL_THREASHOLD 1500		/* number of changed pixels */
#define X_AXIS_IMAGE_SIZE 640			/* work it out */
#define Y_AXIS_IMAGE_SIZE 480			/* ditto */
#define DAY_LIGHT_LEVEL 180			/* mark to differentiate between day and night */
#define TIMEOUT_LEN 1000			/* ??? - doesn't seem to be used anywhere else */
#define MAX_RGBA_IMAGE_SIZE ((640*480*4)+256)	/* maximum size for capturing an image */

[-- Attachment #2: Type: application/pgp-signature, Size: 232 bytes --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2002-11-29 10:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-11-29 10:30 reading the camera device Mat Harris

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).