Assuming you have code to read the JPG file then you have each pixel color and usually your read is sitting in a memory block or memory context .... what more do you want :-)
Run the encoding on the memory block or context.
As you have posted in specifically in C if you don't already have reader code the Independent JPEG Group (IJG) provides a library in C where you will find jpegsr9a.zip.
http://www.ijg.org/[
^]
Under Win32 API it is even more trivial to read a JPG file.