Saving image to file with IImageEncoder - c++

do you have a working code to share.
I’m trying to figure out how to save to a file an IBitmapImage image.
I need to resize existing .jpg file and it seems like the only API for Windows Mobile. I managed to load it convert it to IImage -> IBitmapImage -> IBasicBitmapOps and resize it finally, but I have no clue how to save it properly to a new file.

Use IBitmapImage::LockBits to get access to the image data via its BitmapData* lockedBitmapData parameter. Use the BitmapData to prepare a bitmap file info header, then write that one and the image data in BitmapData::Scan0 to a file using regular file writing with ::WriteFile or higher level ones if you use such.

Related

Determining the format of audio file (MP3) using SAPI

HiI'm trying to create a "Speech to text" app that can transcribe any audio/video file. I've created an app based on this post and it works great for WAV files. But if I use an MP3 file, the line hr = cpInputStream->BindToFile(wInputFileName.c_str(), SPFM_OPEN_READONLY, &sInputFormat.FormatId(), sInputFormat.WaveFormatExPtr(), SPFEI_ALL_EVENTS); returns
The Parameter is incorrect
The question is, can I use MP3 files as input for SAPI? and if yes, how do I determine the correct format for the call to hr = sInputFormat.AssignFormat(SPSF_16kHz16BitStereo) because SPSF_16kHz16BitStereo will certainly not be correct and I don't think we should hardcode it.

Trying to encode a GIF file using giflib

I am given image data and color table I am trying to export it as a single frame GIF using giflib. I looked into the API, but can't get it to work. The program crashes even at the first function:
GifFileType image_out;
int errorCode = 0;
char* fileName = "SomeName.gif";
image_out = *EGifOpenFileName(fileName,true, &errorCode);
It is my understanding that I first need to open a file by specifying it's name and then update it with fileHandle. Then Fill in the screen description, the extension block the image data and add the 3B ending to the file. Then use EGifSpew to export the whole gif. The problem is that I can't even use EGifOpenFileName(); The program crashes at that line.
Can someone help me the API of giflib? This problem is getting really frustrating.
Thanks.
EDIT:
For the purposes of simple encoding I do not want to specify a color table and I just want to encode a single frame GIF.
The prototype is:
GifFileType *EGifOpenFileName(char *GifFileName, bool GifTestExistance, int *ErrorCode)
You should write as
GifFileType* image_out = EGifOpenFileName(fileName,true, &errorCode);
Note GifFileType is not POD type so you should NOT copy like that.

Save image or animation in pyglet

I have an image saving issue. I need to take input of either a bmp, jpg, png, or gif and display it. The display part is fairly easy, the hard part is saving it. I want to take the user-supplied file and save it in a zipped directory that's going to be the save file for this project. I see that I can do pygletimage.save(filename) and get a png file. I'm fine with the format change for still images. The question is, how can I save a gif file easily in pyglet?
I use this function at every frame :
pyglet.image.get_buffer_manager().get_color_buffer().save(filename)
Here's the complete snippet. It saves whatever you display to numbered image files.
#---EXPORT --------------------------------------------------------
def save_a_frame(self):
file_num=str(self.frame_number).zfill(5)
file_t=str(self.chrono)
filename="frame-"+file_num+'-# '+file_t+'sec.png'
pyglet.image.get_buffer_manager().get_color_buffer().save(filename)
print 'image file writen : ',filename
def export_loop(self,dt):
constant_interval=1.0/PicPS
if self.chrono<END_TIME:
# update at a constant dt, regardless of real time
# so that even if refresh is slow no frame should be missing
# self.frame_draw(PicPS)
self.update(constant_interval)
self.frame_draw(dt)
self.frame_number+=1
self.save_a_frame()
else:
exit()
You can save each frame as a PNG via your pygletimage.save() method, then use the imagemagick command-line tool to combine those into a GIF like so:
convert -delay 10 -loop 0 frame_*.png animation.gif
See https://www.imagemagick.org/Usage/anim_basics/ for more information.

generating thumbnails using CF8 cfimage tag - large filesize caused by image metadata

