Light control script

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
Post Reply
oskin
Posts: 84
Joined: Tue May 25, 2004 7:03 pm
Location: Moscow, Russia
Contact:

Light control script

Post by oskin » Thu Aug 26, 2004 1:37 pm

I use this script for control of infra-red highlighting at insufficient illuminance.
A principle of operation: the script periodically receives from ZoneMinder a current frame, calculates average value of brightness of all pixels and depending on result switches on or off an intra-red highlighting.
Howto install:
- save texts of programs in appropriate files (see comments in the beginning)
- compile jpgbright.c the command: gcc -O2 -s -o jpgbright -ljpeg jpgbright.c
- add in crontab a line similar to these: */5 5-10,16-22 * * * /usr/local/zm/opt/lightctl
- in ZoneMinder add the special user for dialogue of a script with ZoneMinder, no access rights are necessary for this user for version 1.19.4. :)
- in the beginning of a file lightctl correct value of variables for your variant.
For me for handle of highlighting it is used OneWire device if for you differently - correct a code of functions light_on and light_off. :)

Code: Select all

#!/bin/sh
# File lightctl

ON_VAL=100
OFF_VAL=125
ZMU=/usr/local/zm/bin/zmu
ZMUSER=irlight
ZMPASS=irlight
MONITORID=1
JPGBRIGHT=/usr/local/zm/opt/jpgbright
STAT=/var/tmp/irlight
 
light_on () {
        echo 1 >/mnt/1wire/12.264329000000/PIO.A
}
light_off () {
        echo 0 >/mnt/1wire/12.264329000000/PIO.A
}
 
cd /var/tmp
if MONNAME=`$ZMU -m $MONITORID -U $ZMUSER -P $ZMPASS -qi | grep '^Name :' | sed -e 's/^Name : //'`; then
        newbr=`$JPGBRIGHT $MONNAME.jpg`
        rm -f $MONNAME.jpg
fi
[ "$newbr" = "" ] && exit 1
currdate=`date '+%Y%m%d%H%M%S'`
[ -f $STAT ] || echo "0 0 $currdate" >$STAT
lastst=`cat $STAT | cut -d' ' -f 1`
[ "$lastst" = "" ] && lastst=0
newst=$lastst
lastbr=`cat $STAT | cut -d' ' -f 2`
[ "$lastbr" = "" ] && lastbr=0
if [ $lastst = 0 ]; then
        if [ $newbr -le $ON_VAL ]; then
                newst=1
                light_on
        fi
else
        if [ $newbr -ge $OFF_VAL ]; then
                newst=0
                light_off
        fi
fi
echo "$newst $newbr $currdate" >$STAT
exit 0

Code: Select all

/* File jpgbright.c */
#include <stdio.h>
#include "jpeglib.h"
#include <setjmp.h>
                                                                                                                        
struct my_error_mgr {
  struct jpeg_error_mgr pub;    /* "public" fields */
                                                                                                                        
  jmp_buf setjmp_buffer;        /* for return to caller */
};
                                                                                                                        
typedef struct my_error_mgr * my_error_ptr;
                                                                                                                        
METHODDEF(void)
my_error_exit (j_common_ptr cinfo)
{
  my_error_ptr myerr = (my_error_ptr) cinfo->err;
                                                                                                                        
  (*cinfo->err->output_message) (cinfo);
                                                                                                                        
  longjmp(myerr->setjmp_buffer, 1);
}
                                                                                                                        
