/* * * compress.c - part of Danovitsch Webcam * * Copyright (C) 2001 by Daan Vreeken * * Published under the terms of the GNU Public License 2.0 * (or any later version) * */ #include #include #include "compress.h" #include "capture.h" #include "webcam.h" char *JPEGBuffer = NULL; int JPEGSize = 0; struct timeval JPEGTime; void CreateJPEG(void) { int X,Y; unsigned char Temp[ImageWidth][3]; unsigned char *TempPtr; unsigned char *FramePtr; char Buf[1000]; int InPipe[2]; int OutPipe[2]; int In; int Out; int Pid1,Pid2; int Readed = 1; int Position = 0; pipe(&InPipe[0]); pipe(&OutPipe[0]); Pid1=fork(); if (Pid1==0) { close(0); dup2(InPipe[0],0); close(InPipe[1]); close(1); dup2(OutPipe[0],1); close(OutPipe[1]); execl("/usr/local/bin/cjpeg","cjpeg","-quality",JPEGQuality,NULL); //*unreached* exit(0); } In=InPipe[1]; Out=OutPipe[1]; close(InPipe[0]); close(OutPipe[0]); Pid2=fork(); if (Pid2==0) { close(Out); sprintf(Buf,"P6\n%d %d 255\n",ImageWidth,ImageHeight); write(In,Buf,strlen(Buf)); for (Y=0; Y0) { if (JPEGBuffer==NULL) JPEGBuffer=(char *)malloc(Readed+Position); else JPEGBuffer=(char *)realloc(JPEGBuffer,Readed+Position); if (JPEGBuffer==NULL) ExitFatal("JPEGBuffer could not be allocated!"); memcpy(JPEGBuffer+Position,&Buf,Readed); Position+=Readed; Readed=read(Out,&Buf,sizeof(Buf)); } close(Out); JPEGSize=Position; Debug(10,"Got %d bytes of JPEG data\n",Position); waitpid(Pid2,NULL,0); waitpid(Pid1,NULL,0); }