|
#include "stdafx.h"
#include "Imagr.h"
#include "ImagrDoc.h"
#include <string.h>
#include <malloc.h>
//-----------------------------------------------------------------------
// Inserts a copy of the image onto the undo stack.
//-----------------------------------------------------------------------
void CImagrDoc::OnDo(CString hint)
{
if (!ChkDIB(true)) return;
int *p, *buf; // Image ptrs
Undo_type *h;
unsigned long n = GetImageSize();
// Allocate n sized block of memory from the heap for pixels
if (!(buf = (int *)malloc(n * sizeof(int)))) {
ATLTRACE2("***"__FUNCTION__" malloc() error for size: %d\n", n);
return;
}
h = (Undo_type *) malloc(sizeof(Undo_type)); // New link
if (!h) {
ATLTRACE2("***"__FUNCTION__" malloc() error for size: %d\n", sizeof(Undo_type));
free(buf);
return;
}
p = (int *) m_image.GetBits(); // Ptr to bitmap
memcpy_s(buf, n * sizeof(int), p, n * sizeof(int)); // Copy bitmap to buf
h->p = buf; // Save ptr to buf
h->mod = IsModified(); // Save modified state
h->ptype = m_image.ptype; // Pixel type
strcpy_s(h->hint, sizeof(h->hint), hint); // Save hint
//if (m_image.undo == NULL) { // New stack
//(done by mainframe)AfxGetApp()->GetMainWnd()->GetMenu()->EnableMenuItem(ID_EDIT_UNDO, MF_ENABLED);
//}
h->next = m_image.undo; // Points to old head ptr.
m_image.undo = h; // New head ptr.
return;
}
//-----------------------------------------------------------------------
// Pops previous state of image off the undo stack and restores image.
//-----------------------------------------------------------------------
void CImagrDoc::OnUnDo()
{
int *p; // Image ptr
Undo_type *h;
unsigned long n, nx = 0, ny = 0;
h = m_image.undo; // Head ptr.
p = (int *) m_image.GetBits(); // Ptr to bitmap
// Check if saved state was a different size
if (sscanf_s(h->hint, "Resize %d x %d", &nx, &ny) > 0 ||
sscanf_s(h->hint, "Rotate %*s %d x %d", &nx, &ny) > 0) {
// Start a new CImage of size nx x ny
m_image.Destroy();
if (!m_image.Create(nx, -(int)ny, 32, 0)) {
fMessageBox("Error - " __FUNCTION__, MB_ICONERROR,
"Failed bitmap .Create()");
return;
}
p = (int *) m_image.GetBits(); // Ptr to bitmap
n = nx * ny;
}
else {
n = m_image.GetWidth() * m_image.GetHeight();
}
// Restore bitmap
memcpy_s(p, n * sizeof(int), h->p, n * sizeof(int));
SetModifiedFlag(h->mod); // Reset flag
m_image.ptype = h->ptype;
m_image.undo = h->next; // Save new head ptr.
free(h->p); // Free pixel buffer
free(h); // Free undo ptr.
ChkData(); // Re-check range (not saved by OnDo)
UpdateAllViews(NULL);
return;
}
//-----------------------------------------------------------------------
// Function frees the undo stack
//-----------------------------------------------------------------------
void CImagrDoc::FreeUnDo()
{
Undo_type *i, *j;
for (i = m_image.undo; i; i = j) {
free(i->p);
j = i->next;
//ATLTRACE2("***"__FUNCTION__" free(%d)\n", i);
free(i);
}
m_image.undo = NULL;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.