Example #1
0
void boxblurYUV(unsigned char* dest, const unsigned char* src,
        unsigned char* buffer, const DSFrameInfo* fi,
        unsigned int size, BoxBlurColorMode colormode){
  int localbuffer=0;
  int size2;
  int offset = fi->width * fi->height;
  if(size<2){
    if(dest!=buffer)
      memcpy(dest,src,fi->framesize);
    return;
  }
  if(buffer==0){
    buffer=(unsigned char*) ds_malloc(fi->framesize);
    localbuffer=1;
  }
  // odd and larger than 2 and maximally half of smaller image dimension
  size  = DS_CLAMP((size/2)*2+1,3,DS_MIN(fi->height/2,fi->width/2));
  //printf("%i\n",size);

  // luminance
  boxblur_hori_C(buffer,  src, fi->width, fi->height, fi->strive, size);
  boxblur_vert_C(dest, buffer, fi->width, fi->height, fi->strive, size);

  size2 = size/2+1;                        // odd and larger than 0
  switch (colormode){
  case BoxBlurColor:
    // color
    if(size2>1){
      boxblur_hori_C(buffer+offset, src+offset,
             fi->width/2, fi->height/2, fi->strive/2, size2);
      boxblur_hori_C(buffer+5*offset/4, src+5*offset/4,
             fi->width/2, fi->height/2, fi->strive/2, size2);

      boxblur_vert_C(dest+offset, buffer+offset,
             fi->width/2, fi->height/2, fi->strive/2, size2);
      boxblur_vert_C(dest+5*offset/4, buffer+5*offset/4,
             fi->width/2, fi->height/2, fi->strive/2, size2);
    }
    break;
  case BoxBlurKeepColor:
    // copy both color channels
    memcpy(dest+offset, src+offset, 2*(fi->strive / 2) * (fi->height / 2));
  case BoxBlurNoColor: // do nothing
  default:
    break;
  }

  if(localbuffer)
    ds_free(buffer);
}
Example #2
0
dlist *create_dlist(unsigned int num_nodes)
{
   if (0 == num_nodes)
   {
      return NULL;
   }
   else
   {
      dlist_node *p_node = (dlist_node *)ds_malloc(sizeof(dlist_node));
      p_node->id = num_nodes;
      p_node->next = p_node->pre = NULL;

      return grow_dlist_from_head(create_dlist(num_nodes-1), p_node);
   }
}
Example #3
0
static int ReadFile(char const *name, byte **buffer, int mallocType)
{
// AREIS BEGIN
#if !defined( _HEXENDS ) && !defined( _IPHONE ) //rww begin
	int handle, count, length;
	struct stat fileinfo;
	byte *buf;

	handle = open(name, O_RDONLY|O_BINARY, 0666);
	if(handle == -1)
	{
		I_Error("Couldn't read file %s", name);
	}
	if(fstat(handle, &fileinfo) == -1)
	{
		I_Error("Couldn't read file %s", name);
	}
	length = fileinfo.st_size;
	if(mallocType == MALLOC_ZONE)
	{ // Use zone memory allocation
		buf = Z_Malloc(length, PU_STATIC, NULL);
	}
	else
	{ // Use c library memory allocation
		buf = ds_malloc(length); //rww malloc->ds_malloc
		if(buf == NULL)
		{
			I_Error("Couldn't malloc buffer %d for file %s.",
				length, name);
		}
	}
	count = read(handle, buf, length);
	close(handle);
	if(count < length)
	{
		I_Error("Couldn't read file %s", name);
	}
	*buffer = buf;
	return length;
#else
	return -1;
#endif //rww end
// AREIS END
}
Example #4
0
/* tries to register current frame onto previous frame.
 *   Algorithm:
 *   discards fields with low contrast
 *   select maxfields fields according to their contrast
 *   check theses fields for vertical and horizontal transformation
 *   use minimal difference of all possible positions
 *   calculate shift as cleaned mean of all remaining fields
 *   calculate rotation angle of each field in respect to center of fields
 *   after shift removal
 *   calculate rotation angle as cleaned mean of all angles
 *   compensate for possibly off-center rotation
 */
