Example #1
0
static mp_obj_t py_image_save(uint n_args, const mp_obj_t *args, mp_map_t *kw_args)
{
    int res;
    image_t *image = py_image_cobj(args[0]);
    const char *path = mp_obj_str_get_str(args[1]);

    mp_map_elem_t *kw_subimage = mp_map_lookup(kw_args, MP_OBJ_NEW_QSTR(qstr_from_str("subimage")), MP_MAP_LOOKUP);
    if (kw_subimage != NULL) {
        mp_obj_t *array;
        mp_obj_get_array_fixed_n(kw_subimage->value, 4, &array);

        rectangle_t r = {
            mp_obj_get_int(array[0]),
            mp_obj_get_int(array[1]),
            mp_obj_get_int(array[2]),
            mp_obj_get_int(array[3]),
        };

        res = imlib_save_image(image, path, &r);
    } else {
        res = imlib_save_image(image, path, NULL);
    }

    if (res != FR_OK) {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, ffs_strerror(res)));
    }

    return mp_const_true;
}
Example #2
0
int
main(int argc, char **argv)
{
    char               *dot, *colon, *n, *oldn;
    Imlib_Image         im;

    /* I'm just plain being lazy here.. get our basename. */
    for (oldn = n = argv[0]; n; oldn = n)
        n = strchr(++oldn, '/');
    if (argc < 3 || !strcmp(argv[1], "-h"))
        usage(-1);
    if (!(im = imlib_load_image(argv[1])))
    {
        fprintf(stderr, PROG_NAME ": Error loading image: %s\n", argv[1]);
        exit(-1);
    }

    /* we only care what format the export format is. */
    imlib_context_set_image(im);
    /* hopefully the last one will be the one we want.. */
    dot = strrchr(argv[2], '.');
    /* if there's a format, snarf it and set the format. */
    if (dot && *(dot + 1))
    {
        colon = strrchr(++dot, ':');
        /* if a db file with a key, export it to a db. */
        if (colon && *(colon + 1))
        {
            *colon = 0;
            /* beats having to look for strcasecmp() */
            if (!strncmp(dot, "db", 2) || !strncmp(dot, "dB", 2) ||
                    !strncmp(dot, "DB", 2) || !strncmp(dot, "Db", 2))
            {
                imlib_image_set_format("db");
            }
            *colon = ':';
        }
        else
        {
            char               *p, *q;

            /* max length of 8 for format name. seems reasonable. */
            q = p = malloc(9);
            memset(p, 0, 8);
            strncpy(p, dot, (strlen(dot) < 9) ? strlen(dot) : 8);
            /* Imlib2 only recognizes lowercase formats. convert it. */
            for (q[8] = 0; *q; q++)
                *q = tolower(*q);
            imlib_image_set_format(p);
            free(p);
        }
        dot--;
    }
    else
        imlib_image_set_format("jpg");

    imlib_save_image(argv[2]);

    return 0;
}
Example #3
0
void saveImage(image * img, const char *filename)
{
    Imlib_Image out;
    int i, j;


    out = imlib_create_image(img->width, img->height);
    imlib_context_set_image(out);


    for (i = 0; i < img->width; i++) {
	for (j = 0; j < img->height; j++) {
	    int red = (int) img->red->vals[i][j];
	    int green = (int) img->green->vals[i][j];
	    int blue = (int) img->blue->vals[i][j];


	    red = red <= 255 ? red : 255;
	    green = green <= 255 ? green : 255;
	    blue = blue <= 255 ? blue : 255;


	    imlib_context_set_color(red, green, blue, 255);
	    imlib_image_draw_line(i, j, i, j, 0);
	}
    }


    imlib_save_image(filename);
    imlib_free_image();
}
Example #4
0
void
tinto_take_snapshot(const char *path) {
  Panel* panel = &panel1[0];

  if (panel->area.bounds.width > server.monitor[0].width)
    panel->area.bounds.width = server.monitor[0].width;

  panel->temp_pmap = XCreatePixmap (server.dsp, server.root_win,
				    panel->area.bounds.width,
				    panel->area.bounds.height,
				    server.depth);
  rendering (panel);

  imlib_context_set_drawable (panel->temp_pmap);
  Imlib_Image img =
    imlib_create_image_from_drawable (None, 0, 0,
				      panel->area.bounds.width,
				      panel->area.bounds.height, 0);

  imlib_context_set_image (img);
  if (!panel_horizontal) {
    imlib_image_flip_horizontal ();
    imlib_image_flip_diagonal ();
  }
  imlib_save_image (path);
  imlib_free_image ();
}
Example #5
0
File: image.c Project: c-aries/swc
static void
save_image(char *filename)
{
  Imlib_Image img;
  int width = image.width;
  int height = image.height;
  DATA32 *data;
  char *suffix;

  if (filename == NULL) {
    return;
  }
  suffix = strrchr(filename, '.');
  if (suffix) {
    data = (DATA32 *)image.pixels;
    img = imlib_create_image_using_data(width, height, data);
    if (img) {
      imlib_context_set_image(img);
      imlib_image_set_format(suffix + 1);
      imlib_save_image(filename);
      imlib_free_image();
    }
  }
  else {
    fprintf(stderr, "wrong file name: %s\n", filename);
    exit(EXIT_FAILURE);
  }
}
Example #6
0
int main(int argc, char** argv) {
    struct stat sb;
    if (!(stat(OUTPUT_DIR, &sb) == 0 && S_ISDIR(sb.st_mode)))
    {
        if (mkdir(OUTPUT_DIR, S_IRWXU | S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH) != 0)
        {
            printf("Error creating directory %s\n",OUTPUT_DIR);
            return -1;
        }
    }
    char tempFileName[512];
    sprintf(tempFileName, "%stest.txt",OUTPUT_DIR);
    FILE* tempFile = fopen(tempFileName, "w+");
    if (tempFile == NULL)
    {
        printf("Failed to create file in current folder.  Please check permissions.\n");
        return -1;
    }
    fclose(tempFile);
    remove(tempFileName);
    //if we get here, we know the directory exists and we can write to it
    //now set up, start, and grab image from camera
    fc2Context context;
    fc2PGRGuid guid;
    fc2Image raw_image, converted_image;
    Imlib_Image temp_image;
    check_point_grey(fc2CreateContext(&context));
    check_point_grey(fc2GetCameraFromIndex(context,0,&guid));
    check_point_grey(fc2Connect(context,&guid));
    check_point_grey(fc2SetDefaultColorProcessing(FC2_IPP));
    check_point_grey(fc2SetVideoModeAndFrameRate(context,
                     FC2_VIDEOMODE_1280x960Y8,
                     FC2_FRAMERATE_15));
    PrintCameraInfo(context);
    check_point_grey(fc2CreateImage(&raw_image));
    check_point_grey(fc2CreateImage(&converted_image));
    check_point_grey(fc2StartCapture(context));
    check_point_grey(fc2RetrieveBuffer(context,&raw_image));
    check_point_grey(fc2ConvertImageTo(FC2_PIXEL_FORMAT_BGRU,&raw_image,&converted_image));
    temp_image = imlib_create_image_using_copied_data(converted_image.cols,
                 converted_image.rows,
                 (unsigned int *)
                 converted_image.pData);
    imlib_context_set_image(temp_image);
    char filename[512];
    sprintf(filename,"%stest.ppm",OUTPUT_DIR);
    imlib_save_image(filename);
    printf("Saved %s\n",filename);
    //image saved, now clean up
    imlib_free_image_and_decache();
    check_point_grey(fc2StopCapture(context));
    check_point_grey(fc2DestroyContext(context));
    check_point_grey(fc2DestroyImage(&raw_image));
    check_point_grey(fc2DestroyImage(&converted_image));
    printf("finished cleanup\n");
    return 0;
}
Example #7
0
void SaveImlibImage(Imlib_Image temp_image, char *name, char *mode_str)
{
  imlib_context_set_image(temp_image);
  char filename[512];
  sprintf(filename,"%snewtest_%sfinal.ppm",OUTPUT_DIR,name);
  imlib_save_image(filename);
  printf("Saved %s\n",filename);
  //image saved, now clean up
  imlib_free_image_and_decache();
}
Example #8
0
void image_save(struct image *img, const char *name)
{
#if defined(HAVE_SDL_IMAGE_H)
    SDL_SaveBMP(img->priv, name);
#elif defined(HAVE_IMLIB2_H)
    imlib_context_set_image(img->priv);
    imlib_save_image(name);
#elif defined(HAVE_CV_H)
    cvSaveImage(name, img->priv);
#endif
}
Example #9
0
void
gib_imlib_save_image(Imlib_Image im, char *file)
{
   char *tmp;

   imlib_context_set_image(im);
   tmp = strrchr(file, '.');
   if (tmp)
   {
     char *p, *pp;
     p = gib_estrdup(tmp + 1);
     pp = p;
     while(*pp) {
       *pp = tolower(*pp);
       pp++;
     }
     imlib_image_set_format(p);
     gib_efree(p);
   }
   imlib_save_image(file);
}
Example #10
0
/* main program */
int main(int argc, char **argv)
{
	/* an image handle */
	Imlib_Image image;

	/* if we provided < 2 arguments after the command - exit */
	if (argc != 3) exit(1);
	/* load the image */
	image = imlib_load_image(argv[1]);
	/* if the load was successful */
	if (image)
	{
		char *tmp;
		/* set the image we loaded as the current context image to work on */
		imlib_context_set_image(image);
		/* set the image format to be the format of the extension of our last */
		/* argument - i.e. .png = png, .tif = tiff etc. */
		tmp = strrchr(argv[2], '.');
		if(tmp)
			imlib_image_set_format(tmp + 1);
		/* save the image */
		imlib_save_image(argv[2]);
	}
}
Example #11
0
/*
Dual wallpaper generator
Copyright (C) 2009 Aleksi Räsänen <*****@*****.**>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
int 
main( int argc, char *argv[] )
{
  Imlib_Image input_image;
  Imlib_Image output_image;

  int image1_width, image1_height, image2_width, image2_height;
  int input_width, input_height, output_width;
  
  if( argc < 7 )  
    {
      printf( "Usage: %s inputfile img1_width img1_height ", argv[0] );
      printf( "img2_width img2_height output.jpg\n" ); 
      exit(1);
    }

  // Check if input file exists or not. If not, quit.
  FILE *fp;
  fp = fopen( argv[1], "r" );

  if( fp == NULL ) 
    {
      printf( "File %s not found!\n", argv[1] );
      exit(1);
    }

  // Read output image dimensions
  image1_width = atoi( argv[2] );
  image1_height = atoi( argv[3] );
  image2_width = atoi( argv[4] );
  image2_height = atoi( argv[5] );

  input_image = imlib_load_image( argv[1] );

  // Output image width must be selected by wider image
  if( image1_width > image2_width )
    output_width = image1_width;
  else
    output_width = image2_width;

  // Create new empty image
  output_image = imlib_create_image( output_width,
				     image1_height + image2_height );

  if(! input_image )
    {
      printf( "Failed to load image %s\n", argv[1] );
      exit(1);
    }

  // Read original image size
  imlib_context_set_image( input_image );
  input_width = imlib_image_get_width();
  input_height = imlib_image_get_height();

  imlib_context_set_image( output_image );

  // Copy whole input image to output image
  imlib_blend_image_onto_image( input_image, 0, 0, 0, 
				input_width, input_height, 
				0, 0,
				image1_width, image1_height );

  imlib_blend_image_onto_image( input_image, 0, 0, 0,
				input_width, input_height,
				0, image1_height,
				image2_width, image2_height );

  imlib_save_image( argv[6] );
 
  return 0;
}
Example #12
0
int main (int argc, char **argv)
{
	Visual *vis;
	Colormap cm;
	Display *_display;
	Imlib_Context context;
	Imlib_Image image;
	Pixmap pixmap;
	Imlib_Color_Modifier modifier = NULL;
	_display = XOpenDisplay (NULL);
	int width, height, depth, i, alpha;

	
	char str1[40];
	char str2[40];
	char str3[40];
	char str4[40];
	char str5[40];
		
	int ck0;
	int w, h;
	w = 0;
	h = 0;
			
			
	char strA1[30] = "hwe";
	char strA2[30] = "hwer";
	const char jpg[15] = "jpg"; //1
	const char png[15] = "png"; //2
	
	char *A1; 
	char *A2; 
	
	strcpy(strA1, argv[argc-1]);
	strcpy(strA2, strA1);
	A1 = strstr(strA1, jpg);
	A2 = strstr(strA2, png);
	
		//check to be sure image format is written right or abort
	 	checkForNull(A1, A2);
		
		
			
		
	for (screen = 0; screen < ScreenCount (_display); screen++)
	{
		display = XOpenDisplay (NULL);

		context = imlib_context_new ();
		imlib_context_push (context);

		imlib_context_set_display (display);
		vis = DefaultVisual (display, screen);
		cm = DefaultColormap (display, screen);

		width = DisplayWidth (display, screen);
		height = DisplayHeight (display, screen);

		depth = DefaultDepth (display, screen);

		pixmap =
			XCreatePixmap (display, RootWindow (display, screen),
							width, height, depth);

		imlib_context_set_visual (vis);
		imlib_context_set_colormap (cm);
		imlib_context_set_drawable (pixmap);
		imlib_context_set_color_range (imlib_create_color_range ());
		
		image = imlib_create_image (width, height);
		imlib_context_set_image (image);
				printf("1\n");
		imlib_context_set_color (0, 0, 0, 255);
		imlib_image_fill_rectangle (0, 0, width, height);

		imlib_context_set_dither (1);
		imlib_context_set_blend (1);
		printf("2\n");
		alpha = 255;


	for (i = 1; i < argc; i++)
	{
		if (modifier != NULL)
		{
			imlib_apply_color_modifier ();
			imlib_free_color_modifier ();
		}

	modifier = imlib_create_color_modifier ();
	imlib_context_set_color_modifier (modifier);

		if (strcmp (argv[i], "-alpha") == 0)
		{
			if ((++i) >= argc)
			{
				fprintf (stderr, "Missing alpha\n");
				continue;
			}
				if (sscanf (argv[i], "%i", &alpha) == 0)
				{
					fprintf (stderr, "Bad alpha (%s)\n", argv[i]);
					continue;
				}
		}
	else if (strcmp (argv[i], "-solid") == 0)
	{
		Color c;
		
		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing color\n");
			continue;
		}
			if (parse_color (argv[i], &c, alpha) == 1)
			{
				fprintf (stderr, "Bad color (%s)\n", argv[i]);
				continue;
			}

		imlib_context_set_color (c.r, c.g, c.b, c.a);
		imlib_image_fill_rectangle (0, 0, width, height);
	}
		else if (strcmp (argv[i], "-clear") == 0)
		{
			imlib_free_color_range ();
			imlib_context_set_color_range (imlib_create_color_range ());
		}
	else if (strcmp (argv[i], "-add") == 0)
	{
		Color c;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing color\n");
			continue;
		}
			if (parse_color (argv[i], &c, alpha) == 1)
			{
				fprintf (stderr, "Bad color (%s)\n", argv[i - 1]);
				continue;
			}

		imlib_context_set_color (c.r, c.g, c.b, c.a);
		imlib_add_color_to_color_range (1);
	}
	else if (strcmp (argv[i], "-addd") == 0)
	{
		Color c;
		int distance;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing color\n");
			continue;
		}
			if ((++i) >= argc)
			{
				fprintf (stderr, "Missing distance\n");
				continue;
			}
				if (parse_color (argv[i - 1], &c, alpha) == 1)
				{
					fprintf (stderr, "Bad color (%s)\n", argv[i - 1]);
					continue;
				}
					if (sscanf (argv[i], "%i", &distance) == 1)
					{
						fprintf (stderr, "Bad distance (%s)\n", argv[i]);
						continue;
					}

				imlib_context_set_color (c.r, c.g, c.b, c.a);
				imlib_add_color_to_color_range (distance);
	}
	else if (strcmp (argv[i], "-gradient") == 0)
	{
		int angle;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing angle\n");
			continue;
		}
			if (sscanf (argv[i], "%i", &angle) == 1)
			{
				fprintf (stderr, "Bad angle (%s)\n", argv[i]);
				continue;
			}

		imlib_image_fill_color_range_rectangle (0, 0, width, height,
												angle);
	}

	 else if (strcmp (argv[i], "-fill") == 0)
	 {
		if ((++i) >= argc)
		{
		  fprintf (stderr, "Missing image\n");
		  continue;
		}
		if ( load_Mod_image(Fill, argv[i], width, height, alpha, image, ck0) == 1)
		{
		  fprintf (stderr, "Bad image (%s)\n", argv[i]);
		  continue;
		}
	 }
	else if (strcmp (argv[i], "-dia") == 0)
	{
		if((++i) >= argc)
		{
			fprintf(stderr, "missing Dia, and Image\n");
			continue;
		}
			strcpy (str1, argv[i]);
			strcpy (str2, str1);
				
			if ( findX(str1, &w, &h) == 1 )
			{
				fprintf(stderr, " Bad Format\n");
				continue;
			}
			else if (findX(str2, &w, &h) == 0 && ((++i) >= argc))
			{
				fprintf(stderr, "Missing Image\n");
				continue;
			}
			else
			{
				//if format is correct then assign a number for
				//load_Mod_Image to check
				ck0 = -2;
				w = w;
				h = h;
			}
			if( load_Mod_image(Dia, argv[i], w, h, alpha, image, ck0) == 1 )
			{
			fprintf(stderr, "Bad Image or Bad Image Dimensions \n");
			}
	} 
	else if (strcmp (argv[i], "-tile") == 0)
	{
		if ((++i) >= argc)

			{
			fprintf(stderr, "format 0 missing \n");
			continue;
			}
				strcpy (str1, argv[i]);
				strcpy (str2, str1);
				strcpy (str3, str2);
				strcpy (str4, str3);
				strcpy (str5, str4);


			if ( findX(str1, &w, &h) == 3 &&  ((++i) >= argc))
			{ 
				fprintf(stderr, "missing Image\n");
				continue;
			} //check to see if format is -tile 0 
			else if (findX(str2, &w, &h) == 3)
			{
				ck0 = 3;
				if( load_Mod_image(Tile, argv[i], width, height, alpha, image, ck0) == 1 )
				{
					fprintf(stderr, "Bad Image or Bad Image Dimensions \n");
					continue;
				}
			}

			if (findX(str3, &w, &h) == 1)
			{
				fprintf(stderr, "bad format\n");
				continue;
			}
			 if (findX(str4, &w, &h) == 0 && ((++i) >= argc))
			{
				fprintf(stderr, "missing something again\n");
				continue;
			}
			if (findX (str5, &w, &h) == 0 )
			{
				ck0 = 2;
				w = w;
				h = h;
				
			}
			if( load_Mod_image(Tile, argv[i], w, h, alpha, image, ck0) == 1 )
			{
			fprintf(stderr, "Bad Image or Bad Image Dimension\n");
			}

	}

	else if (strcmp (argv[i], "-center") == 0)
	{
		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing image\n");
			continue;
		}
			if (load_Mod_image (Center, argv[i], width, height, alpha, image, ck0) == 1)
			{
				fprintf (stderr, "Bad image (%s)\n", argv[i]);
				continue;
			}
	}
	else if (strcmp (argv[i], "-tint") == 0)
	{
		Color c;
		DATA8 r[256], g[256], b[256], a[256];
		int j;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing color\n");
			continue;
		}
			if (parse_color (argv[i], &c, 255) == 1)
			{
				fprintf (stderr, "Bad color\n");
				continue;
			}

		imlib_get_color_modifier_tables (r, g, b, a);

			for (j = 0; j < 256; j++)
			{
				r[j] = (DATA8) (((double) r[j] / 255.0) * (double) c.r);
				g[j] = (DATA8) (((double) g[j] / 255.0) * (double) c.g);
				b[j] = (DATA8) (((double) b[j] / 255.0) * (double) c.b);
			}

		imlib_set_color_modifier_tables (r, g, b, a);
	}
	else if (strcmp (argv[i], "-blur") == 0)
	{
		int intval;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing value\n");
			continue;
		}
			if (sscanf (argv[i], "%i", &intval) == 1)
			{
				fprintf (stderr, "Bad value (%s)\n", argv[i]);
				continue;
			}
		imlib_image_blur (intval);
	}
	else if (strcmp (argv[i], "-sharpen") == 0)
	{
		int intval;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing value\n");
			continue;
		}
			if (sscanf (argv[i], "%i", &intval) == 1)
			{
				fprintf (stderr, "Bad value (%s)\n", argv[i]);
				continue;
			}
		imlib_image_sharpen (intval);
	}
	else if (strcmp (argv[i], "-contrast") == 0)
	{
		double dblval;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing value\n");
			continue;
		}
			if (sscanf (argv[i], "%lf", &dblval) == 1)
			{
				fprintf (stderr, "Bad value (%s)\n", argv[i]);
				continue;
			}
		imlib_modify_color_modifier_contrast (dblval);
	}
	else if (strcmp (argv[i], "-brightness") == 0)
	{
		double dblval;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing value\n");
			continue;
		}
			if (sscanf (argv[i], "%lf", &dblval) == 1)
			{
				fprintf (stderr, "Bad value (%s)\n", argv[i]);
				continue;
			}
		imlib_modify_color_modifier_brightness (dblval);
	}
	else if (strcmp (argv[i], "-gamma") == 0)
	{
		double dblval;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing value\n");
			continue;
		}
			if (sscanf (argv[i], "%lf", &dblval) == 1)
			{
				fprintf (stderr, "Bad value (%s)\n", argv[i]);
				continue;
			}
		imlib_modify_color_modifier_gamma (dblval);
	}
	else if (strcmp (argv[i], "-flipv") == 0)
	{
		imlib_image_flip_vertical ();
	}
	else if (strcmp (argv[i], "-fliph") == 0)
	{
		imlib_image_flip_horizontal ();
	}
	else if (strcmp (argv[i], "-flipd") == 0)
	{
		imlib_image_flip_diagonal ();
	}
	else if (strcmp (argv[i], "-write") == 0)
	{
		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing filename\n");
			continue;
		}
      imlib_save_image (argv[i]);
	}
	else
	{
		usage (argv[0]);
		imlib_free_image ();
		imlib_free_color_range ();

			if (modifier != NULL)
			{
				imlib_context_set_color_modifier (modifier);
				imlib_free_color_modifier ();
				modifier = NULL;
			}
				XFreePixmap (display, pixmap);
				exit (1);
	} // end else
} // end loop off of argc

	if (modifier != NULL)
	{
		imlib_context_set_color_modifier (modifier);
        imlib_apply_color_modifier ();
        imlib_free_color_modifier ();
        modifier = NULL;
	}

		imlib_render_image_on_drawable (0, 0);
		imlib_free_image ();
		imlib_free_color_range ();

		if (setRootAtoms (pixmap) == 0)
			fprintf (stderr, "Couldn't create atoms...\n");

		XKillClient (display, AllTemporary);
		XSetCloseDownMode (display, RetainTemporary);

		XSetWindowBackgroundPixmap (display, RootWindow (display, screen),
									pixmap);

		XClearWindow (display, RootWindow (display, screen));

		XFlush (display);
		XSync (display, False);

		imlib_context_pop ();
		imlib_context_free (context);

	} // end for loop off screen
                   //   } //  frist if statment at start of main
  return 0;
}
Example #13
0
void usbdbg_data_out(void *buffer, int length)
{
    switch (cmd) {
        case USBDBG_SCRIPT_EXEC:
            // check if GC is locked before allocating memory for vstr. If GC was locked
            // at least once before the script is fully uploaded xfer_bytes will be less
            // than the total length (xfer_length) and the script will Not be executed.
            if (!script_running && !gc_is_locked()) {
                vstr_add_strn(&script_buf, buffer, length);
                xfer_bytes += length;
                if (xfer_bytes == xfer_length) {
                    // Set script ready flag
                    script_ready = true;

                    // Set script running flag
                    script_running = true;

                    // Disable IDE IRQ (re-enabled by pyexec or main).
                    usbdbg_set_irq_enabled(false);

                    // Clear interrupt traceback
                    mp_obj_exception_clear_traceback(mp_const_ide_interrupt);
                    // Interrupt running REPL
                    // Note: setting pendsv explicitly here because the VM is probably
                    // waiting in REPL and the soft interrupt flag will not be checked.
                    pendsv_nlr_jump_hard(mp_const_ide_interrupt);
                }
            }
            break;

        case USBDBG_TEMPLATE_SAVE: {
            image_t image ={
                .w = MAIN_FB()->w,
                .h = MAIN_FB()->h,
                .bpp = MAIN_FB()->bpp,
                .pixels = MAIN_FB()->pixels
            };

            // null terminate the path
            length = (length == 64) ? 63:length;
            ((char*)buffer)[length] = 0;

            rectangle_t *roi = (rectangle_t*)buffer;
            char *path = (char*)buffer+sizeof(rectangle_t);

            imlib_save_image(&image, path, roi, 50);
            // raise a flash IRQ to flush image
            //NVIC->STIR = FLASH_IRQn;
            break;
        }

        case USBDBG_DESCRIPTOR_SAVE: {
            image_t image ={
                .w = MAIN_FB()->w,
                .h = MAIN_FB()->h,
                .bpp = MAIN_FB()->bpp,
                .pixels = MAIN_FB()->pixels
            };

            // null terminate the path
            length = (length == 64) ? 63:length;
            ((char*)buffer)[length] = 0;

            rectangle_t *roi = (rectangle_t*)buffer;
            char *path = (char*)buffer+sizeof(rectangle_t);

            py_image_descriptor_from_roi(&image, path, roi);
            break;
        }
        default: /* error */
            break;
    }
}
Example #14
0
void usbdbg_data_out(void *buffer, int length)
{
    switch (cmd) {
        case USBDBG_SCRIPT_EXEC:
            // check if GC is locked before allocating memory for vstr. If GC was locked
            // at least once before the script is fully uploaded xfer_bytes will be less
            // than the total length (xfer_length) and the script will Not be executed.
            if (usbdbg_get_irq_enabled() && !gc_is_locked()) {
                vstr_add_strn(&script_buf, buffer, length);
                xfer_bytes += length;
                if (xfer_bytes == xfer_length) {
                    // Set script ready flag
                    script_ready = 1;
                    // Disable IDE IRQ (re-enabled by pyexec or main).
                    usbdbg_set_irq_enabled(false);
                    // interrupt running script/REPL
                    mp_obj_exception_clear_traceback(mp_const_ide_interrupt);
                    pendsv_nlr_jump_hard(mp_const_ide_interrupt);
                }
            }
            break;

        case USBDBG_TEMPLATE_SAVE: {
            image_t image ={
                .w = fb->w,
                .h = fb->h,
                .bpp = fb->bpp,
                .pixels = fb->pixels
            };

            // null terminate the path
            length = (length == 64) ? 63:length; 
            ((char*)buffer)[length] = 0;

            rectangle_t *roi = (rectangle_t*)buffer;
            char *path = (char*)buffer+sizeof(rectangle_t);

            int res=imlib_save_image(&image, path, roi);
            if (res != FR_OK) {
                nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, ffs_strerror(res)));
            }
            // raise a flash IRQ to flush image
            //NVIC->STIR = FLASH_IRQn;
            break;
        }

        case USBDBG_DESCRIPTOR_SAVE: {
            image_t image ={
                .w = fb->w,
                .h = fb->h,
                .bpp = fb->bpp,
                .pixels = fb->pixels
            };

            // null terminate the path
            length = (length == 64) ? 63:length; 
            ((char*)buffer)[length] = 0;

            rectangle_t *roi = (rectangle_t*)buffer;
            char *path = (char*)buffer+sizeof(rectangle_t);

            py_image_descriptor_from_roi(&image, path, roi);
            break;
        }
        default: /* error */
            break;
    }
}

