From e809cc7405575712f49335e8527aaa13d2d4f26a Mon Sep 17 00:00:00 2001 From: Vladimir Ananiev Date: Sun, 23 Dec 2007 00:20:14 +0300 Subject: [PATCH 8/8] [neuros] add snap screen GrSnapScreenToImage GrSnapScreenToImage - Snap picture form screen to .jpg file Signed-off-by: Vladimir Ananiev --- src/engine/devimage.c | 17 ++++++++++++ src/engine/image_jpeg.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++ src/include/device.h | 2 + src/include/nano-X.h | 4 +++ src/nanox/client.c | 29 ++++++++++++++++++++- src/nanox/nxproto.h | 14 +++++++++- src/nanox/srvfunc.c | 12 ++++++++ src/nanox/srvnet.c | 13 +++++++++ 8 files changed, 155 insertions(+), 2 deletions(-) diff --git a/src/engine/devimage.c b/src/engine/devimage.c index 0950670..24776ba 100644 --- a/src/engine/devimage.c +++ b/src/engine/devimage.c @@ -258,6 +258,23 @@ GdLoadImageFromFile(PSD psd, char *path, int flags) #endif /* defined(HAVE_FILEIO) */ /* + * GdSnapToImage: + * @psd: Drawing surface. + * @x: X coord. + * @y: Y coord. + * @width: width + * @height: height. + * @path: buffer to store image data + * + * Snap to the image. + */ +int +GdSnapToImage(PSD psd, int x, int y, int width,int height,const char* path) +{ + return GdEncodeJPEG(psd,x,y,width,height,path); +} + +/* * GdDecodeImage: * @psd: Drawing surface. * @src: The image data. diff --git a/src/engine/image_jpeg.c b/src/engine/image_jpeg.c index df3b57e..afadf7f 100644 --- a/src/engine/image_jpeg.c +++ b/src/engine/image_jpeg.c @@ -85,6 +85,72 @@ term_source(j_decompress_ptr cinfo) } int +GdEncodeJPEG(PSD psd, int sx, int sy, int swidth,int sheight,const char* file) +{ + int y; + FILE *fp; + unsigned long colorval = 0; + struct jpeg_compress_struct cinfo; + struct jpeg_error_mgr jerr; + + fp = fopen(file, "wb"); + if (!fp) { + return (-1); + } + + cinfo.err = jpeg_std_error(&jerr); + jpeg_create_compress(&cinfo); + jpeg_stdio_dest(&cinfo, fp); + + cinfo.in_color_space = JCS_RGB; + cinfo.image_width = swidth; + cinfo.image_height =sheight; + cinfo.input_components = 3; + jpeg_set_defaults(&cinfo); + jpeg_start_compress(&cinfo, TRUE); + + for (y = 0; y < sheight; y++) { + JSAMPROW row[1]; + int x; + unsigned char *dest = malloc(swidth * 3); + unsigned char *sour = malloc(swidth * 2); + GdReadArea(psd, sx, sy+y, swidth, 1,sour); + unsigned char *ptr = dest; + unsigned char *ch = sour; + for (x = 0; x < swidth; x++) { + switch (psd->bpp) { + case 8: + colorval = PIXEL332TOCOLORVAL(*ch); + break; + case 16: + colorval = + PIXEL565TOCOLORVAL(*((unsigned short *)ch)); + break; + case 24: + case 32: + colorval = PIXEL888TOCOLORVAL(*((unsigned long *)ch)); + break; + } + ptr[2] = (colorval >> 16) & 0xFF; + ptr[1] = (colorval >> 8) & 0xFF; + ptr[0] = (colorval & 0xFF); + ptr += 3; + ch += 2; + } + row[0] = dest; + jpeg_write_scanlines(&cinfo, row, 1); + free(dest); + free(sour); + } + + jpeg_finish_compress(&cinfo); + jpeg_destroy_compress(&cinfo); + fclose(fp); + + return 0; +} + +int GdDecodeJPEG(buffer_t * src, PMWIMAGEHDR pimage, PSD psd, MWBOOL fast_grayscale) { int i; diff --git a/src/include/device.h b/src/include/device.h index 4b2a81a..7a9bc1f 100644 --- a/src/include/device.h +++ b/src/include/device.h @@ -916,6 +916,7 @@ void GdDrawImageFromFile(PSD psd, MWCOORD x, MWCOORD y, MWCOORD width, int GdLoadImageFromFile(PSD psd, char *path, int flags); void GdDrawImageToFit(PSD psd, MWCOORD x, MWCOORD y, MWCOORD width, MWCOORD height, int id); +int GdSnapToImage(PSD psd, int x, int y, int width,int height,const char* path); void GdFreeImage(int id); MWBOOL GdGetImageInfo(int id, PMWIMAGEINFO pii); void GdStretchImage(PMWIMAGEHDR src, MWCLIPRECT *srcrect, PMWIMAGEHDR dst, @@ -943,6 +944,7 @@ int GdDecodeBMP(buffer_t *src, PMWIMAGEHDR pimage); #endif #ifdef HAVE_JPEG_SUPPORT int GdDecodeJPEG(buffer_t *src, PMWIMAGEHDR pimage, PSD psd, MWBOOL fast_grayscale); +int GdEncodeJPEG(PSD psd, int x, int y, int width,int height,const char* path); #endif #ifdef HAVE_PNG_SUPPORT int GdDecodePNG(buffer_t *src, PMWIMAGEHDR pimage); diff --git a/src/include/nano-X.h b/src/include/nano-X.h index 6ff3bf6..2aa8669 100644 --- a/src/include/nano-X.h +++ b/src/include/nano-X.h @@ -865,6 +865,8 @@ void GrDrawImageToFit(GR_DRAW_ID id, GR_GC_ID gc, GR_COORD x, void GrDrawImagePartToFit(GR_DRAW_ID id, GR_GC_ID gc, GR_COORD dx, GR_COORD dy, GR_SIZE dwidth, GR_SIZE dheight, GR_COORD sx, GR_COORD sy, GR_SIZE swidth, GR_SIZE sheight, GR_IMAGE_ID imageid); +int GrSnapScreenToImage(const char *path,GR_COORD x, GR_COORD y, + GR_SIZE width, GR_SIZE height); void GrFreeImage(GR_IMAGE_ID id); void GrGetImageInfo(GR_IMAGE_ID id, GR_IMAGE_INFO *iip); void GrText(GR_DRAW_ID id, GR_GC_ID gc, GR_COORD x, GR_COORD y, @@ -914,6 +916,8 @@ void GrUnregisterInput(int fd); void GrMainLoop(GR_FNCALLBACKEVENT fncb); GR_FNCALLBACKEVENT GrSetErrorHandler(GR_FNCALLBACKEVENT fncb); void GrDefaultErrorHandler(GR_EVENT *ep); +int GrSnapScreenToImage(const char *path,GR_COORD x, GR_COORD y, + GR_SIZE width, GR_SIZE height); /* passive library entry points - available with client/server only*/ void GrPrepareSelect(int *maxfd,void *rfdset); diff --git a/src/nanox/client.c b/src/nanox/client.c index 316d079..80360b4 100644 --- a/src/nanox/client.c +++ b/src/nanox/client.c @@ -4839,5 +4839,32 @@ GrCopyFont(GR_FONT_ID fontid, GR_COORD height) } #endif /*HAVE_FREETYPE_2_SUPPORT*/ +/* +*Snap picture form screen to .jpg file +* +* @param path string containing the filename of the image to save +* @param x the area on screen will be save +* @param y the area on screen will be save +* @param width the area on screen will be save +* @param height the area on screen will be save +* +*/ - +int +GrSnapScreenToImage(const char *path,GR_COORD x, GR_COORD y, + GR_SIZE width, GR_SIZE height) +{ + int ret; + nxSnapScreenToImageReq *req; + LOCK(&nxGlobalLock); + req = AllocReqExtra(SnapScreenToImage, strlen(path)+1); + req->x = x; + req->y = y; + req->width = width; + req->height = height; + memcpy(GetReqData(req), path, strlen(path)+1); + if (TypedReadBlock(&ret, sizeof(ret), GrNumSnapScreenToImage) == -1) + ret = -1; + UNLOCK(&nxGlobalLock); + return ret; +} diff --git a/src/nanox/nxproto.h b/src/nanox/nxproto.h index cc46baf..a4bc274 100644 --- a/src/nanox/nxproto.h +++ b/src/nanox/nxproto.h @@ -1384,4 +1384,16 @@ typedef struct { IDTYPE imageid; } nxDrawImagePartToFitReq; -#define GrTotalNumCalls 126 +#define GrNumSnapScreenToImage 126 +typedef struct { + BYTE8 reqType; + BYTE8 hilength; + UINT16 length; + UINT16 x; + UINT16 y; + UINT16 width; + UINT16 height; +} nxSnapScreenToImageReq; + + +#define GrTotalNumCalls 127 diff --git a/src/nanox/srvfunc.c b/src/nanox/srvfunc.c index d909819..1a780cd 100644 --- a/src/nanox/srvfunc.c +++ b/src/nanox/srvfunc.c @@ -4335,3 +4335,15 @@ GrUngrabKey(GR_WINDOW_ID id, GR_KEY key) SERVER_UNLOCK(); } + +int +GrSnapScreenToImage(const char *path,GR_COORD x, GR_COORD y, + GR_SIZE width, GR_SIZE height) +{ + int ret; + SERVER_LOCK(); + ret = GdSnapToImage(rootwp->psd, x,y, + width, height,path); + SERVER_UNLOCK(); + return ret; +} diff --git a/src/nanox/srvnet.c b/src/nanox/srvnet.c index 3afb1fc..2b85853 100644 --- a/src/nanox/srvnet.c +++ b/src/nanox/srvnet.c @@ -1604,6 +1604,18 @@ GrCopyFontWrapper(void *r) #endif /*HAVE_FREETYPE_2_SUPPORT*/ } +static void +GrSnapScreenToImageWrapper(void *r) +{ + int ret; + nxSnapScreenToImageReq *req = r; + ret = GrSnapScreenToImage(GetReqData(req),req->x,req->y, + req->width,req->height); + GsWriteType(current_fd, GrNumSnapScreenToImage); + GsWrite(current_fd, &ret, sizeof(ret)); + +} + void GrShmCmdsFlushWrapper(void *r); @@ -1741,6 +1753,7 @@ static const struct GrFunction GrFunctions[] = { /* 123 */ {GrCreateFontFromBufferWrapper, "GrCreateFontFromBuffer"}, /* 124 */ {GrCopyFontWrapper, "GrCopyFont"}, /* 125 */ {GrDrawImagePartToFitWrapper, "GrDrawImagePartToFit"}, + /* 126 */ {GrSnapScreenToImageWrapper, "GrSnapScreenToImage"}, }; void -- 1.5.1.6