Difficulties and opportunity of trying to convert JPEG into RAW format.
It is not always possible to convert a JPEG file to the original RAW file.
Note: I speak about original or specific RAW format because it is not a standard: RAW formats are service formats, different one from another, used by the manufacturer to quickly save the contents of the CCD into the buffer and in case on the storage device.
Limits. You might be able to turn it into a valid RAW format, but that doesn't mean the individual values stored in the new RAW file are the same as you might have if you saved the shot directly in RAW format.
Opportunity. It is not said that even if you can convert a JPEG image to RAW format and start from it for processing, the final result will be better than if you start directly from the JPEG you already have. In fact you will apply a filter (algorithm) that transforms one format into another and that can introduce further alterations.
Difficulties. Nonetheless, once you know the specific RAW format you may find a way to formally convert
the image into your RAW format. This will involve not trivial programming efforts, starting from the modeling of the inverse transformation and ending into coding in the preferred language the algorithm.
Existing Programs. The convert
program from the Imagemagick package under Linux (or other OSes) cannot convert any formats directly into the Canon RAW format, it is only capable to read (R) them... So it can do the opposite of what the OP is searching for: to convert RAW to whatever.
convert -list format | grep Canon
CR2 DNG r-- Canon Digital Camera Raw Image Format
CRW DNG r-- Canon Digital Camera Raw Image Format
But Imagemagick is even a scripting/programming language. Once that you have access to the single pixel values you may save them as you want... programming it. But you need to know the specifications of the desired format.
Moreover, since it is an open source program, you can see in the code the routines used to convert from RAW into whatever and see if you are able to invert them.
On the irreversibility of RAW to JPEG conversion.
This happens for many reasons. Among them:
Some formats are lossy, JPEG is usually one of them. You save space at the cost of artifacts and a general modification of the original values for individual pixels.
In the classic JPEG format there are only 8 bits for color (it means 256 shades). In a CCD sensor usually the couters are of 12 or 14 bits (that means 4096 and 16384 shades).
If you think of your image as a histogram, you are going to average in a single bin (column) of the JPEG image, the values that are contained in 16 and 64 bins in the 12 and 14 bits counters respectively. How can you restore individual bin values from the average?
Even if your JPEG is in the recent 12-14-16 bit format (JPEG_XT), you are not aware of the algorithm used for this transformation inside the camera.
(In simple terms, the sensor can have a non-linear efficiency curve. The manufacturer can take this into account by transforming the number from RAW format to a linear shade level in the JPEG, TIFF, ..., format.)
The geometry of the CCD is different from the geometry of an image.
Roughly speaking, in the images we have a pixel with 3 or 4 values relative to the exact same geometric square. All squares are adjacent.
In the CCD instead we have geometrically separated sensors sensitive to different wavelengths, again speaking roughly for the R, G, B values. These sensors may or may not have the same surface, and certainly have a different efficiency which, among other things, changes according to the number of counts per second.
How these RGB sensor sequences are aligned and spaced from each other depends on the design of the CCD. You can see some examples of Color filter array on internet.
Image from Wikipedia
Image from Ephotozine
How the values of the counts of the red sensors translate, for example, into an image value of a pixel R, depends on their geometric position and on the algorithm chosen which may or may not also take into account the values of nearby sensors, either of those sensitive to the same as to other wavelengths.
All of these things are hardly reversible.