Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 22 Nov 2017, 01:47
All times are UTC - 4
 Forum index » Advanced Topics » Cutting edge
Puppy In-House Development
Moderators: Flash, Ian, JohnMurga
Post new topic   Reply to topic View previous topic :: View next topic
Page 27 of 27 [396 Posts]   Goto page: Previous 1, 2, 3, ..., 25, 26, 27
Author Message
technosaurus


Joined: 18 May 2008
Posts: 4756
Location: Kingwood, TX

PostPosted: Fri 06 Nov 2015, 18:52    Post subject:  

In my quest to minimalize jwm (replace, cairo, librsvg, libjpeg and libpng with stb_image and nanosvg), I ended up writing this minimal image viewer:

Note: nanosvg uses zlib license, the rest is public domain
Code:
#include <xcb/xcb.h>
#include <xcb/xcb_image.h>
#define STBI_NO_HDR
#define STBI_NO_LINEAR
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define NANOSVG_IMPLEMENTATION
#include "nanosvg.h"
#define NANOSVGRAST_IMPLEMENTATION
#include "nanosvgrast.h"

int main(int argc, char **argv){
   xcb_connection_t *c = xcb_connect(0, 0);
   xcb_screen_t *s = xcb_setup_roots_iterator(xcb_get_setup(c)).data;
   int w, h, n,
      depth = s->root_depth,
      win_class = XCB_WINDOW_CLASS_INPUT_OUTPUT,
      format = XCB_IMAGE_FORMAT_Z_PIXMAP;
   xcb_colormap_t colormap = s->default_colormap;
   xcb_drawable_t win = xcb_generate_id(c);
   xcb_gcontext_t gc = xcb_generate_id(c);
   xcb_pixmap_t pixmap = xcb_generate_id(c);
   xcb_generic_event_t *ev;
   xcb_image_t *image;
   NSVGimage *shapes = NULL;
   NSVGrasterizer *rast = NULL;
   char *data = NULL;
   unsigned *dp;
   size_t i, len;
   uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
      value_mask = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS,
      values[] = { s->black_pixel, value_mask };

   if (argc<2) return -1;
   if ((data = stbi_load(argv[1], &w, &h, &n, 4)))
      ;
   else if ((shapes = nsvgParseFromFile(argv[1], "px", 96.0f))) {
      w = (int)shapes->width;
      h = (int)shapes->height;
      rast = nsvgCreateRasterizer();
      data = malloc(w*h*4);
      nsvgRasterize(rast, shapes, 0,0,1, data, w, h, w*4);
   }else return -1;
   for(i=0,len=w*h,dp=(unsigned *)data;i<len;i++) //rgba to bgra
      dp[i]=dp[i]&0xff00ff00|((dp[i]>>16)&0xFF)|((dp[i]<<16)&0xFF0000);
   xcb_create_window(c,depth,win,s->root,0,0,w,h,1,win_class,s->root_visual,mask,values);
   xcb_create_pixmap(c,depth,pixmap,win,w,h);
   xcb_create_gc(c,gc,pixmap,0,NULL);
   image = xcb_image_create_native(c,w,h,format,depth,data,w*h*4,data);
   xcb_image_put(c, pixmap, gc, image, 0, 0, 0);
   xcb_image_destroy(image);
   xcb_map_window(c, win);
   xcb_flush(c);
   while ((ev = xcb_wait_for_event(c))) {
      switch (ev->response_type & ~0x80){
      case XCB_EXPOSE: {
         xcb_expose_event_t *x = (xcb_expose_event_t *)ev;
         xcb_copy_area(c,pixmap,win,gc,x->x,x->y,x->x,x->y,x->width,x->height);
         xcb_flush(c);
      }break;
      case XCB_BUTTON_PRESS: goto end;
      default: break;
      }
   }
end:
   xcb_free_pixmap(c, pixmap);
   xcb_disconnect(c);
   return 0;
}
90% of the code is just the verbosity of dealing with an xcb window.


An up to date gist to lighten jwm image loading is available at:
https://gist.github.com/technosaurus/3603768de1f83d670546
image.c.gz
Description  replace jwm's image.c with this and compile with extra CFLAGS:
-DUSE_STB_IMAGE and -DUSE_NANOSVG -Wl,-lm
(with svg, png and jpeg disabled)
supports jpeg,png,gif,svg,tga,bmp and others
gz

 Download 
Filename  image.c.gz 
Filesize  4.41 KB 
Downloaded  89 Time(s) 

_________________
Check out my github repositories. I may eventually get around to updating my blogspot.
Back to top
View user's profile Send private message Visit poster's website 
technosaurus


Joined: 18 May 2008
Posts: 4756
Location: Kingwood, TX

PostPosted: Mon 14 Dec 2015, 10:15    Post subject:  

I have posted my minimalistic libc implemenatation to github and named it Brad's Quixotic C
I wrote a little test program to test extremely low overhead threads and it only takes up 156 bytes of RAM on x86_64 (would probably be less on x86)
... something to think about for long running daemon processes.
It's not a full C implementation by any means, but let me know if there are any priority functions anyone would like to see implemented for their projects.

