Menu

#21 Ability to use it in C#.

closed-implemented-external
nobody
None
1
2015-11-12
2013-08-07
No

Not sure if this is even a valid request.

But i would like to ask for a way to use it in C#, a simple way like a wrapper etc, or whatever is necessary to use it.

As i can´t find any information on this, and would love to be able to use it in c# for my application.

Many Thanks

Discussion

  • DRC

    DRC - 2013-08-07

    I know absolutely nothing about C#. I would imagine that, like you said, a wrapper would have to be constructed, perhaps on top of the TurboJPEG API (that's how our Java wrapper works.)

     
  • Radikal Den store

    Probably, though, there seems to be way to use C code in C# with P/Invoke, sadly i don´t understand it.

    But i can´t understand why no one on the internet have tried to use this in C#.
    I have looked all over the place, as i really want to try it out. Microsofts Jpeg Encoder/Decoder isn´t something to kill for.

     
  • Radikal Den store

    Where can i find how to use Libjpeg-Turbo?
    I want to try it out with c++, but i just want something extremely simple.
    Like, choose a bmp file, compress it, save it.

    But can´t find anything like that.

     
    • DRC

      DRC - 2013-08-10

      The above question is off-topic of the original tracker item, but I'll answer it anyway.

      libjpeg-turbo includes a more straightforward API called "TurboJPEG", which is documented here: http://www.libjpeg-turbo.org/Documentation/Documentation (under "TurboJPEG C API", and you probably want the 1.2.x version.) Once you have an RGB buffer in memory, you use the tjCompress2() function to compress it into another memory buffer, which will contain a JPEG image. Then you would use write() or fwrite() to save it. To get the RGB buffer from a BMP file, you can leverage the convenience functions in bmp.c. These aren't documented or part of the exported TurboJPEG API, but you can look at tjbench.c for an example of how to use them.

      Basically:

      unsigned char rgbbuf=NULL, jpegbuf=NULL;
      unsigned long jpegsize=0;
      int width, height;
      char *filename={whatever};
      tjhandle handle=NULL;

      if(loadbmp(filename, &rgbbuf, &width, &height, BMP_RGB, 0))==-1) {
      printf("Could not load bitmap: %s\n", bmpgeterr());
      exit(1);
      }
      if((handle=tjInitCompress())==NULL) {
      printf("Could not initialize compressor: %s\n", tjGetErrorStr());
      free(rgbbuf);
      exit(1);
      }
      if((tjCompress2(handle, rgbbuf, w, 0, h, TJ_RGB, &jpegbuf, &jpegsize, TJ_{choose a subsampling level here}, {choose a JPEG quality here}, 0)==-1) {
      printf("Could not compress: %s\n", tjGetErrorStr());
      free(rgbbuf);
      tjDestroy(handle);
      exit(1);
      }
      {write jpegbuf to file using the method of your choice.)

       
  • Radikal Den store

    Yes i know, but don´t know where to ask, thanks for answering!

    Though, i have problems with the bmp stuff.
    I can´t find loadbmp, probably because i can´t find the bmp header file, it doesn´t come with the SDK.

    I am probably missing something.

    iostream
    stdlib.h
    stdio.h
    conio.h
    jpeglib.h
    turbojpeg.h
    jmorecfg.h
    jconfig.h
    jerror.h

    using namespace std;
    int loadbmp(char filename, unsigned char buf, int w, int h, int pf,
    int bottomup);
    unsigned char
    rgbbuf=NULL, jpegbuf=NULL;
    unsigned long jpegsize=0;
    int width, height;
    char
    filename={"Screenshot158139.bmp"};
    tjhandle handle=NULL;

    void main(){

    if(loadbmp(filename, &rgbbuf, &width, &height,TJPF_RGB, 0)==-1){
    

    //printf("Could not load bitmap: %s\n", bmpgeterr());
    exit(1);
    }

    if((handle=tjInitCompress())==NULL) {
    printf("Could not initialize compressor: %s\n", tjGetErrorStr());
    free(rgbbuf);
    exit(1);
    }
    if((tjCompress2(handle, rgbbuf, width, 0, height, TJPF_RGB, &jpegbuf, &jpegsize, TJSAMP_444,10, 0))==-1) {
    printf("Could not compress: %s\n", tjGetErrorStr());
    free(rgbbuf);
    tjDestroy(handle);
    exit(1);
    }

    }

    Thanks

    EDIT: think i can just add loadbmp myself like that though. but not sure what BMP_RGB is supposed to be;S

     

    Last edit: Radikal Den store 2013-08-11
    • DRC

      DRC - 2013-08-11

      The correct place to ask is on the libjpeg-turbo-users mailing list. That list is for addressing "user-level" problems with libjpeg-turbo (meaning problems in using the SDK to develop your own programs, since "users" in this case are developers.) There is also a libjpeg-turbo-devel mailing list, which should be used for issues encountered when developing libjpeg-turbo (including issues when building from source.)

      As I said, the loadbmp() API is not exposed in the SDK, so you won't find it there. You have to include bmp.h and bmp.c from the source into your project. Again, please study tjbench.c.

      If you have any follow-up questions, please post them to the mailing list.

       
  • zigzag

    zigzag - 2014-02-13

    +1 for c# wrapper

     
  • DRC

    DRC - 2014-02-13

    I am not a C# developer, so a C# wrapper is just not going to happen unless an organization specifically funds the work or unless someone contributes the code. It would take significant research on my part. The Java wrapper was non-trivial to implement, but that work was fully sponsored. I imagine that the C# wrapper would be similar and would probably also have to be based on the TurboJPEG API, given the inherent difficulties associated with using the libjpeg API in an object oriented manner.

     
  • BP

    BP - 2015-11-12

    I just want to point out, there are now two .NET wrappers for libjpeg-turbo. Neither is as complete as the Java wrapper, but both can encode and decode jpeg images.

    I made this one: http://turbojpegcli.codeplex.com/

    Because I didn't know about this one until I was nearly done: https://www.nuget.org/packages/TurboJpegWrapper/

    Internally, the two wrappers are very different. Mine is written in C++/CLI to accomplish the interoperability. The other is written in C# and uses P/Invoke to call native functions. Both are usable from any .NET language, including C#.

     
  • DRC

    DRC - 2015-11-12

    Cool! Thanks for the pointers. I will close this issue as resolved. At some point, it might be nice to integrate one of these into libjpeg-turbo, but since I'm not a .NET developer, at the moment that isn't a priority for me. Seems like there also might be some advantages to having this be an external thing, given the differing approaches to .NET programming.

     

    Last edit: DRC 2015-11-12
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.