Transform calcTransFields(MotionDetect* md, calcFieldTransFunc fieldfunc,
                         contrastSubImgFunc contrastfunc) {
  Transform* ts = (Transform*) ds_malloc(sizeof(Transform) * md->fieldNum);
  Field** fs = (Field**) ds_malloc(sizeof(Field*) * md->fieldNum);
  double *angles = (double*) ds_malloc(sizeof(double) * md->fieldNum);
  int i, index = 0, num_trans;
  Transform t;
#ifdef STABVERBOSE
  FILE *file = NULL;
  char buffer[32];
  ds_snprintf(buffer, sizeof(buffer), "k%04i.dat", md->frameNum);
  file = fopen(buffer, "w");
  fprintf(file, "# plot \"%s\" w l, \"\" every 2:1:0\n", buffer);
#endif

  DSVector goodflds = selectfields(md, contrastfunc);
  // use all "good" fields and calculate optimal match to previous frame
#ifdef USE_OMP
#pragma omp parallel for shared(goodflds, md, ts, fs) // does not bring speedup
#endif
  for(index=0; index < ds_vector_size(&goodflds); index++){
    int i = ((contrast_idx*)ds_vector_get(&goodflds,index))->index;

    t = fieldfunc(md, &md->fields[i], i); // e.g. calcFieldTransYUV
#ifdef STABVERBOSE
    fprintf(file, "%i %i\n%f %f %i\n \n\n", md->fields[i].x, md->fields[i].y,
        md->fields[i].x + t.x, md->fields[i].y + t.y, t.extra);
#endif
    if (t.extra != -1) { // ignore if extra == -1 (unused at the moment)
      ts[index] = t;
      fs[index] = md->fields + i;
    }
  }

  t = null_transform();
  num_trans = ds_vector_size(&goodflds); // amount of transforms we actually have
  ds_vector_del(&goodflds);
  if (num_trans < 1) {
    ds_log_warn(md->modName, "too low contrast! No field remains.\n"
                             "(no translations are detected in frame %i)", md->frameNum);
    return t;
  }

  int center_x = 0;
  int center_y = 0;
  // calc center point of all remaining fields
  for (i = 0; i < num_trans; i++) {
    center_x += fs[i]->x;
    center_y += fs[i]->y;
  }
  center_x /= num_trans;
  center_y /= num_trans;

  if (md->show) { // draw fields and transforms into frame.
    // this has to be done one after another to handle possible overlap
    if (md->show > 1) {
      for (i = 0; i < num_trans; i++)
        drawFieldScanArea(md, fs[i], &ts[i]);
    }
    for (i = 0; i < num_trans; i++)
      drawField(md, fs[i], &ts[i]);
    for (i = 0; i < num_trans; i++)
      drawFieldTrans(md, fs[i], &ts[i]);
  }
  /* median over all transforms
     t= median_xy_transform(ts, md->field_num);*/
  // cleaned mean
  t = cleanmean_xy_transform(ts, num_trans);

  // substract avg
  for (i = 0; i < num_trans; i++) {
    ts[i] = sub_transforms(&ts[i], &t);
  }
  // figure out angle
  if (md->fieldNum < 6) {
    // the angle calculation is inaccurate for 5 and less fields
    t.alpha = 0;
  } else {
    for (i = 0; i < num_trans; i++) {
      angles[i] = calcAngle(md, fs[i], &ts[i], center_x, center_y);
    }
    double min, max;
    t.alpha = -cleanmean(angles, num_trans, &min, &max);
    if (max - min > md->maxAngleVariation) {
      t.alpha = 0;
      ds_log_info(md->modName, "too large variation in angle(%f)\n",
                  max-min);
    }
  }
  // compensate for off-center rotation
  double p_x = (center_x - md->fi.width / 2);
  double p_y = (center_y - md->fi.height / 2);
  t.x += (cos(t.alpha) - 1) * p_x - sin(t.alpha) * p_y;
  t.y += sin(t.alpha) * p_x + (cos(t.alpha) - 1) * p_y;

#ifdef STABVERBOSE
  fclose(file);
#endif
  return t;
}
Example #5
0
/* select only the best 'maxfields' fields
   first calc contrasts then select from each part of the
   frame a some fields
*/
DSVector selectfields(MotionDetect* md, contrastSubImgFunc contrastfunc) {
  int i, j;
  DSVector goodflds;
  contrast_idx *ci =
    (contrast_idx*) ds_malloc(sizeof(contrast_idx) * md->fieldNum);
  ds_vector_init(&goodflds, md->fieldNum);

  // we split all fields into row+1 segments and take from each segment
  // the best fields
  int numsegms = (md->fieldRows + 1);
  int segmlen = md->fieldNum / (md->fieldRows + 1) + 1;
  // split the frame list into rows+1 segments
  contrast_idx *ci_segms =
    (contrast_idx*) ds_malloc(sizeof(contrast_idx) * md->fieldNum);
  int remaining = 0;
  // calculate contrast for each field
  // #pragma omp parallel for shared(ci,md) no speedup because to short
  for (i = 0; i < md->fieldNum; i++) {
    ci[i].contrast = contrastfunc(md, &md->fields[i]);
    ci[i].index = i;
    if (ci[i].contrast < md->contrastThreshold)
      ci[i].contrast = 0;
    // else printf("%i %lf\n", ci[i].index, ci[i].contrast);
  }

  memcpy(ci_segms, ci, sizeof(contrast_idx) * md->fieldNum);
  // get best fields from each segment
  for (i = 0; i < numsegms; i++) {
    int startindex = segmlen * i;
    int endindex = segmlen * (i + 1);
    endindex = endindex > md->fieldNum ? md->fieldNum : endindex;
    //printf("Segment: %i: %i-%i\n", i, startindex, endindex);

    // sort within segment
    qsort(ci_segms + startindex, endindex - startindex,
      sizeof(contrast_idx), cmp_contrast_idx);
    // take maxfields/numsegms
    for (j = 0; j < md->maxFields / numsegms; j++) {
      if (startindex + j >= endindex)
        continue;
      // printf("%i %lf\n", ci_segms[startindex+j].index,
      //                    ci_segms[startindex+j].contrast);
      if (ci_segms[startindex + j].contrast > 0) {
        ds_vector_append_dup(&goodflds, &ci[ci_segms[startindex+j].index],
                           sizeof(contrast_idx));
        // don't consider them in the later selection process
        ci_segms[startindex + j].contrast = 0;
      }
    }
  }
  // check whether enough fields are selected
  // printf("Phase2: %i\n", ds_list_size(goodflds));
  remaining = md->maxFields - ds_vector_size(&goodflds);
  if (remaining > 0) {
    // take the remaining from the leftovers
    qsort(ci_segms, md->fieldNum, sizeof(contrast_idx), cmp_contrast_idx);
    for (j = 0; j < remaining; j++) {
      if (ci_segms[j].contrast > 0) {
        ds_vector_append_dup(&goodflds, &ci_segms[j], sizeof(contrast_idx));
      }
    }
  }
  // printf("Ende: %i\n", ds_list_size(goodflds));
  ds_free(ci);
  ds_free(ci_segms);
  return goodflds;
}
Example #6
0
void M_LoadDefaults(char *fileName)
{
	int i;
	int len;
	FILE *f;
	char def[80];
	char strparm[100];
	char *newstring;
	int parm;
	boolean isstring;

	// Set everything to base values
	numdefaults = sizeof(defaults)/sizeof(defaults[0]);
	for(i = 0; i < numdefaults; i++)
	{
		*defaults[i].location = defaults[i].defaultvalue;
	}

	// Check for a custom config file
	i = M_CheckParm("-config");
	if(i && i < myargc-1)
	{
		strcpy(defaultfile, myargv[i+1]);
		ST_Message("config file: %s\n", defaultfile);
	}
	else if(cdrom)
	{
		sprintf(defaultfile, "c:\\hexndata\\%s", fileName);
	}
	else
	{
		strcpy(defaultfile, fileName);
	}

#ifndef _HEXENDS //rww begin FIXME - use sram
	// Scan the config file
	f = fopen(defaultfile, "r");
	if(f)
	{
		while(!feof(f))
		{
			isstring = false;
			if(fscanf(f, "%79s %[^\n]\n", def, strparm) == 2)
			{
				if(strparm[0] == '"')
				{
					 // Get a string default
					 isstring = true;
					 len = strlen(strparm);
					 newstring = (char *)ds_malloc(len); //rww malloc->ds_malloc
					 if (newstring == NULL) I_Error("can't malloc newstring");
					 strparm[len-1] = 0;
					 strcpy(newstring, strparm+1);
				}
				else if(strparm[0] == '0' && strparm[1] == 'x')
				{
					sscanf(strparm+2, "%x", &parm);
				}
				else
				{
					sscanf(strparm, "%i", &parm);
				}
				for(i = 0; i < numdefaults; i++)
				{
					if(!strcmp(def, defaults[i].name))
					{
						if(!isstring)
						{
							*defaults[i].location = parm;
						}
						else
						{
							*defaults[i].location = (int)newstring;
						}
						break;
					}
				}
			}
		}

		fclose (f);
	}

#ifdef __WATCOMC__
	// Translate the key scancodes
	for(i = 0; i < numdefaults; i++)
	{
		if(defaults[i].scantranslate)
		{
			parm = *defaults[i].location;
			defaults[i].untranslated = parm;
			*defaults[i].location = scantokey[parm];
		}
	}
#endif

#endif //rww end
}
Example #7
0
void M_FindResponseFile(void)
{
	int i;

	for(i = 1; i < myargc; i++)
	{
#ifndef _HEXENDS //rww begin
		if(myargv[i][0] == '@')
		{
			FILE *handle;
			int size;
			int k;
			int index;
			int indexinfile;
			char *infile;
			char *file;
			char *moreargs[20];
			char *firstargv;

			// READ THE RESPONSE FILE INTO MEMORY
			handle = fopen(&myargv[i][1], "rb");
			if(!handle)
			{

				HEXENPRINTF("\nNo such response file!");
				exit(1);
			}
			ST_Message("Found response file %s!\n",&myargv[i][1]);
			fseek (handle,0,SEEK_END);
			size = ftell(handle);
			fseek (handle,0,SEEK_SET);
			file = ds_malloc (size); //rww malloc->ds_malloc
			fread (file,size,1,handle);
			fclose (handle);

			// KEEP ALL CMDLINE ARGS FOLLOWING @RESPONSEFILE ARG
			for (index = 0,k = i+1; k < myargc; k++)
				moreargs[index++] = myargv[k];
			
			firstargv = myargv[0];
			myargv = ds_malloc(sizeof(char *)*MAXARGVS); //rww malloc->ds_malloc
			memset(myargv,0,sizeof(char *)*MAXARGVS);
			myargv[0] = firstargv;
			
			infile = file;
			indexinfile = k = 0;
			indexinfile++;  // SKIP PAST ARGV[0] (KEEP IT)
			do
			{
				myargv[indexinfile++] = infile+k;
				while(k < size &&  

					((*(infile+k)>= ' '+1) && (*(infile+k)<='z')))
					k++;
				*(infile+k) = 0;
				while(k < size &&
					((*(infile+k)<= ' ') || (*(infile+k)>'z')))
					k++;
			} while(k < size);
			
			for (k = 0;k < index;k++)
				myargv[indexinfile++] = moreargs[k];
			myargc = indexinfile;
			// DISPLAY ARGS
			if(M_CheckParm("-debug"))
			{
				ST_Message("%d command-line args:\n", myargc);
				for(k = 1; k < myargc; k++)
				{
					ST_Message("%s\n", myargv[k]);
				}
			}
			break;
		}
#endif //rww end
	}
}