Save spectrogram as an image

This commit is contained in:
Alexander Kojevnikov 2012-08-17 22:15:02 -07:00
parent 0745673f90
commit 8544a03c3c
6 changed files with 46 additions and 65 deletions

@ -76,6 +76,11 @@ void SpekSpectrogram::open(const wxString& path)
void SpekSpectrogram::save(const wxString& path)
{
wxSize size = GetClientSize();
wxBitmap bitmap(size.GetWidth(), size.GetHeight());
wxMemoryDC dc(bitmap);
render(dc);
bitmap.SaveFile(path, wxBITMAP_TYPE_PNG);
}
void SpekSpectrogram::on_idle(wxIdleEvent& evt)

@ -1,35 +0,0 @@
/* spek-spectrogram.vala
*
* Copyright (C) 2010-2011 Alexander Kojevnikov <alexander@kojevnikov.com>
*
* Spek is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Spek is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Spek. If not, see <http://www.gnu.org/licenses/>.
*/
using Cairo;
using Gdk;
using Gtk;
using Pango;
namespace Spek {
class Spectrogram : DrawingArea {
public void save (string file_name) {
Allocation allocation;
get_allocation (out allocation);
var surface = new ImageSurface (Format.RGB24, allocation.width, allocation.height);
draw (new Cairo.Context (surface));
surface.write_to_png (file_name);
}
}
}

@ -31,7 +31,8 @@ BEGIN_EVENT_TABLE(SpekWindow, wxFrame)
EVT_MENU(wxID_ABOUT, SpekWindow::on_about)
END_EVENT_TABLE()
SpekWindow::SpekWindow(const wxString& path) : wxFrame(NULL, -1, wxEmptyString)
SpekWindow::SpekWindow(const wxString& path) :
path(path), wxFrame(NULL, -1, wxEmptyString)
{
SetTitle(_("Spek - Acoustic Spectrum Analyser"));
// TODO: test on all platforms
@ -79,6 +80,7 @@ SpekWindow::SpekWindow(const wxString& path) : wxFrame(NULL, -1, wxEmptyString)
toolbar->Realize();
this->spectrogram = new SpekSpectrogram(this);
this->cur_dir = wxGetHomeDir();
if (!path.IsEmpty()) {
open(path);
@ -119,7 +121,6 @@ static const char *audio_extensions[] = {
void SpekWindow::on_open(wxCommandEvent& WXUNUSED(event))
{
static wxString cur_dir = wxGetHomeDir();
static wxString filters = wxEmptyString;
static int filter_index = 1;
@ -142,7 +143,7 @@ void SpekWindow::on_open(wxCommandEvent& WXUNUSED(event))
wxFileDialog *dlg = new wxFileDialog(
this,
_("Open File"),
cur_dir,
this->cur_dir,
wxEmptyString,
filters,
wxFD_OPEN
@ -150,7 +151,7 @@ void SpekWindow::on_open(wxCommandEvent& WXUNUSED(event))
dlg->SetFilterIndex(filter_index);
if (dlg->ShowModal() == wxID_OK) {
cur_dir = dlg->GetDirectory();
this->cur_dir = dlg->GetDirectory();
filter_index = dlg->GetFilterIndex();
open(dlg->GetPath());
}
@ -160,6 +161,37 @@ void SpekWindow::on_open(wxCommandEvent& WXUNUSED(event))
void SpekWindow::on_save(wxCommandEvent& WXUNUSED(event))
{
static wxString filters = wxEmptyString;
if (filters.IsEmpty()) {
filters = _("PNG images");
filters += wxT("|*.png");
}
wxFileDialog *dlg = new wxFileDialog(
this,
_("Save Spectrogram"),
this->cur_dir,
wxEmptyString,
filters,
wxFD_SAVE
);
// Suggested name is <file_name>.png
wxString name = _("Untitled");
if (!this->path.IsEmpty()) {
wxFileName file_name(this->path);
name = file_name.GetFullName();
}
name += wxT(".png");
dlg->SetFilename(name);
if (dlg->ShowModal() == wxID_OK) {
this->cur_dir = dlg->GetDirectory();
this->spectrogram->save(dlg->GetPath());
}
dlg->Destroy();
}
void SpekWindow::on_exit(wxCommandEvent& WXUNUSED(event))
@ -179,6 +211,7 @@ void SpekWindow::open(const wxString& path)
{
wxFileName file_name(path);
if (file_name.FileExists()) {
this->path = path;
wxString full_name = file_name.GetFullName();
// TRANSLATORS: window title, %s is replaced with the file name
wxString title = wxString::Format(_("Spek - %s"), full_name.c_str());

@ -37,6 +37,8 @@ private:
void open(const wxString& path);
SpekSpectrogram *spectrogram;
wxString path;
wxString cur_dir;
DECLARE_EVENT_TABLE()
};

@ -50,10 +50,6 @@ namespace Spek {
info_bar.message_type = MessageType.INFO;
info_bar.response.connect(() => info_bar.hide());
filter_png = new FileFilter ();
filter_png.set_name (_("PNG images"));
filter_png.add_pattern ("*.png");
var vbox = new VBox (false, 0);
vbox.pack_start (info_bar, false, true, 0);
add (vbox);
@ -85,28 +81,6 @@ namespace Spek {
drag_finish (cx, false, false, time);
}
private void on_file_save () {
var chooser = new FileChooserDialog (
_("Save Spectrogram"), this, FileChooserAction.SAVE,
Stock.CANCEL, ResponseType.CANCEL,
Stock.SAVE, ResponseType.ACCEPT, null);
chooser.set_default_response (ResponseType.ACCEPT);
chooser.set_current_folder (cur_dir);
// Suggested name is <file_name>.png
var file_name = Path.get_basename (spectrogram.file_name ?? _("Untitled"));
file_name += ".png";
chooser.set_current_name (file_name);
chooser.add_filter (filter_png);
chooser.set_filter (filter_png);
if (chooser.run () == ResponseType.ACCEPT) {
file_name = chooser.get_filename ();
cur_dir = Path.get_dirname (file_name);
spectrogram.save (file_name);
}
chooser.destroy ();
}
private void on_edit_preferences () {
var dlg = new PreferencesDialog ();
dlg.transient_for = this;

@ -40,6 +40,8 @@ IMPLEMENT_APP(Spek)
bool Spek::OnInit()
{
wxInitAllImageHandlers();
SpekPlatform::init();
SpekPreferences::get().init();
spek_audio_init();