_________________
Check out my github repositories. I may eventually get around to updating my blogspot.
Back to top
View user's profile Send private message Visit poster's website 
goingnuts

Joined: 07 Dec 2008
Posts: 896

PostPosted: Sun 02 Jul 2017, 15:23    Post subject:  

Xlib version of technosaurus xcb based image-viewer two post back:
Code:
/*Inspired by
*   xputjpeg by Gleicon S. Moraes - gleicon@terra.com.br
      from: https://searchcode.com/codesearch/raw/107558322/
*   unamed example by technosaurus
      from http://murga-linux.com/puppy/viewtopic.php?t=89272&start=390&sid=1f696000b4032a89e59e66135ce527b8

fun with stb_image...
goingnuts July 2017
compile: gcc xputimage.c -o xputimage -L/usr/lib -L/usr/X11/lib -lX11 -lm
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/xpm.h>
#include <X11/extensions/shape.h>
#include <math.h>

#ifndef sinf
float sinf (float x) { return (float) sin( (double)x ); }
#endif
#ifndef cosf
float cosf (float x) { return (float) cos( (double)x ); }
#endif
#ifndef tanf
float tanf  (float x){return (float) tan ( (double)x );}
#endif
#ifndef atan2f
float atan2f(float x, float y){return (float) atan2( (double)x, (double)y );}
#endif
#ifndef fmodf
float fmodf (float x, float y){return (float) fmod ( (double)x, (double)y );}
#endif
#ifndef acosf
float acosf (float x){return (float) acos( (double)x );}
#endif

#define STBI_NO_HDR
#define STBI_NO_LINEAR
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define NANOSVG_IMPLEMENTATION
#include "nanosvg.h"
#define NANOSVGRAST_IMPLEMENTATION
#include "nanosvgrast.h"

int main (int argc, char **argv) {

   int            i, len, w, h, n, bpl, depth, screen;
   Pixel         white, black;
   Window         Root, iconwin, win;
   XEvent          x_event;
   XImage         *ximage;
   Display         *dpy;
   GC            gc;
   XGCValues      gcv;
   char         *data;
   unsigned       *dp;
   NSVGimage *shapes = NULL;
   NSVGrasterizer *rast = NULL;
   
   if (argc < 2) {
      printf ("Use: %s path.to.image\n", argv[0]);
      exit(1);
   }
   
   if ((data = stbi_load(argv[1], &w, &h, &n, 4)))
      ;
   else if ((shapes = nsvgParseFromFile(argv[1], "px", 96.0f))) {
      w = (int)shapes->width;
      h = (int)shapes->height;
      rast = nsvgCreateRasterizer();
      data = malloc(w*h*4);
      nsvgRasterize(rast, shapes, 0,0,1, data, w, h, w*4);
   } else {
         printf("Unable to load image: %s\n", argv[0]);
      return -1;
   }
      
    for(i=0, len=w*h, dp=(unsigned *)data; i<len; i++) //rgba to bgra
      dp[i]=dp[i]&0xff00ff00|((dp[i]>>16)&0xFF)|((dp[i]<<16)&0xFF0000);
     
      if (!(dpy = XOpenDisplay(NULL)))
      printf("can't open X display\n");
   
   screen = DefaultScreen(dpy);
   Root = RootWindow (dpy, screen);
   white = WhitePixel (dpy, screen);
   black = BlackPixel (dpy, screen);

   win = XCreateSimpleWindow (dpy, Root, 0, 0, w, h, 0, black, white);

   iconwin = XCreateSimpleWindow (dpy,   win, 0, 0, w, h, 0, black, white);

   XSelectInput(dpy, win, ExposureMask | KeyPressMask);
   XMapWindow (dpy, win);
   gc = XCreateGC (dpy, win, 0, &gcv);

   depth = DefaultDepth(dpy, screen);

   switch(depth) {
   
      case 24:    
         bpl=4; break;
      case 16:
      case 15:    
         bpl=2; break;
      default:
         bpl=1; break;
   }

   ximage = XCreateImage (dpy,
           CopyFromParent, depth,
           ZPixmap, 0,
           data,
           w, h,
           bpl*8, bpl * w);

   XFlush(dpy);

   while (1) {
      XNextEvent(dpy, &x_event);
         switch (x_event.type) {
            case Expose:
               XPutImage (dpy, win, gc, ximage, 0,0,0,0, w, h);
                XFlush (dpy);     
               break;
            case KeyPress:   goto end;
            default: break;   
         }
   }

end:
   free (data);
   close(ConnectionNumber(dpy));
   return 0;
}

All the #ifdef math stuff for compile with old uclibc.
One odd thing: If compiled with any CFLAGS like Os, O1 etc. results in svg not working. Static linked bin attached as fake .gz-file.
xputimage.gz
Description  remove .gz extension to get the static linked binary
gz

 Download 
Filename  xputimage.gz 
Filesize  86.88 KB 
Downloaded  32 Time(s) 

Last edited by goingnuts on Sun 02 Jul 2017, 16:44; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website 
greengeek


Joined: 20 Jul 2010
Posts: 4716
Location: Republic of Novo Zelande

PostPosted: Sun 02 Jul 2017, 15:48    Post subject:  

Tested ok on Slacko 5.6
Opens png and jpg fine, but did not work with a tif i made.
Back to top
View user's profile Send private message 
goingnuts

Joined: 07 Dec 2008
Posts: 896

PostPosted: Sun 02 Jul 2017, 17:03    Post subject:  

Thanks for testing! No tiff support but jpg, png, bmp, gif, SVG and others.
Might have a look at https://github.com/jkriege2/TinyTIFF some time...
Update 20170810: Just realized that my code only works at depth 24 - Sad
Back to top
View user's profile Send private message Visit poster's website 
greengeek


Joined: 20 Jul 2010
Posts: 4716
Location: Republic of Novo Zelande

PostPosted: Sat 04 Nov 2017, 14:58    Post subject:  

technosaurus wrote:
In my quest to minimalize jwm (replace, cairo, librsvg, libjpeg and libpng with stb_image and nanosvg), I ended up writing this minimal image viewer:

Note: nanosvg uses zlib license, the rest is public domain
Code:
#include <xcb/xcb.h>
#include <xcb/xcb_image.h>
#define STBI_NO_HDR
#define STBI_NO_LINEAR
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define NANOSVG_IMPLEMENTATION
#include "nanosvg.h"
#define NANOSVGRAST_IMPLEMENTATION
#include "nanosvgrast.h"

int main(int argc, char **argv){
   xcb_connection_t *c = xcb_connect(0, 0);
   xcb_screen_t *s = xcb_setup_roots_iterator(xcb_get_setup(c)).data;
   int w, h, n,
      depth = s->root_depth,
      win_class = XCB_WINDOW_CLASS_INPUT_OUTPUT,
      format = XCB_IMAGE_FORMAT_Z_PIXMAP;
   xcb_colormap_t colormap = s->default_colormap;
   xcb_drawable_t win = xcb_generate_id(c);
   xcb_gcontext_t gc = xcb_generate_id(c);
   xcb_pixmap_t pixmap = xcb_generate_id(c);
   xcb_generic_event_t *ev;
   xcb_image_t *image;
   NSVGimage *shapes = NULL;
   NSVGrasterizer *rast = NULL;
   char *data = NULL;
   unsigned *dp;
   size_t i, len;
   uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
      value_mask = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS,
      values[] = { s->black_pixel, value_mask };

   if (argc<2) return -1;
   if ((data = stbi_load(argv[1], &w, &h, &n, 4)))
      ;
   else if ((shapes = nsvgParseFromFile(argv[1], "px", 96.0f))) {
      w = (int)shapes->width;
      h = (int)shapes->height;
      rast = nsvgCreateRasterizer();
      data = malloc(w*h*4);
      nsvgRasterize(rast, shapes, 0,0,1, data, w, h, w*4);
   }else return -1;
   for(i=0,len=w*h,dp=(unsigned *)data;i<len;i++) //rgba to bgra
      dp[i]=dp[i]&0xff00ff00|((dp[i]>>16)&0xFF)|((dp[i]<<16)&0xFF0000);
   xcb_create_window(c,depth,win,s->root,0,0,w,h,1,win_class,s->root_visual,mask,values);
   xcb_create_pixmap(c,depth,pixmap,win,w,h);
   xcb_create_gc(c,gc,pixmap,0,NULL);
   image = xcb_image_create_native(c,w,h,format,depth,data,w*h*4,data);
   xcb_image_put(c, pixmap, gc, image, 0, 0, 0);
   xcb_image_destroy(image);
   xcb_map_window(c, win);
   xcb_flush(c);
   while ((ev = xcb_wait_for_event(c))) {
      switch (ev->response_type & ~0x80){
      case XCB_EXPOSE: {
         xcb_expose_event_t *x = (xcb_expose_event_t *)ev;
         xcb_copy_area(c,pixmap,win,gc,x->x,x->y,x->x,x->y,x->width,x->height);
         xcb_flush(c);
      }break;
      case XCB_BUTTON_PRESS: goto end;
      default: break;
      }
   }
end:
   xcb_free_pixmap(c, pixmap);
   xcb_disconnect(c);
   return 0;
}
90% of the code is just the verbosity of dealing with an xcb window.


An up to date gist to lighten jwm image loading is available at:
https://gist.github.com/technosaurus/3603768de1f83d670546


Hi Technosaurus, I am interested in this tiny image viewer (and it's relatives on previous page). How do I potentially use them? Is it "C" code? I tried to stick it in a bash script but of course I am well out of my depth.

I am wanting to display (in a minimal window) jpg files that are accumulated from my webcam based "security camera".

Any suggestions appreciated.

cheers!

EDIT : Do I need to do a "git pull" or something from "technosaurus/jwm-image-lite.c" you linked above - or is that just jwm related?
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 27 of 27 [396 Posts]   Goto page: Previous 1, 2, 3, ..., 25, 26, 27
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Advanced Topics » Cutting edge
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0808s ][ Queries: 13 (0.0222s) ][ GZIP on ]