Baseline correction algorithm for Raman spectra. Raman baseline removal Can someone please help with baseline correction algorithm?
My task is to build an algorithm that normalizes baseline of spectrum WITHOUT using any additional software like Mathematica etc.
I have a spectrum. This is a sample from RRUFF database:

I read all color pixel values and plot their brightness over wavelength. Of course I get distorted curve, like this:

Question – please suggest an algorithm that turns curve 2 into curve 1. I understand there may be more then one algorithm involved, but baseline removal is what I am after. Noise correction etc – is a separate question.
I also understand that this may be a question for programmers, not physicists, but I think you guys are better familiar with this issue and may be quick to point me in the right direction. 
On related note, you may know, if I am plotting the wrong value to begin with, I calculate luminance as average of red, green and blue values of read pixel (screen colors composed of 3 RGB values 0–255 each)
 A: For each abscissa, read off the numerical values for the ordinates of the two plots and pair them - the first value from your spectrum as $x$ and the value from the P Cygni data as $y$.
Now load these pairs into a spreadsheet, or Mathematica, or something similar, plotting the points $y$ versus $x$. You can now see whether there is a good quality relationship between the two. By "good quality" I mean the following: you will get many pairs with approximately the same value of $x$, and "good quality" means that all or the vast majority of these pairs will have the same value of $y$, to within a small error. Visually, the points cluster around a thin curve defining some function $y=f(x)$ rather than being scattered uniformly over a two dimensional region.
There are of course rigorous statistical tests that define this quality in different ways, but I'd suggest just visual inspection for a start.
If the data look as though they define a relationship, then the next part is to find out the nature of the function $y=f(x)$.  You could begin with linear regression: say linear regression of Tschebyschev polynomials. Do not try to fit a "naïve" polynomial with regression basis $\{1,\,x,\,x^2,\,\cdots\}$ as this will be numerically highly unstable for polynomial orders greater than about 3. Use the Tschebyschev polynomials, with arguments normalized to cover your range of abscissas, as your basis instead. Alternatively, Mathematica has inbuilt ways of doing nonlinear fitting of rational functions to curves and spitting out a function object that you can use to interpolate your data.