i'm building a small web app that will resize images to different pixel dimensions after they have been uploaded.
I am attempting to create 150px X 100px thumbnail from a 3mb jpg image, but am unable to get the filesize smaller than 68kb ( I would expect to generate a file between 4kb & 15kb file depending on compression type etc).
I have used the action="resize" method of the tag & also used the ImageResize() & imageScaleToFit() functions but all these methods keep the exif & IPTC meta data intact (I am assuming this is what is keeping the thumbnail file as large as it is).
If I use an image without any metadata it resizes the thumbnail to a small filesize as expected.
Is there anyway I can strip the exif & IPTC metadata from the image to reduce the thumbnail size using any of CF8's built in image functions?
you can use the Sanselan Java library to remove EXIF and IPTC metadata. Here is a code sample using the removeExifMetadata from this library:
<cfscript>
// setup and init the Sanselan library
SanselanPath = arrayNew(1);
arrayAppend(SanselanPath, expandPath("sanselan\sanselan-0.97-incubator.jar"));
javaloader = createObject("component", "javaloader.JavaLoader").init(SanselanPath);
// setup your source and destination image
pathToInFile = ExpandPath("myImage.jpg");
pathToOutFile = ExpandPath("MyImagewoEXIF.jpg");
inFile = javaloader.create("java.io.FileInputStream").init(pathToInFile);
outFile = javaloader.create("java.io.FileOutputStream").init(pathToOutFile);
// create the exifRewriter
exifRewriter = javaloader.create("org.apache.sanselan.formats.jpeg.exifRewrite.ExifRewriter").init();
// call the method removeExifMetadata
exifRewriter.removeExifMetadata(inFile,outFile);
outFile.close();
</cfscript>
You can do the same to remove IPTC meta data using iptc.JpegIptcRewriter and the removeIPTC method. You can verify using the CF functions ImageGetEXIFMetaData and ImageGetIPTCMetadata that everything has been removed.
Now I'm not sure that it will really reduce the file size, let me know :-)
You can create a new image the same dimensions as the thumbnail and use ImagePaste to paste in the thumbnail. ColdFusion does not preserve EXIF data with the paste operation.
If you wanted to, you could use the command line tool that is linked in the post that #Ciaran Archer linked above, and then write a bash command (*nix/Mac) or bat command (Windows) that runs the command and then use CFExecute to run that command. I have never really worked with CFImage before or image metadata so I couldn't give you the proper code, but what I said will probably have the best performance, because you are exporting commands to the filesystem is made to do (work with files).
Check this out:
Use
ImageWrite(imageObject,destination,0.9,true) returns small sized image (90% quality, overwrite="true")
instead of
ImageWrite(imageObject,destination,true,0.9) returns big sized image (quality="true" which is 1, overwrite="0.9" which is true)
Select quality ratio from 0 to 1 to reduce the file size.

How to store wxImage into database, using C++?

I have some wxImages and I would like to store them into a BLOB (Binary Large OBject) field in a MySQL database.
There are no methods in wxImage nor wxBitmap for obtaining the binary data as an array of unsigned char so I can load into the database.
My current workaround is to write the image to a temporary file, then load the BLOB field directly from the file.
Is there a more efficient method to load and store a wxImage object into a MySQL BLOB field?
I am using MySql C++ connector 1.05, MS Visual Studio 2008, wxWidgets and C++.
wxWidgets doesn't provide any API to the data from wxBitmap (because it's platform-dependent) but wxImage uses a well-defined (and very simple) format which you can access using its GetData() method as mentioned above. Just notice that you may need to use GetAlpha() as well if your images have alpha channel.
However this is not how I'd do it because the data will be huge if you do it like this. While compressing it, as also suggested above, is possible, why bother doing it manually when wxImage already supports writing image in any of the standard image formats. Just create a wxMemoryOutputStream and pass it to SaveFile(). Then simply access the stream buffer directly using GetOutputStreamBuffer()->GetBufferStart() and related functions.
Unless I'm missing something, couldn't you use WxImage::GetData (http://docs.wxwidgets.org/2.8/wx_wximage.html#wximagegetdata) to get the data and then ::GetHeight and ::GetWidth to know the length of the data that pointer points to? The unsigned char * that is returned by WxImage::GetData looks like it should point to the RGB data that makes up the image.