void usbdbg_control(void *buffer, uint8_t request, uint32_t length)
{
    cmd = (enum usbdbg_cmd) request;
    switch (cmd) {
        case USBDBG_FW_VERSION:
            xfer_bytes = 0;
            xfer_length = length;
            break;

        case USBDBG_FRAME_SIZE:
            xfer_bytes = 0;
            xfer_length = length;
            break;

        case USBDBG_FRAME_DUMP:
            xfer_bytes = 0;
            xfer_length = length;
            break;

        case USBDBG_FRAME_LOCK:
            xfer_bytes = 0;
            xfer_length = length;
            break;

        case USBDBG_FRAME_UPDATE:
            sensor_snapshot(NULL);
            cmd = USBDBG_NONE;
            break;

        case USBDBG_SCRIPT_EXEC:
            xfer_bytes = 0;
            xfer_length =length;
            vstr_reset(&script_buf);
            break;

        case USBDBG_SCRIPT_STOP:
            if (usbdbg_get_irq_enabled()) {
                // Disable IDE IRQ (re-enabled by pyexec or main).
                usbdbg_set_irq_enabled(false);
                // interrupt running code by raising an exception
                mp_obj_exception_clear_traceback(mp_const_ide_interrupt);
                pendsv_nlr_jump_hard(mp_const_ide_interrupt);
            }
            cmd = USBDBG_NONE;
            break;

        case USBDBG_SCRIPT_SAVE:
            /* save running script */
            break;

        case USBDBG_TEMPLATE_SAVE:
        case USBDBG_DESCRIPTOR_SAVE:
            /* save template */
            xfer_bytes = 0;
            xfer_length =length;
            break;

        case USBDBG_ATTR_WRITE: {
            /* write sensor attribute */
            int16_t attr= *((int16_t*)buffer);
            int16_t val = *((int16_t*)buffer+1);
            switch (attr) {
                case ATTR_CONTRAST:
                    sensor_set_contrast(val);
                    break;
                case ATTR_BRIGHTNESS:
                    sensor_set_brightness(val);
                    break;
                case ATTR_SATURATION:
                    sensor_set_saturation(val);
                    break;
                case ATTR_GAINCEILING:
                    sensor_set_gainceiling(val);
                    break;
                default:
                    break;
            }
            cmd = USBDBG_NONE;
            break;
        }

        case USBDBG_SYS_RESET:
            NVIC_SystemReset();
            break;

        case USBDBG_BOOT:
            *((uint32_t *)0x20002000) = 0xDEADBEEF;
            NVIC_SystemReset();
            break;

        case USBDBG_TX_BUF:
        case USBDBG_TX_BUF_LEN:
            xfer_bytes = 0;
            xfer_length = length;
            break;

        default: /* error */
            cmd = USBDBG_NONE;
            break;
    }
}