e576bb2002-10-11Martin Nilsson /* || This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information. */
a367b51999-04-12Per Hedbor #include "global.h" #include <math.h> #include <ctype.h> #include "stralloc.h" #include "object.h"
4dc1d11999-09-25Henrik Grubbström (Grubba) #include "mapping.h"
a367b51999-04-12Per Hedbor #include "interpret.h"
b2d3e42000-12-01Fredrik Hübinette (Hubbe) #include "pike_error.h"
8db2c31999-05-23Mirar (Pontus Hagland) #include "builtin_functions.h"
a367b51999-04-12Per Hedbor  #include "image.h" #include "module_support.h"
6dc2772000-07-28Fredrik Hübinette (Hubbe) 
a367b51999-04-12Per Hedbor extern struct program *image_program; /* **! module Image **! submodule HRZ **!
d8edae2014-07-31Per Hedbor **! HRZ is rather trivial, and not really useful, but: **! **! The HRZ file is always 256x240 with RGB values from 0 to 63. **! No compression, no header, just the raw RGB data.
ab697a2014-08-02Henrik Grubbström (Grubba) **! HRZ is (was?) used for amateur radio slow-scan TV.
a367b51999-04-12Per Hedbor */ /* **! method object decode(string data) **! method mapping _decode(string data) **! method string encode(object image) **! **! Handle encoding and decoding of HRZ images. */ void image_hrz_f_decode(INT32 args) { struct object *io; struct pike_string *s; int c;
391ac52018-08-05Martin Nilsson  get_all_args( NULL, args, "%S", &s);
13670c2015-05-25Martin Nilsson 
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  if(s->len != 256*240*3) Pike_error("This is not a HRZ file\n");
a367b51999-04-12Per Hedbor  push_int( 256 ); push_int( 240 ); io = clone_object( image_program, 2); for(c=0; c<256*240; c++) { rgb_group pix; pix.r = s->str[c*3]<<2 | s->str[c*3]>>4;
98de1f1999-05-24Mirar (Pontus Hagland)  pix.g = s->str[c*3+1]<<2 | s->str[c*3+1]>>4; pix.b = s->str[c*3+2]<<2 | s->str[c*3+2]>>4;
a367b51999-04-12Per Hedbor  ((struct image *)io->storage)->img[c] = pix; } pop_n_elems(args); push_object( io ); } void image_hrz_f__decode(INT32 args) { image_hrz_f_decode(args);
5e9fc02015-08-18Per Hedbor  push_static_text("image");
a367b51999-04-12Per Hedbor  stack_swap(); f_aggregate_mapping(2); } void image_hrz_f_encode(INT32 args ) { struct object *io; struct image *i; struct pike_string *s; int x,y;
391ac52018-08-05Martin Nilsson  get_all_args( NULL, args, "%o", &io);
13670c2015-05-25Martin Nilsson 
13b5ed2014-05-26Per Hedbor  if(!(i = get_storage( io, image_program)))
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Wrong argument 1 to Image.HRZ.encode\n");
13670c2015-05-25Martin Nilsson 
a367b51999-04-12Per Hedbor  s = begin_shared_string( 256*240*3 );
13670c2015-05-25Martin Nilsson 
21b12a2014-09-03Martin Nilsson  memset(s->str, 0, s->len );
a367b51999-04-12Per Hedbor  for(y=0; y<240; y++) if(y < i->ysize) for(x=0; x<256; x++) if(x < i->xsize) { int in = (x + y*256)*3; rgb_group pix = i->img[y*i->xsize+x]; s->str[in+0] = pix.r >> 2; s->str[in+1] = pix.g >> 2; s->str[in+2] = pix.b >> 2; } pop_n_elems(args); push_string( end_shared_string(s) ); }
c9eefb2014-08-21Martin Nilsson void init_image_hrz(void)
a367b51999-04-12Per Hedbor {
226ec82005-01-23Martin Nilsson  ADD_FUNCTION( "decode", image_hrz_f_decode, tFunc(tStr,tObj), 0); ADD_FUNCTION( "_decode", image_hrz_f__decode, tFunc(tStr,tMapping), 0); ADD_FUNCTION( "encode", image_hrz_f_encode, tFunc(tObj,tStr), 0);
a367b51999-04-12Per Hedbor }
c9eefb2014-08-21Martin Nilsson void exit_image_hrz(void)
a367b51999-04-12Per Hedbor { }