GLOBAL(int)
read_JPEG_file (char * filename)
{
  struct jpeg_decompress_struct cinfo;
  struct my_error_mgr jerr;
  FILE * infile;                /* source file */
  JSAMPARRAY buffer;            /* Output row buffer */
  int row_stride;               /* physical row width in output buffer */
  int p;
  long s;
                                                                                                                        
  if ((infile = fopen(filename, "rb")) == NULL) {
    fprintf(stderr, "can't open %s\n", filename);
    return 0;
  }
                                                                                                                        
  cinfo.err = jpeg_std_error(&jerr.pub);
  jerr.pub.error_exit = my_error_exit;
  if (setjmp(jerr.setjmp_buffer)) {
    jpeg_destroy_decompress(&cinfo);
    fclose(infile);
    return 0;
  }
  jpeg_create_decompress(&cinfo);
                                                                                                                        
  jpeg_stdio_src(&cinfo, infile);
                                                                                                                        
  (void) jpeg_read_header(&cinfo, TRUE);

  (void) jpeg_start_decompress(&cinfo);
  row_stride = cinfo.output_width * cinfo.output_components;
  buffer = (*cinfo.mem->alloc_sarray)
                ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
                                                                                                                        
  s = 0;
  while (cinfo.output_scanline < cinfo.output_height) {
    (void) jpeg_read_scanlines(&cinfo, buffer, 1);
      for (p=0;p<row_stride;p+=3) {
        s += buffer[0][p] + buffer[0][p+1] + buffer[0][p+2];
      }
  }
  printf("%i\n", s / cinfo.output_height / row_stride);
                                                                                                                        
  (void) jpeg_finish_decompress(&cinfo);
  jpeg_destroy_decompress(&cinfo);
  fclose(infile);
  return 1;
}
                                                                                                                        
int main(int ac, char **av) {
  if (ac != 2) {
    fprintf(stderr, "Usage: %s file.jpg\n", av[0]);
    exit(1);
  }
  if (!read_JPEG_file(av[1])) exit(1);
  exit(0);
}

oskin
Posts: 84
Joined: Tue May 25, 2004 7:03 pm
Location: Moscow, Russia
Contact:

Post by oskin » Mon Jan 24, 2005 8:11 am

For zm-1.20.0 "special user" should have the following rights:
Stream - View
Monitor - View

Christo
Posts: 89
Joined: Wed Feb 01, 2012 9:48 pm

Re: Light control script

Post by Christo » Wed Feb 01, 2012 9:59 pm

Hello Oskin,

I have tried to compile the jpgbright.c file, but getting loads of errrors.

I am not familiar with programming, so please help me out!!

This is what i get as errors after compiling as described:
jpgbright.c: In function âread_JPEG_fileâ:
jpgbright.c:66: warning: format â%iâ expects type âintâ, but argument 2 has type âlong unsigned intâ
jpgbright.c: In function âmainâ:
jpgbright.c:77: warning: incompatible implicit declaration of built-in function âexitâ
jpgbright.c:79: warning: incompatible implicit declaration of built-in function âexitâ
/tmp/cceLSf0c.o: In function `read_JPEG_file':
jpgbright.c:(.text+0xad): undefined reference to `jpeg_std_error'
jpgbright.c:(.text+0xe2): undefined reference to `jpeg_destroy_decompress'
jpgbright.c:(.text+0x118): undefined reference to `jpeg_CreateDecompress'
jpgbright.c:(.text+0x130): undefined reference to `jpeg_stdio_src'
jpgbright.c:(.text+0x146): undefined reference to `jpeg_read_header'
jpgbright.c:(.text+0x154): undefined reference to `jpeg_start_decompress'
jpgbright.c:(.text+0x1cc): undefined reference to `jpeg_read_scanlines'
jpgbright.c:(.text+0x2ab): undefined reference to `jpeg_finish_decompress'
jpgbright.c:(.text+0x2b9): undefined reference to `jpeg_destroy_decompress'
collect2: ld returned 1 exit status
root@ubuntu:/usr/lib#

Kind regard and thanks in advance, Christo

Jolaf
Posts: 14
Joined: Mon May 02, 2005 10:46 pm
Location: Bordeaux (france)

Re: Light control script

Post by Jolaf » Thu Feb 09, 2012 6:40 pm

hi christo, I finally found the post !

The errors are due to the fact that you don't link the jpeg library with your binary.
First you also must have a "libjpeg-dev" package installed.
I used libjpeg8-dev

I used synaptic on my ubuntu, but the command line to install should be something like
sudo apt-get install libjpeg8-dev

Then, you can compile usint this line :
gcc jpgbright.c -ljpeg -o jpgbright

I stil get 3 information messages
But I'll let you test if this works.

Jolaf'

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests